Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone! Does anyone know a way to get the client machine time zone and then pass it to the server. I have found one script but it doesn't seem to work.
It both a javascritp and C# Combo exactly what I am looking for, If it worked. Attached is the script maybe someone can figure it out what I;m missing.

Thanks

XML
<script language="javascript">
function checkClientTimeZone()
{
    // Set the client time zone
    var dt = new Date();
    SetCookieCrumb(&quot;ClientDateTime&quot;, dt.toString());
    var tz = -dt.getTimezoneOffset();
    SetCookieCrumb(&quot;ClientTimeZone&quot;, tz.toString());
    // Expire in one year
    dt.setYear(dt.getYear() + 1);
    SetCookieCrumb(&quot;expires&quot;, dt.toUTCString());
}
// Attach to the document onload event
checkClientTimeZone();
</script>


And then Server side
public static int GetTimeZoneOffset(HttpRequest Request)
        {
            TimeZone tz = TimeZone.CurrentTimeZone;
            TimeSpan ts = tz.GetUtcOffset(DateTime.Now);
            int result = (int)ts.TotalMinutes;
            HttpCookie cookie = Request.Cookies["ClientTimeZone"];
            if (cookie != null)
                Int32.TryParse(cookie.Value, out result);
            return result;
        }
Posted
Updated 3-Sep-10 13:22pm
v2
Comments
raju melveetilpurayil 3-Sep-10 19:22pm    
[edit] edited for more readability [/edit]

 
Share this answer
 
Comments
Robert Adamo 3-Sep-10 19:46pm    
I check out the link and it seem to have a whole new issue of problem that is out of my league But Thanks anyway
I'll keep looking.
Hi,
just fill the client time zone in a hidden field.
it is the easiest way.
something like this:
Note:you can use now.toUTCString() or any thing else. but to be honest I have not used "toUTCString" before!:confused:
C#
<script>
function GetDate()
{
    var now = new Date();
    var monthnumber = now.getMonth();
    var monthday    = now.getDate();
    var year        = now.getYear();
    monthnumber ++;
    var sMonth=monthnumber.toString();
    var sDay=monthday.toString();
    if (monthnumber<10) sMonth ="0" + sMonth;
    if (sDay<10) sDay ="0" +sDay;
    return year + "/" + sMonth + "/" + sDay;
}
document.getElementById("hdnTime").value=GetDate();
</script>
<input id="hdnTime" type="hidden" value="" />



you can also use xmlhttp to send the result to the server if you want not to refresh your page.
 
Share this answer
 
Comments
Robert Adamo 3-Sep-10 19:45pm    
Sorry very new to this xmlhttp. Can you give an example. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900