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
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 34

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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

626 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