[JS] JS and C # time date format conversion

Source: Internet
Author: User

The afternoon in the MVC and ExtJS date format Mutual conversion encountered problems we took from. NET Server-side serialization The result of a DateTime object is a string format such as '/date (1335258540000)/' strings.

The integer 1335258540000 is actually a number of milliseconds between January 1, 1970 00:00:00 to this datetime interval. Using the Eval function with JavaScript, you can convert this date string to a time-zone date object, as follows

Use var date = eval (' new ' + eval ('/date (1335258540000)/'). Source) So you can get a JS object

See more clearly through alert (date).

Tue Apr 17:09:00 utc+0800

Above is the C # JSON serialization date automatically obtained string, you can also write a function in C # to get this number, for example

?
1 2 3 4 5 6 7 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 by using JSON serialization.

However, when returning from the client to the server side, the date is experiencing problems, how to pass the JavaScript date object back to the server side?

This integer is obtained by gettime () in JavaScript date, and the server side parses the integer and ' constructs ' it into a C # DateTime object. The idea is probably like this, but in the reverse back when it was a bit of trouble.

 Public DateTime ConvertTime (long  millitime)        {            longnew DateTime (1970 1 1 10000  ;             return New DateTime (timetricks);        }

The results obtained by ConvertTime found that the time is less than 8 hours, which is exactly the time zone of the server, East Eight, that is, plus 8 hours of nanosecond, because C # timestamp unit is one out of 10,000 seconds, one hours 3,600 seconds, that is, 8*3600*10000000

So modify the ConvertTime function, correct as follows:

1  PublicDateTime ConvertTime (Longmillitime)2         {3             LongTimetricks =NewDateTime (1970,1,1). Ticks + millitime *10000+ TimeZone.CurrentTimeZone.GetUtcOffset (datetime.now). Hours *3600* (Long)10000000;4             return NewDateTime (timetricks);5}

[JS] JS and C # time date format conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.