-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeExtensions.cs
More file actions
44 lines (40 loc) · 1.77 KB
/
DateTimeExtensions.cs
File metadata and controls
44 lines (40 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
namespace CSharpExtensions.OpenSource
{
public static class DateTimeExtensions
{
/// <summary>
/// Get max date between two dates
/// </summary>
/// <param name="date1"></param>
/// <param name="date2"></param>
/// <returns></returns>
public static DateTime Max(DateTime date1, DateTime date2)
{
return date1 > date2 ? date1 : date2;
}
/// <summary>
/// Get min date between two dates
/// </summary>
/// <param name="date1"></param>
/// <param name="date2"></param>
/// <returns></returns>
public static DateTime Min(DateTime date1, DateTime date2)
{
return date1 < date2 ? date1 : date2;
}
public static string ToHumanReadableString(this TimeSpan t)
{
if (t.TotalSeconds <= 1) { return $@"{t:s\.ff} seconds"; }
if (t.TotalMinutes <= 1) { return $@"{t.TotalSeconds} seconds"; }
if (t.TotalHours <= 1) { return $@"{t.Minutes}:{t.Seconds} minutes"; }
if (t.TotalDays <= 1) { return $@"{t.Hours}:{t.Minutes} hours"; }
return $@"{t.Days} days and {t.Hours}:{t.Minutes} hours";
}
public static DateTime StartOfDay(this DateTime date) => date.Date;
public static DateTime EndOfDay(this DateTime date) => date.StartOfDay().AddDays(1).AddTicks(-1);
public static DateTime? FixUnixDate(this DateTime date) => date < new DateTime(1970, 1, 1) ? null : (DateTime?)date;
public static long ToUnix(this DateTime date) => date < new DateTime(1970, 1, 1) ? 0 : ((DateTimeOffset)date).ToUnixTimeSeconds();
public static string MongoISO(this DateTime date) => date.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ");
}
}