Is Javascript DATE and C # date the best solution?

I get the date from the C # server side using the following code:

DateTime d1 = new DateTime(1970, 1, 1); DateTime d2 = (DateTime)c.ccdTimestamp2; long x = new TimeSpan(d2.Ticks - d1.Ticks).TotalMilliseconds; 

When I get my code from javascript side:

 function (timestamp) { alert("testing :" + new Date(timestamp)) } 

This gives me a fully formatted date, but it does not bring the time of my time zone, since if it is 17.15, it gives me 19.15 GMT +2!

At first, I just tried to pass my C # timestamp without the code above and found this question: How do I format a Microsoft JSON date? But I do not know what JSON is, and I could not understand what I could do! Is it easier to use JSON? If so, can anyone help me? Many thanks


Edit: solution - I did not use universal time on the server side. I left the server side code as is. All I have done is:

 new Date(timestamp).toUTCString() 
+7
source share
3 answers

What you need to do:

  • Always use UTC time on the server
  • Send UTC time to the browser as time units, as now.
  • Convert timestamp to local time in browser

Timestamp Used: 2012-04-11T15:46:29+00:00 :

 var d = new Date ( 1334159189000 ); // gives you back 2012-04-11T15:46:29+00:00 in a slightly different format, but the timezone info matches UTC/GMT+0 d.toUTCString(); // gives you back your local time d.toLocaleString(); 

Just created jsfiddle to show that it does what it should:
http://jsfiddle.net/t8hNs/1/

+10
source

use

 var currentDate = new Date(); //get off set from your browser var offset = Date.getTimezoneOffset(); 
+4
source

you can use javascriptizer

 string json = new JavaScriptSerializer().Serialize(DateTime.Now); 
+3
source

All Articles