2

I've read the documentation as best as I can but cannot work this out.

I have a date/time I'm getting from Bootstrap DateTime Picker that is an ISO 8601 string like this:

2016-04-01T22:00:00+01:00

That string is the result of picking the 1st of April at 10pm in the date/time picker.

I want to convert that to a unix timestamp to store in the db

When I use:

moment(date).format('X')

I get the following unix timestamp: 1459544400

This correlates to 2016-04-01T21:00:00+00:00 which is 1 hour behind the time I picked. No matter what I do, I can't seem to get the unix timestamp to take the offset in to account. Can anyone help me and tell me what I am missing here?

Many thanks in advance!

Sean
  • 2,609
  • 1
  • 18
  • 34
  • Moment is doing the correct thing. What result did you expect? BTW, those two timestamps occur at the same instant. One is not an hour behind the other. – Matt Johnson-Pint Apr 17 '16 at 20:20
  • The problem is though, I'm using this timestamp to set a notification reminder. The timestamp I am getting is always 1 hour behind the time the user inputs using the date/time picker - so my notification will always be 1 hr late. This is being caused by DST. How can I fix that? – Sean Apr 17 '16 at 21:40
  • Well, that scenario isn't really part of your stated question, so I'd suggest you research that separately (there are lots of questions on this subject already). If you can't find what you need, then ask a new question focused on that particular problem, with supporting details. Without seeing any details though, may I suggest you consider that a unix timestamp may not necessarily be the most appropriate form to serialize and persist this data into. An ISO8601 string may work better. Hard to say without knowing more about your complete setup. – Matt Johnson-Pint Apr 17 '16 at 22:20
  • If your scenario is primarily about *scheduling* see [this answer](http://stackoverflow.com/questions/19626177/how-to-store-repeating-dates-keeping-in-mind-daylight-savings-time/19627330#19627330). – Matt Johnson-Pint Apr 17 '16 at 22:21

2 Answers2

2

The one-hour difference is due to Daylight Savings Time, which by definition is not reflected in Unix timestamps.

You may want to consider moment-timezone.js to cope with DST in time conversions.

Guido
  • 876
  • 5
  • 14
1

You can use Date.parse() in javascript.

const isoDate = new Date();
const convertToUnix = Date.parse(isoDate.toISOString());
sks147
  • 196
  • 2
  • 8