Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Time format conversion between unix and windows in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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}

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report