I would like to get the beginning and end of the current day (and accessorily of tomorrow by .add(1, 'day')
) using moment
.
What I am getting now is
now = moment() console.log('now ' + now.toISOString()) console.log('start ' + now.startOf('day').toISOString()) console.log('end ' + now.endOf('day').toISOString())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>
This outputs right now
now 2018-04-18T21:20:02.010Z start 2018-04-17T23:00:00.000Z end 2018-04-18T22:59:59.999Z
Since the times are shifted by an hour, I believe that this is something related to timezones, though I fail to understand how this can be relevant: no matter the time zone, the day in that timezone begins right after midnight today and ends right before midnight today.
Use the Moment.We call moment to create a Moment object with the current date and time. Then we call clone to clone that object. Then we call startOf with 'month' to return the first day of the current month. And then we call format to format the date into the human-readable YYYY-MM-DD format.
to get the current date time with moment . Then we call add with 1 and 'weeks' to add 1 week to the current date and time. And then we call startOf and endOf with 'isoWeek' to get the start and end of next week respectively. isoWeek starts Monday and ends Sunday.
It is giving you midnight local time, but you're printing it out in zulu time. Try using toString
instead, it will print the time out in local time.
now = moment() console.log('now ' + now.toString()) console.log('start ' + now.startOf('day').toString()) console.log('end ' + now.endOf('day').toString())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With