tags:

views:

37

answers:

2

As follow up of the below link.

http://stackoverflow.com/questions/3215508/how-to-format-this-date-type-2010-06-24t000000z-to-sun-24-06-10-7-15-p-m-cdt

I'm converting the utc date format to simple date format. 2010-06-24T00:00:00Z to sun,24/06/10 7.15 p.m (CDT) (converted time)

But is there a way to identify the valid timezone, as the above conversion always detects the time zone based on client m/c. Or is it that we can't detect the valid time zone from UTC??

Appreciate the quick response.

A: 

UTC has no time zone, it is "universal" because it is always the same no matter where you are. Time zone information has to be determined by the observer.

Arnold Spence
A: 

You didn't define what you mean by "valid timezone" -- do you mean local timezone? (UTC is a perfectly valid timezone, after all--or at least it is if you're in the Western European Time Zone and it's not British Summer Time…)

If you have a UTC date and you want to turn it into a date in the local timezone, that's straightforward:

function UTCtoLocal(inDate) {
    // inDate can be null, a string or an object
    // if it's an object, it can be a date or event

    var localDate = new Date();
    var utcDate = new Date().toUTCString();

    if (inDate) {
        if (typeof inDate == "string") {
            utcDate = inDate;
        }
        else { 
            if (inDate.getDay) { // is it really a date?
                utcDate = inDate.toString();
            }
        }
    }

    utcDate = utcDate.substr(0, utcDate.length-3);
    localDate.setTime(Date.parse(utcDate));
    return localDate;
}
Dori