📄 dateandtime.vb
字号:
End Function Public Function DatePart( _ ByVal Interval As Microsoft.VisualBasic.DateInterval, _ ByVal DateValue As System.DateTime, _ Optional ByVal StartOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday, _ Optional ByVal StartOfYear As FirstWeekOfYear = FirstWeekOfYear.Jan1) As Integer Dim WeekRule As CalendarWeekRule = CalendarWeekRule.FirstDay Dim DayRule As DayOfWeek = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek Dim CurCalendar As Calendar = CultureInfo.CurrentCulture.Calendar Select Case Interval Case DateInterval.Year Return DateValue.Year Case DateInterval.Quarter Return Convert.ToInt32(DateValue.Month / 4 + 1) Case DateInterval.Month Return DateValue.Month Case DateInterval.WeekOfYear DayRule = GetDayRule(StartOfWeek, DayRule) WeekRule = GetWeekRule(StartOfYear, WeekRule) Return CurCalendar.GetWeekOfYear(DateValue, WeekRule, DayRule) Case DateInterval.Weekday Return ConvertWeekDay(DateValue.DayOfWeek, StartOfWeek) Case DateInterval.DayOfYear Return DateValue.DayOfYear Case DateInterval.Day Return DateValue.Day Case DateInterval.Hour Return DateValue.Hour Case DateInterval.Minute Return DateValue.Minute Case DateInterval.Second Return DateValue.Second Case Else Throw New ArgumentException End Select End Function Friend Function DateIntervalFromString( _ ByVal Interval As String) As DateInterval Select Case Interval Case "yyyy" Return DateInterval.Year Case "q" Return DateInterval.Quarter Case "m" Return DateInterval.Month Case "ww" Return DateInterval.WeekOfYear Case "w" Return DateInterval.Weekday Case "d" Return DateInterval.Day Case "y" Return DateInterval.DayOfYear Case "h" Return DateInterval.Hour Case "n" Return DateInterval.Minute Case "s" Return DateInterval.Second Case Else Throw New ArgumentException("Argument 'Interval' is not a valid value") End Select End Function Public Function DateAdd(ByVal Interval As String, _ ByVal Number As Double, ByVal DateValue As System.Object) As System.DateTime If (DateValue Is Nothing) Then Throw New ArgumentNullException("DateValue", "Value can not be null.") End If If (Not (TypeOf DateValue Is DateTime)) Then Throw New InvalidCastException End If Return DateAdd(DateIntervalFromString(Interval), Number, CType(DateValue, DateTime)) End Function Public Function DateDiff(ByVal Interval As String, _ ByVal Date1 As System.Object, ByVal Date2 As System.Object, _ Optional ByVal StartOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday, _ Optional ByVal StartOfYear As FirstWeekOfYear = FirstWeekOfYear.Jan1) As System.Int64 If (Date1 Is Nothing) Then Throw New ArgumentNullException("Date1", "Value can not be null.") End If If (Date2 Is Nothing) Then Throw New ArgumentNullException("Date2", "Value can not be null.") End If If (Not (TypeOf Date1 Is DateTime)) Then Throw New InvalidCastException End If If (Not (TypeOf Date2 Is DateTime)) Then Throw New InvalidCastException End If Return DateDiff(DateIntervalFromString(Interval), CType(Date1, DateTime), _ CType(Date2, DateTime), StartOfWeek, StartOfYear) End Function Public Function DatePart(ByVal Interval As String, _ ByVal DateValue As System.Object, _ Optional ByVal StartOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday, _ Optional ByVal StartOfYear As FirstWeekOfYear = FirstWeekOfYear.Jan1) As System.Int32 If (DateValue Is Nothing) Then Throw New ArgumentNullException("DateValue", "Value can not be null.") End If If (Not (TypeOf DateValue Is DateTime)) Then Throw New InvalidCastException End If Return DatePart(DateIntervalFromString(Interval), _ CType(DateValue, DateTime), StartOfWeek, StartOfYear) End Function Public Function DateSerial(ByVal Year As Integer, _ ByVal Month As Integer, _ ByVal Day As Integer) As System.DateTime Dim _date As DateTime If (Year < 0) Then Year = Year + DateTime.Now.Year ElseIf (Year >= 0 And Year <= 29) Then Year += 2000 ElseIf (Year >= 30 And Year <= 99) Then Year += 1900 End If _date = New DateTime(Year, 1, 1) _date = _date.AddMonths(Month - 1) _date = _date.AddDays(Day - 1) Return _date End Function Public Function TimeSerial(ByVal Hour As Integer, _ ByVal Minute As Integer, _ ByVal Second As Integer) As System.DateTime Dim day As Integer = 1 If (Second < 0) Then If (Minute = 0 And Hour = 0) Then Second += 60 ElseIf (Minute = 0) Then Second += 60 Minute = 59 Hour = Hour - 1 Else Second += 60 Minute = Minute - 1 End If ElseIf (Second > 59) Then Minute += Convert.ToInt32(Second \ 60) Second = Convert.ToInt32(Decimal.Remainder(Second, 60)) End If If (Minute < 0) Then If (Hour = 0) Then Minute += 60 Else Minute += 60 Hour = Hour - 1 End If ElseIf (Minute > 59) Then Hour += Convert.ToInt32(Minute \ 60) Minute = Convert.ToInt32(Decimal.Remainder(Minute, 60)) End If If (Hour < 0) Then Hour += 24 ElseIf (Hour > 23) Then day += Convert.ToInt32(Hour \ 24) Hour = Convert.ToInt32(Decimal.Remainder(Hour, 24)) End If Return New DateTime(1, 1, day, Hour, Minute, Second) End Function Public Function DateValue(ByVal StringDate As String) As System.DateTime Try Dim d As DateTime = DateTime.Parse(StringDate, _ System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat, DateTimeStyles.NoCurrentDateDefault) Return d.Date Catch exception As FormatException Throw New InvalidCastException(String.Format("Cast from string {0} to type '{1}' is not valid.", StringDate, "Date")) End Try End Function Public Function TimeValue(ByVal StringTime As String) As System.DateTime Try Return DateTime.MinValue.Add(DateTime.Parse(StringTime).TimeOfDay) Catch exception As FormatException Throw New InvalidCastException(String.Format("Cast from string {0} to type '{1}' is not valid.", StringTime, "Date")) End Try End Function Public Function Year(ByVal DateValue As System.DateTime) As Integer Return DateValue.Year End Function Public Function Month(ByVal DateValue As System.DateTime) As Integer Return DateValue.Month End Function Public Function Day(ByVal DateValue As System.DateTime) As Integer Return DateValue.Day End Function Public Function Hour(ByVal TimeValue As System.DateTime) As Integer Return TimeValue.Hour End Function Public Function Minute(ByVal TimeValue As System.DateTime) As Integer Return TimeValue.Minute End Function Public Function Second(ByVal TimeValue As System.DateTime) As Integer Return TimeValue.Second End Function Public Function Weekday(ByVal DateValue As System.DateTime, _ Optional ByVal StartOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday) As Integer Return DatePart(DateInterval.Weekday, DateValue, StartOfWeek, FirstWeekOfYear.System) End Function Public Function MonthName(ByVal Month As Integer, _ Optional ByVal Abbreviate As Boolean = False) As System.String 'LAMESPEC: MSDN states that in 12-month calendar the '13 month should return empty string, but exception is thrown instead If (Month < 1 Or Month > 13) Then Throw New ArgumentException("Argument 'Month' is not a valid value.") End If If (Abbreviate) Then Return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(Month) Else Return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Month) End If End Function Public Function WeekdayName(ByVal Weekday As Integer, _ Optional ByVal Abbreviate As System.Boolean = False, _ Optional ByVal FirstDayOfWeekValue As FirstDayOfWeek = FirstDayOfWeek.System) As System.String If (Weekday < 1 Or Weekday > 7) Then Throw New ArgumentException("Argument 'Weekday' is not a valid value.") End If Dim dti As DateTimeFormatInfo = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat If (FirstDayOfWeekValue = FirstDayOfWeek.System) Then FirstDayOfWeekValue = CType(dti.FirstDayOfWeek, FirstDayOfWeek) Else FirstDayOfWeekValue = CType((FirstDayOfWeekValue - 1), FirstDayOfWeek) End If Weekday += FirstDayOfWeekValue - FirstDayOfWeek.Sunday Weekday = Convert.ToInt32(Decimal.Remainder(Weekday, 7)) If (Abbreviate) Then Return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedDayName(CType(Weekday, DayOfWeek)) Else Return CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(CType(Weekday, DayOfWeek)) End If End Function End ModuleEnd Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -