标签:
js调用的new Date()不是格式化的时间,例如
var date=new Date();
alert(date);
显示的结果:Wed Oct 14 22:28:32 UTC+0800 2015
今天这里分享一个js写的foramt()函数,希望能对大家有所帮助!
function formate(datetime) {
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1; //js从0开始取
var date = datetime.getDate();
var hour = datetime.getHours();
var minutes = datetime.getMinutes();
var second = datetime.getSeconds();
if (month < 10) {
month = "0" + month;
}
if (date < 10) {
date = "0" + date;
}
if (hour < 10) {
hour = "0" + hour;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (second < 10) {
second = "0" + second;
}
var time = year + "-" + month + "-" + date + " " + hour + ":" + minutes + ":" + second;
return time;
}
var date=new Date();
var datetime=format(date);
alert(datetime);
显示的结果:2015-10-14 22:28:32
参考来源:http://blog.sina.com.cn/s/blog_5fe080e60100dnij.html
标签:
原文地址:http://www.cnblogs.com/haozi2015/p/4881038.html