可能重复:
how to display a date as 2/25/2007 format in javascript, if i have date object
如何格式化以下日期+时间 2012-07-24 17:00 ?
我正在尝试使用
formatDate('yy-mm-dd HH:ii', now));
没有运气。
jQuery(document).ready(function() {
var foo = jQuery('#foo');
function updateTime() {
var now = new Date();
foo.val(now.toString());
}
updateTime();
setInterval(updateTime, 5000); // 5 * 1000 miliseconds
});
这回报我 2012年7月25日星期三17:14:02 GMT + 0300(GTB日光时间)
答案 0 :(得分:0)
jQuery(document).ready(function() {
var foo = jQuery('#foo');
function updateTime() {
var now = new Date();
var date = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes();
foo.val(date);
}
updateTime();
setInterval(updateTime, 5000); // 5 * 1000 miliseconds
});
如果日期/月/小时/分钟小于10,您可以填0
。
答案 1 :(得分:0)
这应该完成它:
function updateTime() {
var now = new Date(),
d = [];
d[0] = now.getFullYear().toString(),
d[1] = now.getMonth()+1, //months are 0-based
d[2] = now.getDate(),
d[3] = now.getHours(),
d[4] = now.getMinutes();
//doing YY manually as getYear() is deprecated
//remove the next line if you want YYYY instead of YY
d[0] = d[0].substring(d[0].length-2); //not using substr(-2) as it doesn't work in IE
//leading zeroes
for (var i=1; i<=4; i++)
if (d[i] < 10) d[i] = '0' + d[i];
foo.val(d[0] + '-' + d[1] + '-' + d[2] + ' ' + d[3] + ':' + d[4]);
}
MDN Javascript Date Object
10 ways to format time and date using JavaScript
Formatting a date in JavaScript