Solved

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

Posted on 2014-07-28
5
386 Views
Last Modified: 2014-08-11
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
0
Comment
Question by:bob_mechler
  • 3
5 Comments
 
LVL 32

Accepted Solution

by:
sarabande earned 250 total points
ID: 40226085
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
 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 250 total points
ID: 40228383
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
 

Author Comment

by:bob_mechler
ID: 40254527
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
 

Author Comment

by:bob_mechler
ID: 40254533
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
 

Author Closing Comment

by:bob_mechler
ID: 40254540
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now