I am using a third party API along with Moment JS for some date formatting in Javascript.
The third party API groups some data/results with an epoch
date in milliseconds, such as:
1559260800000
This equates to:
GMT: Friday, 31 May 2019 00:00:00
Your time zone: Friday, 31 May 2019 01:00:00 GMT+01:00 DST
Relative: In 15 days
We are using a Flatpickr calendar for date selection. When a date is selected it returns the selected date in the local date format such as:
Fri May 31 2019 00:00:00 GMT+0100 (British Summer Time)
If I use moment to convert that I get the following:
var epoch = moment.utc(dayElem.dateObj).valueOf();
1559257200000
This equates to:
GMT: Thursday, 30 May 2019 23:00:00
Your time zone: Friday, 31 May 2019 00:00:00 GMT+01:00 DST
Relative: In 15 days
I understand the issue relates to the local timezone and the first being GMT 00:00:00 and second being GMT 00:01:00 but I am unsure how to resolve it.
Furthermore, later dates in the year such as December when we are outside of BST the dates are GMT so I need this to work then too.
Since your date picker is producing Date
objects in terms of local time, but you actually desire them to be in terms of UTC, then you can do the following:
var timestamp = moment(dayElem.dateObj).utc(true).valueOf();
Passing true
to the utc
function will tell it to keep the date and time value and shift the offset, with the trade-off of picking a different moment in time. In your case, that seems like what you need.
Also, Please don't call it Epoch Time. :)