赞
踩
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public string GetTimeStamp()
{
//DateTime.Now获取的是电脑上的当前时间
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();//精确到秒
}
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetUtcNowTimeStamp()
{
//DateTime.UtcNow获取的是世界标准时区的当前时间(比北京时间少8小时)
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString();//精确到毫秒
}
/// <summary> /// 时间戳转换为DataTime /// </summary> /// <param name="unixTimeStamp"></param> /// <returns></returns> public DateTime TimestampToDataTime(long unixTimeStamp) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当 地时区 DateTime dt = startTime.AddSeconds(unixTimeStamp); System.Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss:ffff")); return dt; } /// <summary> /// DataTime转时间戳 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public long DataTimeToTimestamp(DateTime dateTime) { //new System.DateTime(1970, 1, 1) System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(dateTime); // 当地时区 long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数 System.Console.WriteLine(timeStamp); return timeStamp; }
/// <summary> /// 计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值...... /// /// DateTime1 第一个日期和时间 /// DateTime2 第二个日期和时间 /// </summary> /// <param name="DateTime1"></param> /// <param name="DateTime2"></param> /// <returns></returns> private string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; try { TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒"; Debug.Log("时间相差:" + dateDiff); } catch { print("计算失败!"); } return dateDiff; } /// /// 已重载.计算一个时间与当前本地日期和时间的时间间隔,返回的是时间间隔的日期差的绝对值. /// /// 一个日期和时间 /// private string DateDiff(DateTime DateTime1) { print(this.DateDiff(DateTime1, DateTime.Now)); return this.DateDiff(DateTime1, DateTime.Now); }
/// <summary>
/// 当前时间是否超过目标日期
/// </summary>
/// <returns></returns>
public static bool IsProcessTimeOut(string TragetTime = "20230227010000")
{
DateTime dt = DateTime.ParseExact(TragetTime, "yyyyMMddhhmmss", System.Globalization.CultureInfo.CurrentCulture);
if (DateTime.Now > dt)
{
//print("超时了");
return true;
}
//print("未超时");
return false;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。