/// <summary>
/// 时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static Int64 getTimeStamp(DateTime dateTime)
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds);
}
/// <summary>
/// 时间戳转本地时间
/// </summary>
/// <param name="unixTimeStamp"></param>
/// <returns></returns>
public static DateTime getLocalDateTime(double unixTimeStamp)
{

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); //当地时区
DateTime dtime = startTime.AddSeconds(unixTimeStamp);
return dtime;
}



/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public string GetTimeStamp()
{
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}

/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}


 

 

把数据库存的double类型时间戳转成日期时间 (下面这个功能是遍历DataTable,把原有的addTime(时间戳)转为现有add_time日期字符串)

 



DataColumn dc = null; //原来的数据上增加一列
dc = dt.Columns.Add("add_time", Type.GetType("System.String")); //日期

for (int i = 0; i < dt.Rows.Count; i++)
{
Console.WriteLine(dt.Rows[i]["addTime"].ToString());
double unixTimeStamp = 0;
if (dt.Rows[i]["addTime"].ToString().Trim() == null || dt.Rows[i]["addTime"].ToString().Trim() == "")
{
unixTimeStamp = 0;
}
else
{
unixTimeStamp = Convert.ToDouble(pe.dt.Rows[i]["addTime"].ToString());
}
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dtime = startTime.AddMilliseconds(unixTimeStamp);
//dt.Rows[i]["addTime"] = dtime;//不能覆盖原来值,报错类型不一致,所以要新建一个row
dt.Rows[i]["add_time"] = dtime.ToString("yyyy/MM/dd HH:mm:ss");


//dt.Rows.Add(dr);//如果新实例化 DataRow dr = dt.NewRow(); 一个行,需要加入这行

}