87,992
社区成员
发帖
与我相关
我的任务
分享alert( new Date().toLocaleString() ) // chrome 下不能格式化为 2013年11月6日 8:54:11[/quote]难道是浏览器版本不同?
[/quote]
你看看 http://www.w3help.org/zh-cn/causes/SJ2004alert( new Date().toLocaleString() ) // chrome 下不能格式化为 2013年11月6日 8:54:11[/quote]难道是浏览器版本不同?
alert( new Date().toLocaleString() ) // chrome 下不能格式化为 2013年11月6日 8:54:11var format = function(time, format){
var t = new Date(time);
var tf = function(i){return (i < 10 ? '0' : '') + i};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
switch(a){
case 'yyyy':
return tf(t.getFullYear());
break;
case 'MM':
return tf(t.getMonth() + 1);
break;
case 'mm':
return tf(t.getMinutes());
break;
case 'dd':
return tf(t.getDate());
break;
case 'HH':
return tf(t.getHours());
break;
case 'ss':
return tf(t.getSeconds());
break;
}
})
}
alert(format("Thu Aug 22 2013 15:12:00 GMT+0800", 'yyyy-MM-dd HH:mm:ss'))
借花献佛了,这是以前在论坛看到的一位大神给出的代码(不记得哪个帖子了)。
功能是按照自定义的格式,输出时间
测试了下,是没有问题的。感觉这段代码写的很好,可以参考一下啊!!<script type="text/javascript">
var d = new Date("Thu Aug 22 2013 15:12:00 GMT+0800");
console.log(d.toLocaleString());
console.log(d.toLocaleString().replace("日","").replace(/[年月]/g,"-"));
</script>var test = "Thu Aug 22 2013 15:12:00 GMT+0800";
var date = new Date(test);
alert(formatDate(date));
function formatTen(num) {
return num > 9 ? (num + "") : ("0" + num);
}
function formatDate(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return year + "-" + formatTen(month) + "-" + formatTen(day) + " " + formatTen(hour) + ":" + formatTen(minute) + ":" + formatTen(second);
} var s = 'Thu Aug 22 2013 15:12:00 GMT+0800';
s = new Date(s);
alert( s.getFullYear() )
根据你需要的格式 再 拼
<script>
Date.prototype.toHyphenDateString = function() {
var year = this.getFullYear();
var month = this.getMonth() + 1;
var date = this.getDate();
if (month < 10) { month = "0" + month; }
if (date < 10) { date = "0" + date; }
var hours = this.getHours();
var mins = this.getMin
var mins = this.getMinutes();
var second = this.getSeconds();
return year + "-" + month + "-" + date + " " + hours + ":" + mins + ":" + second;
};
$(function (){
var date = new Date();
// 输出2013-11-06 16:05:50
console.log(date.toHyphenDateString());
});
</script>