// DateTime --> long
public static long ConvertDateTimeToLong(DateTime dt)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan toNow = dt.Subtract(dtStart);
long timeStamp = toNow.Ticks;
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
return timeStamp;
}
// long --> DateTime
public static DateTime ConvertLongToDateTime(long d)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(d + "0000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = dtStart.Add(toNow);
return dtResult;
}
测试 long –> DateTime:
string time = "1490782464000";
DateTime dt = TimeUtils.ConvertLongToDateTime(long.Parse(time ));
System.Console.WriteLine(dt.ToString());
输出:
2017/3/29 星期三 下午 6:14:24