JS格式化时间
2015-07-31 13:59
495 查看
var now = new Date();//今天
var tomo = new Date((now/1000+86400*1)*1000);//明天
$("#staDate").val(formatDate((now),"yyyy-MM-dd"));
$("#endDate").val(formatDate((tomo),"yyyy-MM-dd"));
//格式化日期,
function formatDate(date,format){
var paddNum = function(num){
num += "";
return num.replace(/^(\d)$/,"0$1");
}
//指定格式字符
var cfg = {
yyyy : date.getFullYear() //年 : 4位
,yy : date.getFullYear().toString().substring(2)//年 : 2位
,M : date.getMonth() + 1 //月 : 如果1位的时候不补0
,MM : paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0
,d : date.getDate() //日 : 如果1位的时候不补0
,dd : paddNum(date.getDate())//日 : 如果1位的时候补0
,hh : date.getHours() //时
,mm : date.getMinutes() //分
,ss : date.getSeconds() //秒
}
format || (format = "yyyy-MM-dd hh:mm:ss");
return format.replace(/([a-z])(\1)*/ig,function(m){return cfg[m];});
}
var tomo = new Date((now/1000+86400*1)*1000);//明天
$("#staDate").val(formatDate((now),"yyyy-MM-dd"));
$("#endDate").val(formatDate((tomo),"yyyy-MM-dd"));
//格式化日期,
function formatDate(date,format){
var paddNum = function(num){
num += "";
return num.replace(/^(\d)$/,"0$1");
}
//指定格式字符
var cfg = {
yyyy : date.getFullYear() //年 : 4位
,yy : date.getFullYear().toString().substring(2)//年 : 2位
,M : date.getMonth() + 1 //月 : 如果1位的时候不补0
,MM : paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0
,d : date.getDate() //日 : 如果1位的时候不补0
,dd : paddNum(date.getDate())//日 : 如果1位的时候补0
,hh : date.getHours() //时
,mm : date.getMinutes() //分
,ss : date.getSeconds() //秒
}
format || (format = "yyyy-MM-dd hh:mm:ss");
return format.replace(/([a-z])(\1)*/ig,function(m){return cfg[m];});
}
相关文章推荐
- js 创建可扩展库
- js 错误/异常处理
- 代码复用 -- 深入了解javascript
- JS今天坑了我一把
- JS今天坑了我一把
- Json——js和C#对Json的操作
- JS 操作 DOM
- 原生js实现ajax和将数组以列表形式显示出来
- The Future of JavaScript MVC Frameworks
- js 实现飘浮
- js运动框架
- JS方法
- Newtonsoft.Json 高级用法
- 【分享】 封装js操作textarea 方法集合(兼容很好)。
- elasticlunr.js 最新版本v0.6.7发布啦
- js中的三个编码函数:escape,encodeURI,encodeURIComponent
- javascript实现检验的各种规则
- 纯JS实现本地图片预览的方法
- javascript动画算法实例分析
- js正则验证方法大全