
正文
C# 时间戳的相关操作
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
一般的 时间戳 格式分为两种 即 10位(秒)时间戳 与 13位(毫秒)时间戳
时间戳 类型也分为两种 即 本地时间戳 与 世界统一(UTC)时间戳
废话不多说,直接上代码:
一、时间戳获取方法
/// <summary>
/// 获取时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc)</param>
/// <param name="format">时间戳格式(只能为 10、13)</param>
/// <returns></returns>
private double GetTimestamp(int format, DateTimeKind timeKind)
{
TimeSpan timeSpan = new TimeSpan(); switch (timeKind)
{
case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(, , , , , , timeKind); break;
case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(, , , , , , timeKind); break;
default: throw new Exception("时间类型 只能为 Local、Utc");
} switch (format)
{
case : return timeSpan.TotalSeconds;
case : return timeSpan.TotalMilliseconds;
default: throw new Exception("时间戳格式 只能为 10、13");
}
} /// <summary>
/// 获取10位时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public int Get10Timestamp(DateTimeKind timeKind = DateTimeKind.Local)
{
return Convert.ToInt32(GetTimestamp(, timeKind));
} /// <summary>
/// 获取13位时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public long Get13Timestamp(DateTimeKind timeKind = DateTimeKind.Local)
{
return Convert.ToInt64(GetTimestamp(, timeKind));
}
二、时间戳验证方法
/// <summary>
/// 验证时间戳(10位、13位皆可)
/// </summary>
/// <param name="timestamp">时间戳</param>
/// <param name="timeDiff">允许时差(10位时单位为 秒,13位时单位为 毫秒)</param>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public bool ValidateTimestamp(double timestamp, int timeDiff, DateTimeKind timeKind = DateTimeKind.Local)
{
TimeSpan timeSpan = new TimeSpan(); switch (timeKind)
{
case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(, , , , , , timeKind); break;
case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(, , , , , , timeKind); break;
default: throw new Exception("时间类型 只能为 Local、Utc");
} double nowTimestamp = ; //现在的时间戳
int format = timestamp.ToString("f0").Length; switch (format)
{
case : nowTimestamp = timeSpan.TotalSeconds; break;
case : nowTimestamp = timeSpan.TotalMilliseconds; break;
default: throw new Exception("时间戳格式 错误");
} double nowTimeDiff = nowTimestamp - timestamp; //现在的时差 if (-timeDiff <= nowTimeDiff && nowTimeDiff <= timeDiff)
return true;
else
return false;
}
三、由 时间戳 转换为 DateTime 方法
/// <summary>
/// 将时间戳装换为DateTime(10位、13位皆可)
/// </summary>
/// <param name="timestamp">时间戳</param>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <param name="toTimeKind">返回的时间类型(只能为 Local、Utc,默认与 timeKind 一致)</param>
/// <returns></returns>
public DateTime TimestampToDateTime(double timestamp, DateTimeKind timeKind = DateTimeKind.Local, DateTimeKind toTimeKind = DateTimeKind.Unspecified)
{
DateTime startTime;
toTimeKind = timeKind; switch (timeKind)
{
case DateTimeKind.Utc: startTime = new DateTime(, , , , , , timeKind); break;
case DateTimeKind.Local: startTime = new DateTime(, , , , , , timeKind); break;
default: throw new Exception("时间类型 只能为 Local、Utc");
} DateTime newTime;
int format = timestamp.ToString("f0").Length; switch (format)
{
case : newTime = startTime.AddSeconds(timestamp); break;
case : newTime = startTime.AddMilliseconds(timestamp); break;
default: throw new Exception("时间戳格式 错误");
} if (newTime.Kind != toTimeKind)
newTime = toTimeKind == DateTimeKind.Local ? newTime.ToLocalTime() : newTime.ToUniversalTime(); return newTime;
}





