typescript moment.js 在现有日期上设置时间未正确设置时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34070152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:09:00 来源:igfitidea点击:
moment.js set time on existing date is not setting the time correctly
提问by daehaai
let end: Date = new Date();
let h: string = "13";
let m: string = "20";
moment(end).set({ hour: parseInt(h, 10), minute: parseInt(m, 10) });
I am trying to set time on an existing date. however the above doe is not setting the time in the date. The time always comes as ...00:00:...
我正在尝试在现有日期设置时间。然而,上面确实没有在日期中设置时间。时间总是以...00:00:...
Any idea?
任何的想法?
Thanks,
谢谢,
回答by mk.
You have to convert your moment to a Date
, and assign that Date
object to your end
variable:
您必须将您的时刻转换为 a Date
,并将该Date
对象分配给您的end
变量:
end = moment(end)
.set({ hour: parseInt(h, 10), minute: parseInt(m, 10) })
.toDate();