0

Can anyone help me in converting the below date format into the preferred format?

"Tue Dec 08 18:00:00 IST 2020"

preferred format:

2020-12-08 18:00:00 +05:30

  • Please look for library such as `date-fn` – blaz May 17 '21 at 09:18
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – bluevulture May 17 '21 at 09:19
  • @bluevulture not a duplicate, since there is not a date object but a string with an arbitrary\* date format in it that needs to be converted to a different format. \*arbitrary as in not something supported natively by JS. – VLAZ May 17 '21 at 09:27
  • Is it always an IST date? Or do you have other timezone codes as well? – trincot May 17 '21 at 09:40
  • @trincot no, it comes with different timezones from the backend. – Name_is_Vatsal May 17 '21 at 10:04
  • JavaScript doesn't know about all timezone codes, so you'll need to implement your own translation or find a library that has them. I just tried moment-timezone, and it does not recognise IST, so also there you'll have to add it to the list. Better would be of course that the back end uses ISO 8601 compliant output. Then JavaScript can parse it. – trincot May 17 '21 at 10:17

1 Answers1

0

This is not possible because time zone abbreviations are not well defined, and many are not unique.

For example, there are 3 different interpretations of IST:

  • India Standard Time (UTC+05:30)
  • Israel Standard Time (UTC+02:00)
  • Irish Standard Time (UTC+01:00)

You seem to be asking about the first one, but there's no way for a computer to know that when you are considering all of the time zones of the world.

The list of possibilities gets a bit crazy if you start thinking about other details, such as:

  • Some time zones have more than one abbreviation commonly used.
  • There are "invented" abbreviations that some people use and others do not.
  • Many time zones don't have any real abbreviation at all and just use a numeric offset.
  • There are abbreviations from other languages than just English.
  • In some languages, your string including the abbreviation might contain non-Latin characters, such as with Asian languages, Cyrillic languages, Hebrew, etc.

Thus, you cannot just convert whatever abbreviation might be in your string to the correct offset. Instead, you need to generate the string such that it already has the offset in it, or you need additional information - such as a time zone identifier (e.g.: "Asia/Kolkata"), or at least a country code and language to help disambiguate the abbreviation.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575