Search code examples
javascriptdatemomentjsmilliseconds

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


I'm using momentjs in my app. But I don't understand it very well.

I need to get a date in milliseconds, but I need only day, month and year. Others should be set to 0 (Hours, minutes, seconds and milliseconds)

I'm trying with some like this:

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

But it returns milliseconds with extra info I don't want.

How can I get only milliseconds from days, months, and years?


Solution

  • If you want to get the milliseconds from midnight of a given date, you don't need moment.
    Just use the setHours method:

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

    With moment, you can use startOf:

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