2

I have this date

Sep 7, 2019, 1:00 PM CEST

and want to convert it into a timestamp.

How would I go about doing this?

jrocc
  • 1,274
  • 2
  • 18
  • 48

4 Answers4

2

Replace CEST -> (CEST) and try to convert like below,

new Date("Sep 7, 2019, 1:00 PM CEST".replace('CEST', '(CEST)'))

Solution implemented based on this valuable article. Credit goes to article author :)

Casper
  • 1,469
  • 10
  • 19
  • Hi @casper, Can you explain reason behind changing `CEST` to `(CEST)`. It works perfectly fine but how? – Prasad Telkikar Sep 07 '19 at 03:33
  • it is not a proper conversion - the converted value is not equal to the original string – H S Sep 07 '19 at 03:35
  • @PrasadTelkikar I referred this article to address this problem. According to article, timezone name need to wrap in parentheses https://flaviocopes.com/javascript-dates/ – Casper Sep 07 '19 at 03:48
  • 1
    Good article to understand `Date` in javascript. +1 for article – Prasad Telkikar Sep 07 '19 at 03:58
  • @PrasadTelkikar Yeah, I've included article in the answer. – Casper Sep 07 '19 at 04:03
  • what about other timezones when app goes global?? – joyBlanks Sep 07 '19 at 04:13
  • @joyBlanks good question, here i only focused on problem asked by jrocc – Casper Sep 07 '19 at 04:36
  • yes I think using a library would be great like moment and all. – joyBlanks Sep 07 '19 at 04:45
  • Using the built–in parser for formats not supported by ECMA-262 is strongly recommended against as the result is implementation dependent. The string should be manually parsed (maybe 3 lines of code) or a library can be used. Changing the string from one unsupported format to another doesn't change that. – RobG Sep 08 '19 at 05:50
1

This answer is more of a pseudo-code then an exact javascript code.

The format of the string (posted by OP) is not supported natively. One of the answers used moment's moment function with second argument to parse the timezone i.e. CEST part in the querying string basically, but I found that conversion faulty too - https://www.epochconverter.com/timezones?q=1567841400&tz=Europe%2FBerlin - wondering what is 1567841400 try running this answer - https://stackoverflow.com/a/57830429/7986074

So the code would look like this -

  1. Extract the time-zone attribute from the string - i.e. CEST - one may use ''.substr
  2. Convert the extracted time-zone string to the UTC offset.
  3. Use the UTC offset to make the date string.
  4. Parse the string so made with utilities such as Date or moment
H S
  • 735
  • 4
  • 12
-1

You might need to convert your CEST to GMT+0200 which contains the timezone and the offset as well.

const date = new Date('Sep 7, 2019, 1:00 PM CEST'.replace('CEST', 'GMT+0200'));

console.log(date);
fyasir
  • 2,924
  • 2
  • 23
  • 36
  • 1
    For me this produces 2019-09-07T03:00:00.000Z which is incorrect. CEST is +2, so it should produce 2019-09-07T11:00:00Z. I suspect that moment.js is ignoring the timezone abbreviation in the timestamp. Also, an answer should not rely on a library that isn't tagged or mentioned in the OP. – RobG Sep 08 '19 at 06:00
  • @RobG I tried the accepted answer which also produces 2019-09-07T03:00:00.000Z. I might be wrong by the question didn't mention that you can't use momentjs. – fyasir Sep 08 '19 at 06:15
  • It's a convention of SO not to do so to prevent multiple (effectively identical) answers with different libraries. Anyway, moment.js doesn't help in this case. – RobG Sep 08 '19 at 06:38
  • @RobG thanks for enlightening those issues. I removed the moment library and also found native date requires timezone+offset to convert to utc. I updated my answer. you can have a look. – fyasir Sep 08 '19 at 06:42
-1

Did you try passing that string directly into the Date constructor? But before you have to get rid of the timezone. Here is an easy example:

// 1. A variable with your date as a string literal
const dateStr = "Sep 7, 2019, 1:00 PM CEST"

// 2. Get rid of the timezone and use the result to instantiate a new date
const d = new Date(dateStr.slice(0,-4))

// 3. Now that you have your date instance, use getTime() method to get the timestamp
const timestamp = d.getTime()

Hope my answer can help you!