C # DateTime and Unix timestamp type swaps

Source: Internet
Author: User
Tags datetime string format

There was a textbox1 and TextBox2 and a button
Require TextBox1 to enter the number of milliseconds after. Press Button TextBox2 Show time
such as textbox1 input 1248671343262 Press button after TextBox2 display similar to Mon June 13:09:03 CST 2009 such time

The number of milliseconds you call 1248671343262 is the Windows file time (a 64-bit value) that converts a DateTime object through a method TOFILETIMEUTC or Tofiletime, which represents the self Coordinated Universal Time (UTC) A.D. (C.E.) 1601 The number of intervals that have elapsed since 12:00 midnight, January 1, at a distance of 100 nanoseconds. Windows uses file time to record when an application creates, accesses, or writes files.
First convert it backwards to a DateTime object:
DateTime dt = DATETIME.FROMFILETIMEUTC (1248671343262);
You can also convert first to long ticks (the scale is 100 nanoseconds),
Long ticks = 1248671343262/100;
DateTime dt = new DateTime (ticks);
Then convert by format:
Dt.getdatetimeformats (' R ') [0].tostring ();
Tue, 1601 10:41:07 GMT
Then look at the difference between GMT and CST and make some adjustments.

See below for Unix timestamps and C # datetime type swaps

<summary>
method in converting a UNIX timestamp to a regular
System.DateTime value (and also to)
</summary>
<param name= "timestamp" >value to be converted</param>
<returns>converted datetime in String format</returns>
private static DateTime Converttimestamp (double timestamp)
{
Create a new datetime value based on the Unix epoch
DateTime converted = new DateTime (1970, 1, 1, 0, 0, 0, 0);

Add the timestamp to the value
DateTime newdatetime = converted.addseconds (timestamp);

Return the value in string format
return Newdatetime.tolocaltime ();
}


<summary>
method in converting a System.DateTime value to a UNIX timestamp
</summary>
<param name= "value" >date to Convert</param>
<returns></returns>
Private double Converttotimestamp (datetime value)
{
Create timespan by subtracting the value provided from
The Unix Epoch
TimeSpan span = (value-new datetime (1970, 1, 1, 0, 0, 0, 0). ToLocalTime ());

Return to total seconds (which is a UNIX timestamp)
Return (double) span.totalseconds;
}

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.