2025-03-28 09:49:56 +08:00

42 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataUpdate
{
/// <summary>
/// 时间戳转换
/// </summary>
class TimeHelper
{
/// <summary>
/// 将DateTime类型转换为long类型
/// </summary>
/// <param name="dt">时间</param>
/// <returns></returns>
public static long ConvertDataTimeLong(DateTime dt)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan toNow = dt.Subtract(dtStart);
long timeStamp = toNow.Ticks;
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 7));
return timeStamp;
}
/// <summary>
/// 将Long类型转换为DateTime类型
/// </summary>
/// <param name="d">long</param>
/// <returns></returns>s
public static DateTime ConvertLongDateTime(long d)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(d + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = dtStart.Add(toNow);
return dtResult;
}
}
}