-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathDateTimeFormat.cs
More file actions
66 lines (57 loc) · 1.63 KB
/
DateTimeFormat.cs
File metadata and controls
66 lines (57 loc) · 1.63 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
namespace SourceGit.Models
{
public class DateTimeFormat
{
public static readonly List<DateTimeFormat> Supported = new List<DateTimeFormat>
{
new("yyyy/MM/dd"),
new("yyyy.MM.dd"),
new("yyyy-MM-dd"),
new("MM/dd/yyyy"),
new("MM.dd.yyyy"),
new("MM-dd-yyyy"),
new("dd/MM/yyyy"),
new("dd.MM.yyyy"),
new("dd-MM-yyyy"),
new("MMM d yyyy"),
new("d MMM yyyy"),
};
public static int ActiveIndex
{
get;
set;
} = 0;
public static bool Use24Hours
{
get;
set;
} = true;
public string DateFormat
{
get;
}
public string Example
{
get => DateTime.Now.ToString(DateFormat);
}
public DateTimeFormat(string date)
{
DateFormat = date;
}
public static string Format(ulong timestamp, bool dateOnly = false)
{
var localTime = DateTime.UnixEpoch.AddSeconds(timestamp).ToLocalTime();
return Format(localTime, dateOnly);
}
public static string Format(DateTime localTime, bool dateOnly = false)
{
var actived = Supported[ActiveIndex];
if (dateOnly)
return localTime.ToString(actived.DateFormat);
var format = Use24Hours ? $"{actived.DateFormat} HH:mm:ss" : $"{actived.DateFormat} hh:mm:ss tt";
return localTime.ToString(format);
}
}
}