heyr
heyr

Reputation: 5794

Get the beginning of the day in timestamp using Momentjs library

For instance the timestamp is: 1542769200 which its output is: GMT: Wednesday, 21 November 2018 03:00:00. However what I am trying to achieve is to get the beginning of this day, by having the hour, minutes and seconds set to 00.00.00.

Upvotes: 1

Views: 1589

Answers (4)

ylun
ylun

Reputation: 2534

I would recommend using valueOf() instead of .unix()*1000

moment().startOf("day").valueOf()

Upvotes: 0

Herlangga Sefani
Herlangga Sefani

Reputation: 36

This is what you looking for :

moment().startOf('day').unix()

Upvotes: 0

Akrion
Akrion

Reputation: 18525

You could do something like this:

var timestamp = 1542769200;
var date = moment.unix(timestamp).startOf('day')

console.log('date:', moment.unix(timestamp).format())
console.log('start of day:', date.format())
console.log('new unix:', date.unix())
console.log('test:', moment.unix(date.unix()).format())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Upvotes: 1

knighteq
knighteq

Reputation: 66

moment(moment(moment().unix() * 1000).startOf('day').unix() * 1000)

Upvotes: 1

Related Questions