开发者

DateTime difference between server and client

everyone! I'm solving next problem with time in my ASP.NET MVC project:

Problem: need to calculate difference between client DateTime and server DateTime. I have javascript function, that do Ajax query to server (DateController) with timeStamp parameter = getNow(), code of which is below.

   getNow: function() {
        var date = new Date();
        return (date.getTime() + (date.getTimezoneOffset() * 60000));
    }

Next, on server side I have DateController, in which I need to calculate dateTime difference in milliseconds:

     ....
     var clientMs = long.Parse(Request.QueryString["t"]);
     var dt1970 = new DateTime(1970, 1, 1, 0, 0, 0);
     var msFrom1970 = (DateTime.Now - dt1970).TotalMilliseconds
            +(DateTime.UtcNow - DateTime.Now).TotalMilliseconds;
     var timeOffset = msFrom1970 -clientMs; 

     return new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Data = new { responseText = timeOffset.ToString() 
        };
     }

But timeOffset is calculated wrong. I t开发者_C百科ry to check this logic in small console app:

 class Program
{
    static void Main(string[] args)
    {
         var clientMs =  1304411645875; 
        //value  of clientMs I get from javaScript by using page
        //http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_gettime
        //with this script
         /*
         <HTML>
         <body>
           <script type="text/javascript">
           var d=new Date();
            document.write(d.getTime());
           </script>
          </body>
         </HTML>
         */



        var dt1970 = new DateTime(1970, 1, 1, 0, 0, 0);
        var msFrom1970 = (DateTime.Now - dt1970).TotalMilliseconds
            +(DateTime.UtcNow - DateTime.Now).TotalMilliseconds;
        Console.WriteLine( msFrom1970);
        Console.ReadLine();
    }
}

Difference is about hours. Can someone show me my mistake or problem is occurred by something else?


You seem to be struggling with converting a javascript date to a C# date.

[Update] getUTCMilliseconds() only returns the milliseconds part of a date [/Update]
To pass the date in milliseconds to the server, use this function:

getNow: function() {
    var date = new Date();
    return date.getUTCMilliseconds();
}

And to convert this value of UTC milliseconds to a DateTime in .NET:

// Convert UTC milliseconds to System.DateTime
DateTime dtClient = new DateTime((millisecondsClient * TimeSpan.TicksPerMillisecond) + 621355968000000000);

// Test if this conversion is correct:
TimeSpan offset = DateTime.UtcNow - dtClient;

What you seem to be missing is the conversion from milliseconds to ticks.

References:
http://www.w3schools.com/jsref/jsref_getUTCMilliseconds.asp
http://twit88.com/blog/2011/01/23/net-datetime-from-milliseconds/


Daylight savings time prevents function's getTimezoneOffset() return from being a constant even for a given locale.


Maybe DateTime.Now greater than DateTime.UtcNow and (DateTime.UtcNow - DateTime.Now).TotalMilliseconds returns 0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜