C#時間戳轉日期 互轉| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |
| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 15742635| C#時間戳轉日期 互轉| 2021-12-28T19:55:00| | BlogPost|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PubsLibrary { /// <summary> /// C#时间格式转换为时间戳(互转) /// 时间戳定义为从格林威治时间 1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。 /// </summary> public class DateTime_TimeStamp { protected int m_timestamp; /// <summary> /// 获取时间戳Timestamp /// </summary> /// <param name="dt"></param> /// <returns></returns> public static int GetTimeStamp(DateTime dt) { DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0); int timeStamp = Convert.ToInt32((dt - dateStart).TotalSeconds); return timeStamp; } /// <summary> /// 时间戳Timestamp转换成日期 /// </summary> /// <param name="timeStamp"></param> /// <returns></returns> public static DateTime GetDateTime(int timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = ((long)timeStamp * 10000000); TimeSpan toNow = new TimeSpan(lTime); DateTime targetDt = dtStart.Add(toNow); return targetDt; } /// <summary> /// 时间戳Timestamp转换成日期 /// </summary> /// <param name="timeStamp"></param> /// <returns></returns> public static DateTime GetDateTime(string timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = long.Parse(timeStamp + "0000000"); TimeSpan toNow = new TimeSpan(lTime); DateTime targetDt = dtStart.Add(toNow); return dtStart.Add(toNow); } } }
| 648658| | 2021-12-28T19:55:00| false| | 2021-12-28T19:55:04.603| true| using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PubsLibrary { /// <summa| Anonymous| 标签:00,C#,timeStamp,System,DateTime,戳轉,-------------,using,互轉 From: https://www.cnblogs.com/ralphlauren/p/18621188