如何链接javascript日期功能 [英] How to chain javascript date functions
本文介绍了如何链接javascript日期功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我想要这样的东西:
var d = new Date()。setMinutes(0).setSeconds(0).setMilliseconds(0);
此语法与错误相符:
(new Date).setMinutes(0).setSeconds不是函数
我知道我可以这样做:
var d = new Date();
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
但这感觉很冗长,很麻烦。有没有更好的方法?
解决方案
您可以使用setMinutes方法设置秒和msecs:
var d = new Date();
d.setMinutes(0,0,0);
还可以使用hours- d.setHours(0,0,0, 0);
Is there a way to chain javascript date functions?
for example, I would like to something like this:
var d = new Date().setMinutes(0).setSeconds(0).setMilliseconds(0);
this syntax breaks with error:
(new Date).setMinutes(0).setSeconds is not a function
I know I can do this:
var d = new Date();
d.setMinutes(0);
d.setSeconds(0);
d.setMilliseconds(0);
but this feels verbose and cumbersome. Is there a better way?
解决方案
You can set seconds and msecs with the setMinutes method:
var d = new Date();
d.setMinutes(0,0,0);
Also works with hours- d.setHours(0,0,0,0);
这篇关于如何链接javascript日期功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文