How do I convert a date and time from a known time zone abbreviation to the timezone of a Browser client's timezone

I know how to get the time zone of a webserver.  I just use C# in the code behind.

 I have a date time value stored in a database in the same time zone as the webserver and I have code in the program I'm working with to extract that date.

I also know how to get the time zone of the location of the web browser client using the (Central Daylight Time) portion of the new Date(); structure in JavaScript.

How do I get from the web server date and time of
July 28, 2014 01:30 PM CST to July 28, 2014 02:30 PM EST which represents the web user's Date, Time and Time Zone abbreviation.

Keep in mind the user can be anywhere.  The GMT -0400 for EDT and the GMT -0500 for CDT I see when displaying the date indicates I need to convert the Date Time portion by one hour.  

I probably need to concatenate the adjusted client browser time with the appropriate client browser Time Zone Abbreviation.

Is there a way to do this (Date math) without reverting to string manipulation until the last step when I add the Time Zone abbreviation?  

Bob
LVL 1
bob_mechlerProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
I have a date time value stored in a database in the same time zone as the webserver
normally times are stored in GMT and then can be shown correctly everywhere. you should check if that isn't the case already. if the local times are stored you should try to change that as it is not the standard way and very inconvenient. conversions are not very difficult but elaborate. the functions to call differ from language to language but it is always the following method: first set the current time zone to the time zone of the time value. then convert to GMT time. then set the new time zone and finally get the local time. only the last step would involve strings (beside the source format also was a string).

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b0lsc0ttIT ManagerCommented:
Javascript's Date object has a getTimezoneOffset method.  That is usually what I use when trying to work with times in different zones.  The abbreviation is much more complicated.  Also it may or may not reflect daylight savings time, which you should try to account for.

There is some basic info on the getTimezoneOffset method at http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp with a pretty simple example of usage.  If you need more specifics or help just let us know.  The suggestion above to use GMT during this process is a good one.  If your server code provides the GMT time to browser script then much easier to change it to the local time in Javascript.

bol
0
bob_mechlerProgrammerAuthor Commented:
Here is the javascript function I came up with after being pointed in the right direction with getTimezoneOffset etc.

        
function GetClientTZ() {
            var fm = ["January", "February", "March", "April", "May", "April", "June", "July",
           "August", "Septempber", "October", "November", "December"];
            var x = new Date();
            var c = x.toString();
            var tz = " ";
            if (c.indexOf("(") > 0) {
                var e = c.slice(c.indexOf("(") + 1, c.indexOf(")"));
                var f = e.split(' ');
                f[0] = f[0].slice(0, 1);
                f[1] = f[1].slice(0, 1);
                f[2] = f[2].slice(0, 1);
                tz = f[0] + f[1] + f[2];
            } else {
                var f = c.split(' ');
                tz = f[4];
            }
            var servtzos = "<%=resulttzserver%>";
            var clilogondate = "<%=logondate%>";
            var cm = x.getTimezoneOffset() * -1;
            var tzadj = cm - servtzos;
            var d = new Date(clilogondate);
            d.setMinutes(d.getMinutes() + tzadj); 
            var datetime = fm[d.getMonth() + 1] + " " + d.getDate() + ", " + d.getFullYear() + " at " +
               (d.getHours() > 12 ? d.getHours() - 12 : d.getHours()) + ":" + (d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes()) + " " +
               (d.getHours() > 12 ? "PM" : "AM") + " " +
               tz;
            document.getElementById("LastLogonLabel").innerHTML = datetime.toString();
        }
    </script>

Open in new window


I grabbed the Server timezoneoffset and the client timezoneoffset  and did a little math and formatting to get what I wanted.

Last sign-in: August 11, 2014 at 5:37 PM EDT

In page load of the code behind I had this code

           
            TimeZone tz = TimeZone.CurrentTimeZone;
            TimeSpan ts = tz.GetUtcOffset(DateTime.Now);
            resulttzserver = (int)ts.TotalMinutes;

Open in new window


In the client side javascript this portion got the minutes from GTM for the server's timezone

           
           var clilogondate = "<%=logondate%>";  
            var cm = x.getTimezoneOffset() * -1;
            var tzadj = cm - servtzos;
            var d = new Date(clilogondate);
            d.setMinutes(d.getMinutes() + tzadj); 

Open in new window


Bob
0
bob_mechlerProgrammerAuthor Commented:
The server date was not UTC so I had to get the offset of the server datetime and do the math with the client datetime.
0
bob_mechlerProgrammerAuthor Commented:
I'm looking more for actual examples of code based on my question. I guess I need to pose my questions more interestingly so I will get sample code that people must have already on hand.

Bob
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.