Original: C # get UTC time, and UTC datetime convert each other
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