123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using ERP.Framework.Constants;
- namespace System
- {
- public static class DateTimeExtension
- {
-
-
-
-
-
- public static int GetAge(this DateTime @this)
- {
-
-
-
- if (DateTime.Today.Month < @this.Month ||
- DateTime.Today.Month == @this.Month &&
- DateTime.Today.Day < @this.Day)
- {
- return DateTime.Today.Year - @this.Year - 1;
- }
-
- return DateTime.Today.Year - @this.Year;
- }
-
-
-
-
-
- public static bool IsWeekDay(this DateTime @this)
- {
- return !(@this.DayOfWeek == DayOfWeek.Saturday || @this.DayOfWeek == DayOfWeek.Sunday);
- }
-
-
-
-
-
- public static bool IsWeekendDay(this DateTime @this)
- {
- return @this.DayOfWeek == DayOfWeek.Saturday || @this.DayOfWeek == DayOfWeek.Sunday;
- }
-
-
-
-
-
-
- public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startDayOfWeek = DayOfWeek.Sunday)
- {
- var start = new DateTime(dt.Year, dt.Month, dt.Day);
- if (start.DayOfWeek != startDayOfWeek)
- {
- var d = startDayOfWeek - start.DayOfWeek;
- if (startDayOfWeek <= start.DayOfWeek)
- {
- return start.AddDays(d);
- }
- return start.AddDays(-7 + d);
- }
- return start;
- }
-
-
-
-
-
- public static TimeSpan ToEpochTimeSpan(this DateTime @this) => @this.ToUniversalTime().Subtract(new DateTime(1970, 1, 1));
-
-
-
-
-
-
-
- public static bool InRange(this DateTime @this, DateTime minValue, DateTime maxValue)
- {
- return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0;
- }
-
-
-
-
-
- public static string ToDateString(this DateTime @this)
- {
- return @this.ToString(DateTimeConstant.Date);
- }
-
-
-
-
-
- public static string ToDateShortString(this DateTime @this)
- {
- return @this.ToString(DateTimeConstant.Date_Short);
- }
-
-
-
-
-
- public static string ToDateLongString(this DateTime @this)
- {
- return @this.ToString(DateTimeConstant.Date_Long);
- }
-
-
-
-
-
- public static string ToDateTimeString(this DateTime @this)
- {
- return @this.ToString(DateTimeConstant.Dete_Time);
- }
-
-
-
-
-
- public static string ToDateTimeShortString(this DateTime @this)
- {
- return @this.ToString(DateTimeConstant.Dete_Time_Short);
- }
-
-
-
-
-
- public static string ToDateTimeLongString(this DateTime @this)
- {
- return @this.ToString(DateTimeConstant.Dete_Time_Long);
- }
- }
- }
|