using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlexJobApi.Core
{
///
/// 时间工具
///
public static class DateTimeUtils
{
///
/// 转时间
///
/// 对象
/// 时间
public static DateTime? ToDateTime(this object obj)
{
if (obj != null && DateTime.TryParse(obj.ToString(), out var date) && date != DateTime.MinValue && date != DateTimeOffset.MinValue)
{
return date;
}
return null;
}
///
/// 转时间
///
/// 字典
/// 键
/// 值
public static DateTime? ToDateTime(this IDictionary dic, TKey key)
{
if (dic != null && dic.ContainsKey(key) && dic[key] != null && DateTime.TryParse(dic[key]?.ToString(), out var date) && date != DateTime.MinValue) return date;
return null;
}
///
/// 时间转时间戳
///
/// 时间
/// true毫秒级 false秒级
/// 时间戳
public static long ToTimeStamp(this DateTime time, bool milliseconds = true)
{
var timespan = time.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0);
return milliseconds ? (long)timespan.TotalMilliseconds : timespan.Seconds;
}
///
/// 时间戳转时间
///
/// 时间戳
/// true毫秒级 false秒级
/// 时间
public static DateTime ToDateTimeByMilliseconds(this long timestamp, bool milliseconds = true)
{
var time = new DateTime(1970, 1, 1, 0, 0, 0);
time = milliseconds ? time.AddMilliseconds(timestamp) : time.AddSeconds(timestamp);
return time.ToLocalTime();
}
///
/// 截取日期
///
/// 时间
/// 日期
public static DateTime? GetYYYYMMDD(this DateTime? date)
{
return date.HasValue ? date.Value.GetYYYYMMDD() : null;
}
///
/// 截取日期
///
/// 时间
/// 日期
public static DateTime GetYYYYMMDD(this DateTime date)
{
return new DateTime(date.Year, date.Month, date.Day);
}
///
/// 获取年龄
///
///
///
public static int? GetAge(this DateTime? birthday)
{
if (birthday == null) return null;
var age = DateTime.Now.Year - birthday.Value.Year;
if (birthday.Value.Date > DateTime.Today.AddYears(-age))
{
age--;
}
return age;
}
///
/// 获取时间范围选择器
///
///
///
///
public static bool TryToDateTimeRange(this string str, out DateTimeRange range)
{
range = new DateTimeRange();
if (str.IsNotNull())
{
if (str.Contains(","))
{
var times = str.Split(',');
if (times.Length == 2
&& DateTime.TryParse(times[0], out var start) && start != DateTime.MinValue
&& DateTime.TryParse(times[1], out var end) && end != DateTime.MinValue
&& end >= start)
{
range.Start = start;
range.End = end;
}
}
else if (str.StartsWith("Relative"))
{
var splits = str.Split("_");
if (splits.Length == 4
&& int.TryParse(splits[2], out var quantity))
{
var type = splits[1];
var unit = splits[3];
if ((type == "Before" || type == "Current" || type == "After")
&& (unit == "Day" || unit == "Week" || unit == "Month"))
{
var now = DateTime.Now;
range.Start = new DateTime(now.Year, now.Month, now.Day);
if (type == "Before")
{
quantity = 0 - quantity;
}
switch (unit)
{
case "Day":
range.Start = range.Start.AddDays(quantity);
range.End = new DateTime(range.Start.Year, range.Start.Month, range.Start.Day, 23, 59, 59, 999);
break;
case "Week":
var week = (int)range.Start.DayOfWeek;
range.Start = range.Start.AddDays(0 - week);
range.Start = range.Start.AddDays(quantity * 7);
range.End = new DateTime(range.Start.Year, range.Start.Month, range.Start.Day, 23, 59, 59, 999).AddDays(6);
break;
case "Month":
range.Start = new DateTime(now.Year, now.Month, 1).AddMonths(quantity);
range.End = new DateTime(range.Start.Year, range.Start.Month, DateTime.DaysInMonth(range.Start.Year, range.Start.Month), 23, 59, 59, 999);
break;
}
}
}
}
else if (DateTime.TryParse(str, out var time) && time != DateTime.MinValue)
{
range.Start = time;
range.End = time;
}
}
if (range.Start == DateTime.MinValue && range.End == DateTime.MinValue)
{
range = null;
return false;
}
else if (
range.Start.Hour == 0 && range.Start.Minute == 0 && range.Start.Second == 0
&& range.End.Hour == 0 && range.End.Minute == 0 && range.End.Second == 0)
{
range.End = new DateTime(range.End.Year, range.End.Month, range.End.Day, 23, 59, 59, 999);
}
return true;
}
///
/// 获取今天范围
///
///
public static DateTimeRange GetTodayRange()
{
var now = DateTime.Now;
var range = new DateTimeRange();
range.Start = new DateTime(now.Year, now.Month, now.Day);
range.End = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
return range;
}
///
/// 格式化
///
///
///
public static string Format(this TimeSpan span)
{
if (span.Days > 0)
{
return $"{span.Days}天";
}
else if (span.Hours > 0)
{
return $"{span.Hours}小时{span.Minutes}分";
}
else if (span.Minutes > 0)
{
return $"{span.Minutes}分{span.Seconds}秒";
}
else if (span.Seconds > 0)
{
return $"{span.Seconds}秒";
}
else if (span.Milliseconds > 0)
{
return $"{span.Milliseconds}毫秒";
}
return null;
}
}
}