59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace StoreHub.KJDWebService.Commons
|
|
{
|
|
public class TimeUtils
|
|
{
|
|
/// <summary>
|
|
/// 时间戳转为C#格式时间
|
|
/// </summary>
|
|
/// <param name="timeStamp"></param>
|
|
/// <returns></returns>
|
|
public static DateTime StampToDateTime(int timeStamp)
|
|
{
|
|
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
|
long lTime = long.Parse(timeStamp + "0000000");
|
|
TimeSpan toNow = new TimeSpan(lTime);
|
|
return dateTimeStart.Add(toNow);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 时间戳转为C#格式时间
|
|
/// </summary>
|
|
/// <param name="timeStamp"></param>
|
|
/// <returns></returns>
|
|
public static DateTime StampMillisecondsToDateTime(long timeStamp)
|
|
{
|
|
DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
|
long lTime = long.Parse(timeStamp + "0000");
|
|
TimeSpan toNow = new TimeSpan(lTime);
|
|
return dateTimeStart.Add(toNow);
|
|
}
|
|
|
|
/// <summary>
|
|
/// C#格式时间转换为Unix时间戳(秒)
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
/// <returns></returns>
|
|
public static int DateTimeToTimestampSeconds(DateTime time)
|
|
{
|
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
|
return (int)(time - startTime).TotalSeconds;
|
|
}
|
|
|
|
/// <summary>
|
|
/// C#格式时间转换为Unix时间戳(毫秒)
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
/// <returns></returns>
|
|
public static long DateTimeToTimestampMilliseconds(DateTime time)
|
|
{
|
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
|
return (long)(time - startTime).TotalMilliseconds;
|
|
}
|
|
}
|
|
}
|
|
|