Get the App
SLTechnology News&Howtos  ›  Servers  › 

Time format conversion between unix and windows in C #

Shulou Source: shulou.com Published: 2022-06-03 00:08:17 10月25日 Update

Recently, we have done order docking on shopEX platform, because shopEX is Php, so the time format of submitting the order is first converted to unix time format, and then it can be submitted successfully.

Similarly, all the dates returned by response are in unix time format, so we can only use them if we convert them to system time.

After searching and sorting it out on the Internet, here are some small examples written in C # for your reference. The rigor of the data needs to be judged in the actual use.

Using System

Using System.Collections.Generic

Using System.Text

Using System.Data

Using System.Data.SqlClient

Namespace ConsoleApplication1

{

/ / /

/ date format interchange between unix and windowns

/ zhangyong 2012-03-07

/ / /

Class Program

{

Private static readonly string time = DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss")

Static void Main (string [] args)

{

String DataStr = time

Program p = new Program ()

String tempTime = p.ConvertToUnix (DataStr)

String ssd = p.ConvertToWin (tempTime)

}

/ / /

/ / convert the date format in nuix to normal date format, provided that the format passed in is correct

/ / /

/ / the timestamp passed in

/ / /

Public String ConvertToWin (String timestampString)

{

DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970)

Long lTime = long.Parse (timestampString + "0000000")

TimeSpan toNow = new TimeSpan (lTime)

DateTime dtResult = dtStart.Add (toNow)

Return dtResult.ToString ("yyyy-MM-dd HH:mm:ss")

}

/ / /

/ / convert normal dates to unix date-time stamp format

/ / /

/ / the normal date is converted to a string format such as: yyyy-MM-dd HH:mm:ss

/ unix time

Public string ConvertToUnix (string dateTime)

{

DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970, 1,1))

DateTime dtNow = DateTime.Parse (dateTime)

TimeSpan toNow = dtNow.Subtract (dtStart)

String timeStamp = toNow.Ticks.ToString ()

TimeStamp = timeStamp.Substring (0, timeStamp.Length-7)

Return timeSt}

}

}

Tags: Format time date order Craft rigor identical success rigor example premise character string actual platform data system reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology NVidia Linux Huawei