When working with JavaScript, the Date.parse() method is a handy tool for parsing date strings formatted according to standards like RFC 2822 or ISO 8601.
RFC 2822 specifies the format for valid date/time strings in detail. It mentions that the time zone designator should either be a UTC-style offset (e.g. -0800) or an obsolete designator like PDT or CST. ISO 8601, on the other hand, only allows UTC time zone designators.
To handle the discrepancies between time zone designators and UTC offsets, you may need to create a mapping function. However, it's important to note that this approach can be tricky due to ambiguities in some obsolete designators, as explained by @MattJohnson in a comment below.
var timeZoneDesignatorMap = {
akdt : '-0800',
akst : '-0900',
art : '-0300'
// Add other designators here.
};
function mappedDateParse(dateString) {
var name, newDateString, regex;
for (name in timeZoneDesignatorMap) {
regex = new RegExp(name, 'i');
if (dateString.search(regex) !== -1) {
newDateString = dateString.replace(regex, timeZoneDesignatorMap[name]);
return Date.parse(newDateString);
}
}
return Date.parse(dateString);
}
console.log(mappedDateParse('June 20 2015 10:22 AKDT'));
console.log(mappedDateParse('June 20 2015 10:22 -0800'));
console.log(mappedDateParse('June 20 2015 10:22 pDT'));