加入收藏 | 设为首页 | 会员中心 | 我要投稿 我爱制作网_池州站长网 (https://www.0566zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

深入Unix时间戳与C# DateTime时间类型互换的详解

发布时间:2022-12-12 11:01:40 所属栏目:Unix 来源:
导读:  ConvertIntDateTime方法的基本思路是通过获取本地时区表示Unixk开始时间,加上Unix时间值(即过去的秒数).

  ConvertDateTimeInt方法的基本思路是通过刻度数差,再把刻度数转换为秒数,当然要说明的是uni
  ConvertIntDateTime方法的基本思路是通过获取本地时区表示Unixk开始时间,加上Unix时间值(即过去的秒数).
 
  ConvertDateTimeInt方法的基本思路是通过刻度数差,再把刻度数转换为秒数,当然要说明的是unix时间戳,我这里返回的是double类型,意义上并非是真正的Unix时间戳格式。
 
  要获取真正Unix时间戳的,只获取整数部分就可以了。
 
  复制代码 代码如下:
 
  dangranusing System;
 
  using System.Collections.Generic;
 
  using System.Text;
 
  namespace WWFramework.DateTimes
 
  {
 
  ///
 
  /// 时间相关函数
 
  ///
 
  public static class Function
 
  {
 
  ///
 
  /// 将Unix时间戳转换为DateTime类型时间
 
  ///
 
  ///
 
  double 型数字
 
  /// DateTime
 
  public static System.DateTime ConvertIntDateTime(double d)
 
  {
 
  System.DateTime time = System.DateTime.MinValue;
 
  System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
 
  time = startTime.AddSeconds(d);
 
  return time;
 
  }
 
  ///
 
  /// 将c# DateTime时间格式转换为Unix时间戳格式
 
  ///
 
  ///
 
  时间
 
  /// double
 
  public static double ConvertDateTimeInt(System.DateTime time)
 
  {
 
  double intResult = 0;
 
  System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
 
  intResult = (time - startTime).TotalSeconds;
 
  return intResult;
 
  }
 
  }
 
  }
 

(编辑:我爱制作网_池州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章