如何格式化此日期+;JavaScript带来的时间?(+;jsFiddle)
可能重复:如何格式化此日期+;JavaScript带来的时间?(+;jsFiddle),javascript,jquery,Javascript,Jquery,可能重复: 如何将以下日期和时间格式化为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()); } upda
如何将以下日期和时间格式化为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夏令时)
如果日期/月/小时/分钟小于10,您可以填充
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]);
}
函数更新时间(){
var now=新日期(),
d=[];
d[0]=now.getFullYear().toString(),
d[1]=now.getMonth()+1,//月份是基于0的
d[2]=now.getDate(),
d[3]=now.getHours(),
d[4]=now.getMinutes();
//不推荐使用getYear()手动执行YY
//如果希望YYY而不是YY,请删除下一行
d[0]=d[0]。子字符串(d[0]。长度-2);//不使用substr(-2),因为它在IE中不起作用
//前导零
对于(var i=1;我已经花了一些精力来回答你的问题,但是我没有太多时间来磨磨蹭蹭了。如何添加0
以只允许两分钟?(08代替8)我把它留作你的作业。将每个实体传递给函数,如果小于10,则返回0+值
好吧,如果我知道,我不会要求你…对不起,但我必须回忆起我对这个答案的接受,直到它完成。对不起,我不能帮助你。这也是为了帮助,不是“做我的家庭作业”。我帮助你是因为我知道你会我不知道答案。但我希望你能做一些工作。谢谢你。我觉得奇怪的是,我无法将它工作到我的页面中…你复制了整个函数吗?完全是的。这是因为我也有其他jQuery脚本吗?不太可能,因为我的脚本只使用局部变量。好吧,尝试一下其他的,即使它这与几乎相同。请按而不是创建索引。我要说的是检查您的javascript控制台是否有错误(安装了Firebug的Chrome和FF中的F12,或Firefox的本机控制台中的Ctrl+Shift+K),如果仍然不起作用,那么您最好的选择就是找到我的错误。我已经接受了您的答案。非常感谢!
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]);
}