Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
440 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 35

Accepted Solution

by:
sarabande earned 1000 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 1000 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

721 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