using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PubsLibrary
{
///
/// C#时间格式转换为时间戳(互转)
/// 时间戳定义为从格林威治时间 1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
///
public class DateTime_TimeStamp
{
protected int m_timestamp;
///
/// 获取时间戳Timestamp
///
///
///
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;
}
///
/// 时间戳Timestamp转换成日期
///
///
///
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;
}
///
/// 时间戳Timestamp转换成日期
///
///
///
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);
}
}
}