2

I have an application that parses information from RSS feeds which specify that dates should be specified in RFC-822 format - for which Date.parse works fine. In the main, people put in conformant timestamps. However, I've come across a situation where someone is giving the date and time as (e.g.) "Sat, 1 Dec 2018 00:00:00 MSK" (where as far as I can tell MSK is Moscow time zone).

Is there any way of programatically converting such a string to something I can use with Date.parse (clearly for MSK I could hardcode to + (or -) the offset of Moscow but I'd like to be a little more flexible/reliable than that)

For various reasons I'd prefer to avoid having to include another library to do this, so answers involving standard javascript would be appreciated

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
  • You should find some ideas how to solve this at this link: https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc – Gaston Jan 06 '19 at 11:00
  • that seems to be more of a question on 'given a date how do i show it in the local timezone' question. I want to go the other way – Tom Tanner Jan 06 '19 at 12:32

1 Answers1

2

So from my experince such problems i mean all timezone issues was easily solved by external libraries like momentJs. everytime i had date problems moment js solved it without any troubles.

Keep in mind that playing with dates in js is not perfectly implemented out of the box and couse a lot of troubles. If you dont want adding additional library you will need to write your own convertedr function something like:

const date = 'Sat, 1 Dec 2018 00:00:00 MSK';

const dateConverted = (date)=> {
    return date.replace('MSK', '+0300');
}

const convertedDate = dateConverted(date);
Date.parse(convertedDate)

But in this case you will need to extended dataConverter function with other not recognised by js timezones like. For example GMT + 3 can be represented also by another abbrevations like:

  • AST – Arabia Standard Time
  • C – Charlie Time Zone
  • EAT – Eastern Africa Time
  • EEST – Eastern European Summer Time
  • FET – Further-Eastern European Time
  • IDT – Israel Daylight Time
  • SYOT – Syowa Time
  • TRT – Turkey Time

There is no other way own converter function or external library which has that implemented.

Łukasz Blaszyński
  • 1,536
  • 1
  • 8
  • 13
  • So what you are saying is there is no standard javascript library that can convert a timezone string like MSK to an offset from UTC? – Tom Tanner Jan 06 '19 at 16:31
  • Its not a part of VanillaJS, and there are libraries which can help with you problem for example you can use library like MomentJs to handle it. In case of vanillaJs you will need to create converters by yourself. – Łukasz Blaszyński Jan 06 '19 at 16:33