Akshay Prajapati
Akshay Prajapati

Reputation: 21

Convert IST date to UTC in RSS feed

I've a Date in string format 'Wed, 08 Apr 2020 15:24:06 IST' I want to convert it into javascript Date so that I can perform operations. I want to get UTC date from IST or whatever is passed by the api and I have to convert it as per user timezone so I can't remove IST from it. IST(Indian Standard Time) is just an example it could be AET(Australian Eastern Time),EET(Eastern European Time), etc...

new Date('Wed, 08 Apr 2020 15:24:06 IST')

I'm Getting Invalid Date

This string comes from the Times Of India RSS feeds, such as this one. How do I handle time zone abbreviations in RSS feeds?

Upvotes: 1

Views: 410

Answers (3)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241748

Without additional context, this isn't possible. Time zone abbreviations are not standardized nor are they unique.

In your example, you said IST means "Indian Standard Time" to you, however IST is also used for "Israel Standard Time" and "Irish Standard Time". All of which have very different time zone offsets.

Another common example is CST, which could be "Central Standard Time", "Cuba Standard Time", or "China Standard Time".

Instead - you should go back to the original source and obtain a time zone offset (such as +05:30) or a time zone identifier (such as Asia/Kolkata), or derive that context in some other way.

You said you were retrieving this timestamp from an RSS feed from Times of India. That is the type of additional context I'm speaking of. Since you know that it's and Indian news source, you can safely assume that IST means India Standard Time.

You should know that the format of the RSS feed at Times of India is not using timestamps correctly. The RSS 2.0 specification requires timestamps to be in RFC 822 format. In that format, IST is not a valid time zone. Indeed, if you put it into a site like Feed Validator, it will give you the error "element must be an RFC-822 date-time".

Note that RFC 822 is old and outdated. It's been superseded with RFC 2822, and then again by RFC 5322. Time zone abbreviations have all been obsoleted and moved to the obs-zone section. Thus, they really shouldn't be used at all. Only numeric offsets should appear. That said, many RSS implementations follow the old standard, and thus it is reasonable to accept some of the abbreviations specified in obs-zone. Specifically, all implementations should accept the following:

"Z"    == "+0000"
"UT"   == "+0000"
"GMT"  == "+0000"
"EST"  == "-0500"
"EDT"  == "-0400"
"CST"  == "-0600"
"CDT"  == "-0500"
"MST"  == "-0700"
"MDT"  == "-0600"
"PST"  == "-0800"
"PDT"  == "-0700"

(The other military zones "A" - "Y" should never be used.)

Note that IST is not among them. Thus to answer your question directly, when parsing an RFC 822 timestamp that contains a non-standard time zone abbreviation, you will need to replace the abbreviation with an offset yourself before parsing. In this case:

var s = 'Wed, 08 Apr 2020 15:24:06 IST';
var d = new Date(s.replace('IST', '+0530'));

You could also replace the other obs-zone abbreviations I listed, but it should generally not be necessary. Most implementations of ECMAScript will parse those (even though they are not required to by spec).

If you encounter other RSS feeds with other time zone abbreviations, you will need to manually make a determination as to how to interpret them. There is no definitive list.

Some additional reading:

Upvotes: 2

joy08
joy08

Reputation: 9662

If you want to parse or display a moment in UTC, you can use moment.utc()

var res = moment.utc("Wed, 08 Apr 2020 15:24:06",'DD-MMM-YYYY').format("YYYY-MM-DD");
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Upvotes: 0

Armash Ansari
Armash Ansari

Reputation: 78

Try this:

moment('Wed, 08 Apr 2020 15:24:06').utc();

And you are getting error because of IST just remove that and it will not give you an error

If you want only date then use this:

moment('Wed, 08 Apr 2020 15:24:06').utc().format('YYYY-MM-DD');

Upvotes: 0

Related Questions