I want to get the epoch seconds using momentjs.

For example, time right now is 2019-04-20T15:07:04.388Z EST, I want to get yesterday start time epoch 2019-04-19T00:00:00.000Z in UTC.

I have tried the below code -

    const now = new Date();
    const start = moment(now) // get current datetime
        .utcOffset(0) // convert to UTC
        .subtract(24, "hours") // go 24 hours into the past
        .startOf("day") // get the start of the date we landed on
        .unix(); // get unix timestamp

The output of the above program is -

    Sat Apr 20 2019 15:11:23 GMT-0400 (EDT) {}

According to https://www.unixtimestamp.com/index.php the 1555650000 translates to Fri, 19 Apr 2019 05:00:00 +0000. But I want it to be Fri, 19 Apr 2019 00:00:00 +0000 in UTC.

The momentjs version used in our code -

"moment": "2.24.0",
"moment-timezone": "^0.5.23"

Any idea how can I get this?

1 Answers

Senthil On Best Solutions

you can try this one line statement

console.log( moment.utc().subtract(1, 'days').startOf('day').toString()); 

Output : Sun Apr 21 2019 00:00:00 GMT+0000