In the afternoon, we encountered a problem in converting the date formats of MVC and EXTJS. The result of serializing a DateTime object from the .NET server side is a string format, such as '/Date(1335258540000)/' .
The integer 1335258540000 is actually a millisecond interval between January 1, 1970 and this DateTime. Through javascript, use the eval function to convert this date string into a Date object with a time zone, as follows
Use var date = eval('new ' + eval('/Date(1335258540000)/').source) to get a JS object
Check it clearly through alert(date).
Tue Apr 24 17:09:00 UTC+0800 2012
The above is a string automatically obtained by serializing the date of C# JSON. You can also write a function to get this number through C#, for example
public long MilliTimeStamp(DateTime TheDate) { DateTime d1 = new DateTime(1970, 1, 1); DateTime d2 = TheDate.ToUniversalTime(); TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks); return (long)ts.TotalMilliseconds; }The above function is the same as the integer in the string obtained using JSON serialization.
However, when returning to the server side from the client, there is a problem with the date. How to pass the Date object of javascript back to the server side?
First, get this integer through getTime() in javascript Date, and then parse the integer on the server and 'construct' it into a C# DateTime object. The idea is probably like this, but I encountered some trouble when I went back in the opposite direction.
public DateTime ConvertTime(long millionTime) { long timeTricks = new DateTime(1970, 1, 1).Ticks + millionTime * 10000 ; return new DateTime(timeTricks); }The results obtained through ConvertTime found that the time was 8 hours less, which happened to be the server's time zone, East Eighth Zone time, which means that 8 hours of nanoseconds have to be added, because the time stamp unit of C# is one ten millionth of a second, one hour is 3600 seconds, that is, 8*3600*10000000000
So the ConvertTime function was modified, the correct one is as follows:
public DateTime ConvertTime(long millionTime) { long timeTricks = new DateTime(1970, 1, 1).Ticks + millionTime * 10000 + TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours * 3600 * (long)100000000; return new DateTime(timeTricks); }The above simple example of the conversion of time and date formats of js and C# is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.