日期:2014-05-18  浏览次数:20443 次

DateTime时间转换

时间转换
public long MilliTimeStamp(DateTime TheDate)
  {
  DateTime d1 = new DateTime(1970, 1, 1);
  DateTime d2 = TheDate.ToUniversalTime();
  TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
  return (long)ts.TotalMilliseconds;
  }

long time = MilliTimeStamp(DateTime.Now);
这样可得到一个13位的数字,返回 1970 年 1 月 1 日至今的毫秒数。

如何逆向回来,如果从JS客户端获取到 new Date().getTime(),然后在C#服务器中怎么解析返回一个DateTime对象。包含时区,网上看了一些都有错误。例如1335258540000,怎么得到DateTime

------解决方案--------------------
C# code
            long ss = 1335258540000;
            DateTime dt2 = new DateTime(1970,1,1);
            dt2 = dt2.AddMilliseconds(ss);

            Console.Write(dt2.ToString());

------解决方案--------------------
new DateTime().Ticks
------解决方案--------------------
DateTime dt = new DateTime(1970,1,1);
dt = dt.AddMilliseconds(1225112546);