using System; using System.Diagnostics; namespace Masuit.Tools.DateTimeExt { /// /// 日期操作工具类 /// public static class DateUtil { /// /// 返回相对于当前时间的相对天数 /// /// /// 相对天数 public static string GetDateTime(this DateTime dt, int relativeday) { return dt.AddDays(relativeday).ToString("yyyy-MM-dd HH:mm:ss"); } /// /// 返回标准时间格式string /// public static string GetDateTimeF(this DateTime dt) => dt.ToString("yyyy-MM-dd HH:mm:ss:fffffff"); /// /// 返回标准时间 /// /// 日期时间字符串 /// 格式 public static string GetStandardDateTime(this string fDateTime, string formatStr) { if (fDateTime == "0000-0-0 0:00:00") { return fDateTime; } var s = Convert.ToDateTime(fDateTime); return s.ToString(formatStr); } /// /// 返回标准时间 yyyy-MM-dd HH:mm:ss /// /// 日期时间字符串 public static string GetStandardDateTime(this string fDateTime) { return GetStandardDateTime(fDateTime, "yyyy-MM-dd HH:mm:ss"); } /// /// 获取该时间相对于1970-01-01 00:00:00的秒数 /// /// /// public static double GetTotalSeconds(this DateTime dt) => new DateTimeOffset(dt).ToUnixTimeSeconds(); /// /// 获取该时间相对于1970-01-01 00:00:00的毫秒数 /// /// /// public static double GetTotalMilliseconds(this DateTime dt) => new DateTimeOffset(dt).ToUnixTimeMilliseconds(); /// /// 获取该时间相对于1970-01-01 00:00:00的微秒时间戳 /// /// /// public static long GetTotalMicroseconds(this DateTime dt) => new DateTimeOffset(dt).Ticks / 10; /// /// 获取该时间相对于1970-01-01 00:00:00的纳秒时间戳 /// /// /// public static long GetTotalNanoseconds(this DateTime dt) => new DateTimeOffset(dt).Ticks * 100 + Stopwatch.GetTimestamp() % 100; /// /// 获取该时间相对于1970-01-01 00:00:00的分钟数 /// /// /// public static double GetTotalMinutes(this DateTime dt) => new DateTimeOffset(dt).Offset.TotalMinutes; /// /// 获取该时间相对于1970-01-01 00:00:00的小时数 /// /// /// public static double GetTotalHours(this DateTime dt) => new DateTimeOffset(dt).Offset.TotalHours; /// /// 获取该时间相对于1970-01-01 00:00:00的天数 /// /// /// public static double GetTotalDays(this DateTime dt) => new DateTimeOffset(dt).Offset.TotalDays; /// /// 返回本年有多少天 /// /// /// 年份 /// 本年的天数 public static int GetDaysOfYear(this DateTime _, int iYear) { return IsRuYear(iYear) ? 366 : 365; } /// 本年有多少天 /// 日期 /// 本天在当年的天数 public static int GetDaysOfYear(this DateTime dt) { //取得传入参数的年份部分,用来判断是否是闰年 int n = dt.Year; return IsRuYear(n) ? 366 : 365; } /// 本月有多少天 /// /// 年 /// 月 /// 天数 public static int GetDaysOfMonth(this DateTime _, int iYear, int month) { return month switch { 1 => 31, 2 => (IsRuYear(iYear) ? 29 : 28), 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31, _ => 0 }; } /// 本月有多少天 /// 日期 /// 天数 public static int GetDaysOfMonth(this DateTime dt) { //--利用年月信息,得到当前月的天数信息。 return dt.Month switch { 1 => 31, 2 => (IsRuYear(dt.Year) ? 29 : 28), 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31, _ => 0 }; } /// 返回当前日期的星期名称 /// 日期 /// 星期名称 public static string GetWeekNameOfDay(this DateTime idt) { return idt.DayOfWeek.ToString() switch { "Mondy" => "星期一", "Tuesday" => "星期二", "Wednesday" => "星期三", "Thursday" => "星期四", "Friday" => "星期五", "Saturday" => "星期六", "Sunday" => "星期日", _ => "" }; } /// 返回当前日期的星期编号 /// 日期 /// 星期数字编号 public static string GetWeekNumberOfDay(this DateTime idt) { return idt.DayOfWeek.ToString() switch { "Mondy" => "1", "Tuesday" => "2", "Wednesday" => "3", "Thursday" => "4", "Friday" => "5", "Saturday" => "6", "Sunday" => "7", _ => "" }; } /// 判断当前年份是否是闰年,私有函数 /// 年份 /// 是闰年:True ,不是闰年:False private static bool IsRuYear(int iYear) { //形式参数为年份 //例如:2003 var n = iYear; return n % 400 == 0 || n % 4 == 0 && n % 100 != 0; } /// /// 判断是否为合法日期,必须大于1800年1月1日 /// /// 输入日期字符串 /// True/False public static bool IsDateTime(this string strDate) { DateTime.TryParse(strDate, out var result); return result.CompareTo(DateTime.Parse("1800-1-1")) > 0; } } }