C # get UTC time, and UTC datetime convert each other

Source: Internet
Author: User

Mostly from http://blog.sina.com.cn/s/blog_4c6e822d0102dsdz.html.

Just start learning a little C # knowledge and study the UTC time acquisition as follows

DateTime dt = Datetime.utcnow;

Other:

The DateTime.UtcNow.ToString () output is a 0 time zone event,

DateTime.Now.ToString () output is the time of the current time zone, we in China use the East eight district time, so the difference 8 hours

Here is the conversion of class to each other

class UTC

{

public static int Convertdatetimeint (System.DateTime time)

{

Double intresult = 0;

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1));

Intresult = (time-starttime). TotalSeconds;

return (int) intresult;

}

public static DateTime Convertintdatetime (double UTC)

{

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1));

StartTime = Starttime.addseconds (UTC);

StartTime = starttime.addhours (8);//Convert to Beijing time (Beijing time =UTC time + 8 hours)

return startTime;

}

Calling methods

DateTime UtcNow = DateTime.Now.ToUniversalTime ();

Double utc= Convertdatetimeint (UtcNow);

DateTime dtime = convertintdatetime (UTC);

}

C # get UTC time, and UTC datetime convert each other

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.