0

I have a date at client side, I am converting that date to millisecond there and passing the milliseconds to server side code and the converting that to again in date format, but the issue is in this process my date changes. Below is my scenario

JavaScript Date:

    var myDate= Fri Apr 01 2011 05:00:00 GMT+0530 (India Standard Time) {}
    //Converted to milliseconds via this code (new Date(myDate)).getTime()
    Output: 1301700600000

Now I am passing the above string(1301700600000) to my server side code via ajax. Below is the server side code.

private void Test(string myDate)
{
    long myDateMilliseconds=long.Parse(myDate);
    var myDate = new DateTime(1970, 1, 1) + new TimeSpan(myDateMilliseconds*10000);
    //Here the date becomes Date = {3/31/2011 12:00:00 AM}
}

i.e Fri Apr 01 2011 05:00:00 GMT+0530 is not equal to {3/31/2011 12:00:00 AM} Note the date and time difference.

May I know how there is difference coming between the dates which I passed and the date which I have produced at the server.

NotMe
  • 87,343
  • 27
  • 171
  • 245
Rocky Singh
  • 15,128
  • 29
  • 99
  • 146
  • You should never depend on the date that the browser/client gives. At best, it is unreliable; at worst..? it can be manipulated at will. – JustinStolle Apr 01 '11 at 03:57
  • See http://stackoverflow.com/questions/249760 for general info on Unix-type timestamps. All you might be missing is conversion to local time. Consider using `.UtcNow` for the conversion and adjust that baseline – skarmats Apr 01 '11 at 04:03
  • @JustinStolle I want date from the client that's why I am going with milliseconds else I will face the format issue. – Rocky Singh Apr 01 '11 at 04:28
  • See my answer for a follow-up to my comment. Just edited it to fix a mistake – skarmats Apr 01 '11 at 05:01

3 Answers3

1

How to create TimeSapn from milliseconds? Call TimeSpan.FromMilliseconds http://msdn.microsoft.com/en-us/library/system.timespan.frommilliseconds.aspx

Note: consider using UTC versions of functions for getting Date and Time values. And read about time zones...

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

Looks like you are losing the precession there. Why not simply pass the Date string and Parse it on server.

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • Umm just to be clear i tried your code like this `long myDateMilliseconds=long.Parse("1301700600000"); var myDate = new DateTime(1970, 1, 1) + new TimeSpan(myDateMilliseconds*10000); myDate.Dump(); ` and i got correct date `01-Apr-11 11:30:00 PM ` – Shekhar_Pro Apr 01 '11 at 04:05
  • Nothing just a small extension function to Print out whatever is there in the object.. in this case `myDate` ...| I use [LINQPAD](http://linqpad.net/) to check small C# snippets and `Dump` is available in there. .. – Shekhar_Pro Apr 02 '11 at 06:08
0

Try using

var d = new Date();
var utcMs = d.UTC();

on the client-side. Then use

var utcThen = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(clientUtcMs);

var localThen = utcThen.ToLocalTime();

A better solution would be to compute the difference between UTC time and the local time at the client and send that to the server. Round it to half hours (is there a smaller timespan between time zones?) and you'll have a pretty precise info.

skarmats
  • 1,907
  • 15
  • 18
  • @Rocky Singh: There was a bug in my post. Should be `.UTC()` not `.getUTCMilliseconds()`. Fixed that – skarmats Apr 01 '11 at 05:00