/// <summary>
/// 检测目标时间戳是否在今天
/// </summary>
/// <param name="targetTime"></param>
/// <returns></returns>
public static bool IsToday(int targetTime)
{
bool relt = false;
if (targetTime >= GetTodayStarttimeStamp() && targetTime <= GetTodayEndtimeStamp())
{
relt = true;
//Console.WriteLine("目标时间戳是在今天");
}
else
{
relt = false;
//Console.WriteLine("目标时间戳不是在今天");
}
return relt;
}
/// <summary>
/// 获取今天的开始时间戳
/// </summary>
/// <returns></returns>
public static int GetTodayStarttimeStamp()
{
DateTime start = DateTime.Today;
return ConvertDateTimeInt(start);
}
/// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time"> DateTime时间格式</param>
/// <returns>Unix时间戳格式</returns>
public static int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
/// <summary>
/// 获取今天的结束时间戳
/// </summary>
/// <returns></returns>
public static int GetTodayEndtimeStamp()
{
DateTime End = DateTime.Today.AddDays(86399F / 86400);
return ConvertDateTimeInt(End);
}
/// <summary>
/// 获取当前时间戳
/// </summary>
/// <returns></returns>
public static int GetCurrentTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt32(ts.TotalSeconds);
}
/// <summary>
/// 13位时间戳转为DateTime
/// </summary>
/// <param name="timeStamp">时间戳</param>
/// <returns></returns>
public static DateTime Convert13bitTimeStampToTime(string timeStamp)
{
DateTime time = DateTime.Now;
if (string.IsNullOrEmpty(timeStamp))
{
return time;
}
try
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000");
TimeSpan toNow = new TimeSpan(lTime);
time = dtStart.Add(toNow);
}
catch (Exception ex)
{
Deb.LogError(" Convert13bitTimeStampToTime Exception:"+ex.Message);
}
return time;
}
/// 时间戳转为C#格式时间
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
private static DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
}
/// <summary>
/// 获取两个时间之间的时间差
/// </summary>
/// <returns></returns>
public static string GetTimeDiff(DateTime lastTime,DateTime pastTime)
{
TimeSpan t = lastTime - pastTime;
Deb.LogError("天:" + t.TotalDays.ToString());
Deb.LogError("时:" + t.TotalHours.ToString());
Deb.LogError("分:" + t.TotalMinutes.ToString());
Deb.LogError("秒:" + t.TotalSeconds.ToString());
double day = t.TotalDays;
double hours = t.TotalHours;
double min = t.TotalMinutes;
double sec = t.TotalSeconds;
string str = string.Empty;
if (day >= 30)
{
str = pastTime.ToString("yyyy-MM-dd hh:mm:ss");
}
else if (day >= 2 && day < 30)
{
str = ((int)day) + " 天前";
}
else if (day >= 1 && day < 2)
{
str = "昨天";
}
if(string.IsNullOrEmpty(str)&&(hours>0&&hours<=24))
{
str = ((int)hours)+" 小时前";
}
if(string.IsNullOrEmpty(str) && (min > 0 && min <= 60))
{
str = ((int)min)+" 分钟前";
}
if (string.IsNullOrEmpty(str) && (sec > 0 && sec <= 60))
{
str = ((int)min) + " 秒前";
}
return str;
}
/// <summary>
/// 获取当前时间与指定时间戳之间的时间间隔
/// </summary>
/// <param name="targetTimespan"></param>
/// <returns></returns>
public static string GetTimeDiffByToday(string targetTimespan)
{
Deb.LogError(targetTimespan);
DateTime tarD = Convert13bitTimeStampToTime(targetTimespan);
DateTime today = GetTime(GetCurrentTimeStamp().ToString());
return GetTimeDiff(today, tarD);
}