本工具中的时间皆为秒,如果时间为毫秒,需要先转化为秒,再进行使用
using System;
public static class Tools_TimeFormat
{
/// <summary>
/// 服务器发过来的时间进行格式转化
/// </summary>
public static string ToTimeFormatString(this long seconds)
{
//long timeLong = (System.DateTime.Now.Ticks - System.DateTime.UtcNow.Ticks) / 10000;
//DateTime time = new DateTime(1970, 1, 1);
//time = time.AddSeconds(seconds);
//time = time.AddMilliseconds(timeLong);
string ret = SecondsToDateTime(seconds).ToString("yyyy/MM/dd @ HH:mm");
return ret;
}
/// <summary>
/// UTC距1970年秒数 - 转成本地时间
/// </summary>
/// <param name="seconds"></param>
/// <returns></returns>
private static DateTime SecondsToDateTime(long seconds)
{
DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(seconds).ToLocalTime();
if (time.Year < 1970 || time.Year > 2030)
{
time = new DateTime(2000, time.Month, time.Day, time.Hour, time.Minute, time.Second);
}
return time;
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余3页未读,立即下载