由于jquery没有日期转换,所以要导入dateformat.js这个文件,然后调用dateformat(new date(),'yyyy-mm-dd')方法即可;
// date必填, pattern默认'yyyy-mm-dd hh:mm:ss'
function dateformat (date, pattern) {
var week = {'0':'日', '1':'一', '2':'二', '3':'三', '4':'四', '5':'五', '6':'六'};
pattern = pattern == null ? 'yyyy-mm-dd hh:mm:ss' : pattern;
var o = {
'm+': date.getmonth() + 1, // 月份
'd+': date.getdate(), // 日
'h+': date.gethours() % 12 === 0 ? 12 : date.gethours() % 12, // 小时
'h+': date.gethours(), // 小时
'm+': date.getminutes(), // 分
's+': date.getseconds(), // 秒
'q+': math.floor((date.getmonth() + 3) / 3), // 季度
's': date.getmilliseconds() // 毫秒
};
if (/(y+)/.test(pattern)) {
pattern = pattern.replace(regexp.$1, (date.getfullyear() + '').substring(4 - regexp.$1.length));
}
if (/(e+)/.test(pattern)) {
pattern = pattern.replace(regexp.$1, ((regexp.$1.length > 1) ? (regexp.$1.length > 2 ? '星期' : '周') : '') + week[date.getday() + '']);
}
for (var k in o) {
if (new regexp('(' + k + ')').test(pattern)) {
pattern = pattern.replace(regexp.$1, (regexp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substring(('' + o[k]).length)));
}
}
return pattern;
}
dateformat(new date());
// 输出: 2017-07-12 17:49:44
dateformat(new date(), 'yyyy年 mm月 dd日 hh时 mm分 ss秒 s毫秒 周e 第q季度');
// 输出: 2017年 07月 12日 17时 55分 49秒 360毫秒 周三 第3季度
以上就是dateformat的使用介绍的详细内容。