momentjs-日期(年、月、日)到毫秒

momentjs - Date (Year, month, day) to milliseconds

本文关键字:日期 momentjs-      更新时间:2023-09-26

我在应用程序中使用momentjs。但我不是很理解。

我需要一个毫秒的日期,但我只需要天、月和年。其他应设置为0(小时、分钟、秒和毫秒)

我正在尝试这样的东西:

var a = moment();
a = moment(a, "YYYY MM DD").valueOf();

但它会返回毫秒的额外信息,我不想要。

我怎么能从几天、几个月和几年中只得到几毫秒?

如果您想获得给定日期午夜后的毫秒数,则不需要时间
只需使用setHours方法:

let date = new Date();
date.setHours(0, 0, 0, 0);
console.log(+date);

随着时间的推移,您可以使用startOf:

let date = moment()
date.startOf('day');
console.log(+date);