/// <summary> /// Get the calendar time description from this DateTime instance to a specified DateTime instance /// </summary> /// <param name="This">The date instance which to start comparison from</param> /// <param name="dateTime">The date instance to compare to</param> /// <param name="formats">An object describing how the output string should be displayed</param> /// <returns></returns> public static string CalendarTime(this DateTime This, DateTime dateTime, CalendarTimeFormats formats = null) { formats = formats ?? new CalendarTimeFormats(); var startDate = This.Kind == DateTimeKind.Local ? This : This.ToLocalTime(); var endDate = dateTime.Kind == DateTimeKind.Local ? dateTime : dateTime.ToLocalTime(); var timeDiff = endDate - startDate; if (startDate.Date == endDate.Date) { return(endDate.ToString(formats.SameDay)); } if (startDate.AddDays(1).Date == endDate.Date) { return(endDate.ToString(formats.NextDay)); } if (startDate.AddDays(-1).Date == endDate.Date) { return(endDate.ToString(formats.LastDay)); } if (timeDiff.TotalDays > 1 && timeDiff.TotalDays < 7) { return(endDate.ToString(formats.NextWeek)); } if (timeDiff.TotalDays >= -6 && timeDiff.TotalDays < -1) { return(endDate.ToString(formats.LastWeek)); } return(endDate.ToString(formats.EverythingElse)); }
/// <summary> /// Get the calendar time description from this DateTime instance to the current time /// </summary> /// <param name="This">The date instance which to compare with the current date</param> /// <param name="formats">An object describing how the output string should be displayed</param> /// <returns></returns> public static string CalendarTime(this DateTime This, CalendarTimeFormats formats = null) { return(CalendarTime(This, DateTime.Now, formats)); }