查看: 422|回复: 0
打印 上一主题 下一主题

[其他] C#中关于时间戳的一些方法

[复制链接]
may    

8830

主题

80

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52304
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-6-28 23:39:55 |只看该作者 |倒序浏览
  1.     /// <summary>
  2.     /// 获取当前时间戳
  3.     /// </summary>
  4.     /// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳.</param>
  5.     /// <returns></returns>
  6.     public static long GetTimeStamp(bool bflag = true)
  7.     {
  8.         TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  9.         long ret;
  10.         if (bflag)
  11.             ret = Convert.ToInt64(ts.TotalSeconds);
  12.         else
  13.             ret = Convert.ToInt64(ts.TotalMilliseconds);
  14.         return ret;
  15.     }

  16.     static DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  17.     public static string NormalizeTimpstamp0(long timpStamp)
  18.     {
  19.         long unixTime = timpStamp * 10000000L;
  20.         TimeSpan toNow = new TimeSpan(unixTime);
  21.         DateTime dt = dtStart.Add(toNow);
  22.         return dt.ToString("yyyy-MM-dd");
  23.     }


  24.     /// <summary>
  25.     /// 时钟式倒计时
  26.     /// </summary>
  27.     /// <param name="second"></param>
  28.     /// <returns></returns>
  29.     public string GetSecondString(int second)
  30.     {
  31.         return string.Format("{0:D2}", second / 3600) + string.Format("{0:D2}", second % 3600 / 60) + ":" + string.Format("{0:D2}", second % 60);
  32.     }

  33.     /// 将Unix时间戳转换为DateTime类型时间
  34.     /// </summary>
  35.     /// <param name="d">double 型数字</param>
  36.     /// <returns>DateTime</returns>
  37.     public static System.DateTime ConvertIntDateTime(double d)
  38.     {
  39.         System.DateTime time = System.DateTime.MinValue;
  40.         System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0));
  41.         Debug.Log(startTime);
  42.         time = startTime.AddSeconds(d);
  43.         return time;
  44.     }

  45.     /// <summary>
  46.     /// 将c# DateTime时间格式转换为Unix时间戳格式
  47.     /// </summary>
  48.     /// <param name="time">时间</param>
  49.     /// <returns>double</returns>
  50.     public static double ConvertDateTimeInt(System.DateTime time)
  51.     {
  52.         double intResult = 0;
  53.         System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  54.         intResult = (time - startTime).TotalSeconds;
  55.         return intResult;
  56.     }


  57.     /// <summary>
  58.     /// 日期转换成unix时间戳
  59.     /// </summary>
  60.     /// <param name="dateTime"></param>
  61.     /// <returns></returns>
  62.     public static long DateTimeToUnixTimestamp(DateTime dateTime)
  63.     {
  64.         var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind);
  65.         return Convert.ToInt64((dateTime - start).TotalSeconds);
  66.     }

  67.     /// <summary>
  68.     /// unix时间戳转换成日期
  69.     /// </summary>
  70.     /// <param name="unixTimeStamp">时间戳(秒)</param>
  71.     /// <returns></returns>
  72.     public static DateTime UnixTimestampToDateTime(DateTime target, long timestamp)
  73.     {
  74.         DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind);
  75.         return start.AddSeconds(timestamp);
  76.     }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-5 02:18 , Processed in 0.129898 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部