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?

7
  • 1
    Date.parse() is returning NaN unfortunately Commented Sep 7, 2019 at 3:05
  • yes directly, it won't be useful - that's why I removed the comment - writing an answer though :) Commented Sep 7, 2019 at 3:06
  • Okay thank you for the help! Commented Sep 7, 2019 at 3:09
  • 1
    Try this, new Date("Sep 7, 2019, 1:00 PM CEST".replace('CEST', '(CEST)')) Commented Sep 7, 2019 at 3:14
  • Worked perfectly thank you if you want to write it as an answer ill give you the check Commented Sep 7, 2019 at 3:18

4 Answers 4

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 :)

Sign up to request clarification or add additional context in comments.

9 Comments

Hi @casper, Can you explain reason behind changing CEST to (CEST). It works perfectly fine but how?
it is not a proper conversion - the converted value is not equal to the original string
@PrasadTelkikar I referred this article to address this problem. According to article, timezone name need to wrap in parentheses flaviocopes.com/javascript-dates
Good article to understand Date in javascript. +1 for article
@PrasadTelkikar Yeah, I've included article in the answer.
|
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

Comments

-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!

1 Comment

This likely changes the timezone of the date so that the time value is different.
-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);

4 Comments

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 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.
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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.