using System; using System.Collections.Generic; using System.Text; namespace TEAMModelOS.SDK { public static class DateTimeHelper { public static DateTime FromUnixTimestamp(this long unixtime) { DateTime sTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local); return sTime.AddMilliseconds(unixtime); } public static long ToUnixTimestamp(this DateTime datetime) { //DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime sTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local, TimeZoneInfo.Local); return (long)(datetime - sTime).TotalMilliseconds; } /// /// 获得 GMT+8 时间 /// /// public static DateTime GetGMTTime(this DateTime dateTime, int GMT = 8) { //SystemTimeZoneById : https://learn.microsoft.com/zh-cn/previous-versions/windows/embedded/ms912391(v=winembedded.11) //TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");//设置时区 //DateTime easternTime = TimeZoneInfo.ConvertTimeFromUtc(dateTime, easternZone); //return easternTime; //处理UTC时差 TimeZoneInfo localTimezone = TimeZoneInfo.Local; var Hours = localTimezone.BaseUtcOffset.Hours; dateTime = dateTime.AddHours(GMT-Hours); return dateTime; } /// /// 默认东八区+8 /// /// /// /// public static DateTimeOffset GetGMTTime(this DateTimeOffset dateTime,int GMT=+8) { //处理UTC时差 TimeZoneInfo localTimezone = TimeZoneInfo.Local; var Hours = localTimezone.BaseUtcOffset.Hours; dateTime = dateTime.AddHours(GMT-Hours); return dateTime; } public static DateTimeOffset GetUTCTime(this DateTimeOffset dateTime, int GMT) { //处理UTC时差 TimeZoneInfo localTimezone = TimeZoneInfo.Local; var Hours = localTimezone.BaseUtcOffset.Hours; dateTime = dateTime.AddHours(Hours - GMT); return dateTime; } public static int getDays(int year) { int day = 0; for (int i = 0;i<=12;i++) { int days = 0; if (i != 2) { switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; } } else { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { days = 29; } else { days = 28; } } day += days; } return day; } #region 获取 本周、本月、本季度、本年 的开始时间或结束时间 /// /// 获取开始时间 /// /// Week、Month、Season、Year /// /// public static DateTime? GetTimeStartByType(string TimeType, DateTime now) { switch (TimeType) { case "Week": return now.AddDays(-(int)now.DayOfWeek + 1); case "Month": return now.AddDays(-now.Day + 1); case "Season": var time = now.AddMonths(0 - ((now.Month - 1) % 3)); return time.AddDays(-time.Day + 1); case "Year": return now.AddDays(-now.DayOfYear + 1); default: return null; } } /// /// 获取结束时间 /// /// Week、Month、Season、Year /// /// public static DateTime? GetTimeEndByType(string TimeType, DateTime now) { switch (TimeType) { case "Week": return now.AddDays(7 - (int)now.DayOfWeek); case "Month": return now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1); case "Season": var time = now.AddMonths((3 - ((now.Month - 1) % 3) - 1)); return time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1); case "Year": var time2 = now.AddYears(1); return time2.AddDays(-time2.DayOfYear); default: return null; } } #endregion } }