0

I have input string like 2023-02-02 15:35 CET and I have to convert this into UTC time.

I'm trying to find a solution for this, but I'm not able to figure out how to convert input string into Date() object in js.

My main problem is that I cannot figure out how to create Date() with timezone. Time-zones can differ and it could be anything like BST, HKTect.

Can somebody help how to get UTC time from input string like this?

Roman Pecho
  • 69
  • 1
  • 6
  • 1
    Re *I cannot figure out how to create Date() with timezone*. You can't. ECMAScript *Date* objects don't have a timezone. Civil timezone abbreviations like CET are ambiguous and may represent different offsets. – RobG Aug 24 '23 at 20:53
  • "BST" - is that "Bangladesh Standard Time", "Bougainville Standard Time", "British Summer Time", or (historically) "British Standard Time"? – Matt Johnson-Pint Aug 28 '23 at 18:38

1 Answers1

-1

to convert your date to UTC you can use the .toUTCString() method.

var inputDate = "2023-02-02 15:35 EST";
var date = new Date(inputDate);
var dateUTC = date.toUTCString();
console.log(dateUTC);
  • `EST` is working without problem, but other timezones like `CET`, `BST` is not working with this. I have edited my question to show this – Roman Pecho Aug 24 '23 at 17:09
  • "2023-02-02 15:35 EST" is not a format supported by ECMA-262 so parsing is implementation dependent. Safari at least treats it as an invalid date. – RobG Aug 24 '23 at 20:54