C#时间与时间戳转换

1、根据时间戳获取时间(10位/13位)

/// <summary> 
/// 根据时间戳获取时间(10位) 
/// </summary>  
public static DateTime TimeStampToDateTime(string timeStamp)
{
    Int64 begtime = Convert.ToInt64(timeStamp) * 10000000;
    DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);
    long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
    long time_tricks = tricks_1970 + begtime;//日志日期刻度
    DateTime dt = new DateTime(time_tricks);//转化为DateTime
    return dt;
}
/// <summary> 
/// 根据时间戳获取时间(13位) 
/// </summary>  
public static DateTime TimeLongStampToDateTime(string timeStamp)
{
    Int64 begtime = Convert.ToInt64(timeStamp) * 10000;
    DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);
    long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
    long time_tricks = tricks_1970 + begtime;//日志日期刻度
    DateTime dt = new DateTime(time_tricks);//转化为DateTime
    return dt;
}

2、根据时间获取时间戳(10位/13位)

 

 /// <summary>
 /// 获取时间戳
 /// </summary>
 /// <param name="time">时间格式字符串(yyyy-MM-dd HH:mm:ss)</param>
 /// <param name="type">0:10位,1:13位</param>
 /// <returns></returns>
 public static string Timestamps(string time, int type)
 {
     string stamps = string.Empty;
     if (!string.IsNullOrEmpty(time))
     {
         stamps = type == 0 ? ((Convert.ToDateTime(time).ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString() : ((Convert.ToDateTime(time).ToUniversalTime().Ticks - 621355968000000000) / 10000).ToString();
     }
     else
     {
         if (type == 0)
         {
             long lstamps = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;//10位时间戳生成方式
             stamps = lstamps.ToString();
         }
         else
         {
             long lstamps = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;//13位时间戳生成方式
             stamps = lstamps.ToString();
         }
     }
     return stamps;
 }

 3、获取时间戳/获取时间差

 

/// <summary>
/// 13位时间戳
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static string ToTimestamps(string time)
{
    var ts = Convert.ToDateTime(time).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return Convert.ToInt64(ts.TotalMilliseconds).ToString();
}
/// <summary>
/// 10位时间戳
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static string ToTimetalstamps(string time)
{
    var ts = Convert.ToDateTime(time).ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return Convert.ToInt64(ts.TotalSeconds).ToString();
}
/// <summary>
/// 时间戳转时间
/// </summary>
/// <param name="timestamp"></param>
/// <returns></returns>
public static DateTime GetDateTime(long timestamp)
{
    long begtime = timestamp * 10000000;
    DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0);
    long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
    long time_tricks = tricks_1970 + begtime;//日志日期刻度
    DateTime dt = new DateTime(time_tricks);//转化为DateTime
    return dt;
}
/// <summary>
/// 返回传入时间与当前时间相差的秒
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public static int GetTimeSecond(string time)
{
    DateTime now = DateTime.Now;
    TimeSpan ts = now.Subtract(Convert.ToDateTime(time));
    int sec = (int)ts.TotalSeconds;
    return sec;
}
/// <summary>
/// 返回两个时间相差的秒
/// </summary>
/// <param name="btime">开始时间</param>
/// <param name="etime">结束时间</param>
/// <returns></returns>
public static int GetDifferTimeSecond(string btime, string etime)
{
    DateTime now = Convert.ToDateTime(btime);
    TimeSpan ts = now.Subtract(Convert.ToDateTime(etime));
    int sec = (int)ts.TotalSeconds;
    return sec;
}

 

End...

 

posted @ 2024-11-04 13:52  小飛  阅读(168)  评论(0编辑  收藏  举报