您的位置:首页 > Web前端 > JQuery

js和jQuery格式化时间

2017-12-27 16:32 274 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息