首页 > 代码库 > c#中时间戳和日期的相互装换
c#中时间戳和日期的相互装换
时间戳转换为日期:
/// <summary>
/// 将时间戳转换为datetime类型
/// </summary>
/// <param name="yn"></param>
/// <returns></returns>
public DateTime GetTime(int now)
{
string timeStamp = now.ToString();
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long ITime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(ITime);
DateTime dtResult = dtStart.Add(toNow);
return dtResult;
} //亲测有效
datetime转换为时间戳:
/// <summary>
/// datetime类型转换为时间戳
/// </summary>
/// <param name="?"></param>
/// <returns></returns>
public static double ConvertDateTimeToInt(DateTime dt)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (dt - startTime).TotalSeconds;
return intResult;
} //这个还没有试过