How to convert between C # datetime type and JavaScript date

Source: Internet
Author: User
Mono Chinese Forum address: http://www.sinoprise.cn/showtopic-242.aspx

In JavaScript, the time is recorded in milliseconds, and the start time is. This is UTC (GMT. In China, it should be, because China is in the GMT + 8 hours ). The C # Time is recorded in units of ticks. the literal translation is the CPU time "tick", which is a unit smaller than milliseconds. In the conversion between the two, pay attention to the conversion of the start time and the time zone.CodeHowever, to convert the time from C # To javascript:

Reference:

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;
}

Note:

Timespan Ts = new timespan (d2.ticks-d1.ticks );
Cannot be written:

Timespan Ts = d2-d1.ticks;

If the second method is used, the time passed to JavaScript will be added with 8 hours on the basis of the GMT +. My test time is and it will be after being printed in JavaScript.
In the first method, the DOTNET framework automatically converts the time zone.

I have time to study it in detail.

Related Article

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.