【转】UNIX时间戳与.net日期类的转换

1 将系统时间转换成UNIX时间戳
  DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
  DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
  TimeSpan toNow = dtNow.Subtract(dtStart);
  string timeStamp = toNow.Ticks.ToString();
  timeStamp = timeStamp.Substring(0,timeStamp.Length - 7);

2将UNIX时间戳转换成系统时间
  string timeStamp = "1144821796";
  DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
  long lTime = long.Parse(timeStamp + "0000000");
  TimeSpan toNow = new TimeSpan(lTime);
  DateTime dtResult = dtStart.Add(toNow);

 

 

//===================================

//DateTime轉變為UNIX時間,直接转换,不改变时区
DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0);
TimeSpan toNow = dt.Subtract(dtStart);
int timeStamp = (int)(toNow.Ticks/10000000);

//===================================

 
转自:http://blog.163.com/i_lcw/blog/static/87809734200926118577/

【转】UNIX时间戳与.net日期类的转换,布布扣,bubuko.com

【转】UNIX时间戳与.net日期类的转换

上一篇:前端开发:CSS Position 全解析


下一篇:spring boot学习(8) SpringBoot 之切面AOP