DateTime - ToEpochTimeSpan
A DateTime extension method that converts the @this to an epoch time span.
public static void Main() { DateTime todayDate = DateTime.Now; Console.WriteLine("Current Date: {0}\n", todayDate.ToFullDateTimeString()); // C# Extension Method: DateTime - ToEpochTimeSpan TimeSpan epochTimeSpan = todayDate.ToEpochTimeSpan(); Console.WriteLine("Epoch time span.\n\n"); Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", epochTimeSpan.Days); Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", epochTimeSpan.TotalDays); Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", epochTimeSpan.Hours); Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", epochTimeSpan.TotalHours); Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", epochTimeSpan.Minutes); Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", epochTimeSpan.TotalMinutes); Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", epochTimeSpan.Seconds); Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", epochTimeSpan.TotalSeconds); Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", epochTimeSpan.Milliseconds); Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", epochTimeSpan.TotalMilliseconds); Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", epochTimeSpan.Ticks); }
View Source
using System; public static partial class Extensions { /// <summary> /// A DateTime extension method that converts the @this to an epoch time span. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>@this as a TimeSpan.</returns> public static TimeSpan ToEpochTimeSpan(this DateTime @this) { return @this.Subtract(new DateTime(1970, 1, 1)); } }