DateTime - IsAfternoon
A DateTime extension method that query if '@this' is afternoon.
public static void Main() { DateTime date1 = new DateTime(2018, 2, 2, 12, 0, 0); DateTime date2 = new DateTime(2006, 7, 25, 10, 45, 09); Display(date1); Display(date2); } public static void Display(DateTime date) { // C# Extension Method: DateTime - IsAfternoon if(date.IsAfternoon()) { Console.WriteLine("{0, 40}: It's afternoon", date.ToFullDateTimeString()); } else { Console.WriteLine("{0, 40}: It's not afternoon", date.ToFullDateTimeString()); } }
View Source
using System; public static partial class Extensions { /// <summary> /// A DateTime extension method that query if '@this' is afternoon. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>true if afternoon, false if not.</returns> public static bool IsAfternoon(this DateTime @this) { return @this.TimeOfDay >= new DateTime(2000, 1, 1, 12, 0, 0).TimeOfDay; } }