js使用方法:
var now = new Date(1500541859000);
var year = now.getFullYear(),
month = now.getMonth() + 1,
date = now.getDate(),
hour = now.getHours(),
minute = now.getMinutes(),
second = now.getSeconds();
function p(s) {
return s < 10 ? '0' + s: s;
}
var timeFormat = year + "-" + p(month) + "-" + p(date) + " " + p(hour) + ":" + p(minute) + ":" + p(second);
jQuery使用方法:
$.myMethode = {
//定义一个方法formatTime
formatTime: function(time){
var now= new Date(parseInt(time));
var year = now.getFullYear(),
month = now.getMonth() + 1,
date = now.getDate(),
hour = now.getHours(),
minute = now.getMinutes(),
second = now.getSeconds();
timeFormat = year + "-" + $.numCheck.p(month) + "-" + $.numCheck.p(date) + " " + $.numCheck.p(hour) + ":" + $.numCheck.p(minute) + ":" + $.numCheck.p(second);
return timeFormat;
}
}
$.numCheck = {
//定义一个方法
p: function(s){
return s < 10 ? '0' + s: s;
}
}
// 调用方法
var formateTime = $.myMethode.formatTime(1500541859000);

本文介绍了如何使用JavaScript和jQuery进行日期格式化的具体方法。通过创建自定义函数来处理时间戳,将其转换为易于阅读的日期格式。此外,还展示了如何在jQuery中封装此功能以便于复用。
350

被折叠的 条评论
为什么被折叠?



