mde / timezone-js

DEPRECATED: Timezone-enabled JavaScript Date object. Uses Olson zoneinfo files for timezone data.
824 stars 183 forks source link

Formatting timezoneJS.Date with CST/CDT timezone #83

Closed nareshbhatia closed 11 years ago

nareshbhatia commented 11 years ago

I am trying to output timezoneJS.Date in the same format that I receive it - with CST/CDT indicating the timezone (IETF-compliant RFC 2822 format). For example, here are two times on the day when Daylight Savings time began in 2008:

09 Mar 2008 01:00 CST
09 Mar 2008 03:00 CDT

timezoneJS.Date constructor correctly parses the date, but I cannot figure out how to easily format the output to look exactly like the input. Also it appears that I must specify the 2nd parameter although the timezone is included in the input string.

var date1 = new timezoneJS.Date('09 Mar 2008 01:00 CST', 'America/Chicago');
var date2 = new timezoneJS.Date('09 Mar 2008 03:00 CDT', 'America/Chicago');
console.log(date1.toString());
console.log(date2.toString());

The output is shown below, not in the input format and also no timezone string.

2008-03-09 01:00:00
2008-03-09 03:00:00

What is the easiest way to get the desired output?

Thanks.

longlho commented 11 years ago

toString takes in a format and a tz, both of which are optional. Default format is yyyy-MM-dd HH:mm:ss. The tokens are:

nareshbhatia commented 11 years ago

@longlho, thanks so much for a quick response - this works like a charm!

A couple of follow up questions:

Thanks again.

longlho commented 11 years ago

For the 1st question: yes that's indeed how it works.

For the 2nd question: The API is the exact replicate of the normal JS Date object since it's supposed to be just a wrapper. The toString method (along with set and get timezone) are pretty much the only differences.

The API for tz parsing is in the README I believe.

nareshbhatia commented 11 years ago

Another curious question. In the constructor below, why do I have to supply the timezone in the second parameter even though it is included in the string? Seems limiting in case I need to read bunch of date strings with different time zones embedded in them.

new timezoneJS.Date('09 Mar 2008 01:00 CST', 'America/Chicago')