Soragim
Soragim

Reputation: 337

Moment js - can't get time passed from the beginning of the day

I'm trying to get the time in seconds that passed since 00:00:00 of today. e.g at 13pm I want to get that 13*60*60*1000 seconds have passed. I tried using moment().valueOf() but I get the unix epoch time although it states in other questions that it's supposed to retrieve what I'm looking for. (I'm using moment js in react native for that matter)

Upvotes: 0

Views: 953

Answers (2)

Joel Cox
Joel Cox

Reputation: 3469

moment() gives you the current time.

moment().startOf('day') gives you the start of the current day.

(moment() - moment().startOf('day')) will give you the current number of milliseconds since the start of the day.

Divide that by 1000 and you've got the number of seconds since the start of the day.

var seconds = (moment() - moment().startOf('day')) / 1000;

Upvotes: 1

Alexei Darmin
Alexei Darmin

Reputation: 2129

You want to get the current time, and subtract it by the time at the very start of the day.

const now = new moment()
const dayStart = new moment().format('YYYY MM DD')
const msSinceStart = now.diff(dayStart)

Upvotes: 1

Related Questions