| 1 |
#Region "Microsoft.VisualBasic::d63cd54a853a12b7706f778f74ce678c, Microsoft.VisualBasic.Core\Extensions\ValueTypes\DateTimeHelper.vb"
|
| 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 |
#End Region
|
| 46 |
|
| 47 |
Imports System.Runtime.CompilerServices
|
| 48 |
|
| 49 |
Namespace ValueTypes
|
| 50 |
|
| 51 |
Public Module DateTimeHelper
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Public ReadOnly Property MonthList As Dictionary(Of String, Integer)
|
| 58 |
|
| 59 |
Sub New()
|
| 60 |
MonthList = New Dictionary(Of String, Integer)
|
| 61 |
|
| 62 |
MonthList.Add("January", 1)
|
| 63 |
MonthList.Add("Jan", 1)
|
| 64 |
|
| 65 |
MonthList.Add("February", 2)
|
| 66 |
MonthList.Add("Feb", 2)
|
| 67 |
|
| 68 |
MonthList.Add("March", 3)
|
| 69 |
MonthList.Add("Mar", 3)
|
| 70 |
|
| 71 |
MonthList.Add("April", 4)
|
| 72 |
MonthList.Add("Apr", 4)
|
| 73 |
|
| 74 |
MonthList.Add("May", 5)
|
| 75 |
|
| 76 |
MonthList.Add("June", 6)
|
| 77 |
MonthList.Add("Jun", 6)
|
| 78 |
|
| 79 |
MonthList.Add("July", 7)
|
| 80 |
MonthList.Add("Jul", 7)
|
| 81 |
|
| 82 |
MonthList.Add("August", 8)
|
| 83 |
MonthList.Add("Aug", 8)
|
| 84 |
|
| 85 |
MonthList.Add("September", 9)
|
| 86 |
MonthList.Add("Sep", 9)
|
| 87 |
|
| 88 |
MonthList.Add("October", 10)
|
| 89 |
MonthList.Add("Oct", 10)
|
| 90 |
|
| 91 |
MonthList.Add("November", 11)
|
| 92 |
MonthList.Add("Nov", 11)
|
| 93 |
|
| 94 |
MonthList.Add("December", 12)
|
| 95 |
MonthList.Add("Dec", 12)
|
| 96 |
End Sub
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
Public Function GetMonthInteger(mon As String) As Integer
|
| 104 |
If Not MonthList.ContainsKey(mon) Then
|
| 105 |
For Each k As String In MonthList.Keys
|
| 106 |
If String.Equals(mon, k, StringComparison.OrdinalIgnoreCase) Then
|
| 107 |
Return MonthList(k)
|
| 108 |
End If
|
| 109 |
Next
|
| 110 |
|
| 111 |
Return -1
|
| 112 |
Else
|
| 113 |
Return MonthList(mon)
|
| 114 |
End If
|
| 115 |
End Function
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 124 |
Public Function FillDateZero(d As Integer) As String
|
| 125 |
Return If(d >= 10, d, "0" & d)
|
| 126 |
End Function
|
| 127 |
|
| 128 |
|
| 129 |
枚举出在<paramref name="start"/>到<paramref name="ends"/>这个时间窗里面的所有日期,单位为天
|
| 130 |
|
| 131 |
<param name="start"></param>
|
| 132 |
<param name="ends"></param>
|
| 133 |
<returns>返回值里面包含有起始和结束的日期</returns>
|
| 134 |
Public Iterator Function DateSeq(start As Date, ends As Date) As IEnumerable(Of Date)
|
| 135 |
Do While start <= ends
|
| 136 |
' Yield $"{start.Year}-{If(start.Month < 10, "0" & start.Month, start.Month)}-{If(start.Day < 10, "0" & start.Day, start.Day)}"
|
| 137 |
Yield start
|
| 138 |
start = start.AddDays(1)
|
| 139 |
Loop
|
| 140 |
End Function
|
| 141 |
|
| 142 |
|
| 143 |
yyyy-mm-dd
|
| 144 |
|
| 145 |
<param name="x"></param>
|
| 146 |
|
| 147 |
<Extension>
|
| 148 |
Public Function YYMMDD(x As Date) As String
|
| 149 |
Dim mm$ = If(x.Month < 10, "0" & x.Month, x.Month)
|
| 150 |
Dim dd$ = If(x.Day < 10, "0" & x.Day, x.Day)
|
| 151 |
Return $"{x.Year}-{mm}-{dd}"
|
| 152 |
End Function
|
| 153 |
|
| 154 |
|
| 155 |
Convert <see cref="DateTime"/> to unix time stamp
|
| 156 |
|
| 157 |
<param name="time"></param>
|
| 158 |
|
| 159 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 160 |
<Extension>
|
| 161 |
Public Function UnixTimeStamp(time As DateTime) As Long
|
| 162 |
Static ZERO As New DateTime(1970, 1, 1, 0, 0, 0)
|
| 163 |
Return (time.ToUniversalTime - ZERO).TotalSeconds
|
| 164 |
End Function
|
| 165 |
|
| 166 |
|
| 167 |
将Unix时间戳转换为可读的日期
|
| 168 |
|
| 169 |
<param name="unixDateTime"></param>
|
| 170 |
|
| 171 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 172 |
<Extension>
|
| 173 |
Public Function FromUnixTimeStamp(unixDateTime As Long) As Date
|
| 174 |
Return DateTimeOffset _
|
| 175 |
.FromUnixTimeSeconds(unixDateTime) _
|
| 176 |
.DateTime _
|
| 177 |
.ToLocalTime()
|
| 178 |
End Function
|
| 179 |
|
| 180 |
Const ZeroDate1$ = "0001-01-01, 00:00:00"
|
| 181 |
Const ZeroDate2$ = "0000-00-00, 00:00:00"
|
| 182 |
|
| 183 |
对于unix timestamp而言,这个日期是零
|
| 184 |
|
| 185 |
Const ZeroDate3$ = "1970-01-01, 08:00:00"
|
| 186 |
|
| 187 |
<Extension>
|
| 188 |
Public Function IsEmpty(time As Date, Optional unixTimestamp As Boolean = False) As Boolean
|
| 189 |
Dim ts = time.FormatTime()
|
| 190 |
|
| 191 |
If ts = ZeroDate1 OrElse ts = ZeroDate2 Then
|
| 192 |
Return True
|
| 193 |
ElseIf unixTimestamp AndAlso ts = ZeroDate3 Then
|
| 194 |
Return True
|
| 195 |
Else
|
| 196 |
Return False
|
| 197 |
End If
|
| 198 |
End Function
|
| 199 |
End Module
|
| 200 |
End Namespace
|