0
点赞
收藏
分享

微信扫一扫

MIT 6.858 计算机系统安全讲义 2014 秋季(四)

柠檬的那个酸_2333 2024-03-11 阅读 9
.net

moment.js方便了javascript中日期的计算,所以我也写这些函数方便vb.net中日期的计算。
求今年的最后一天的23:59:59,这个月的最后一天等等。

Function EndOf(Unit As String) As Date
    Dim mydate As Date = Now()
    Dim myyear As Integer = Year(mydate)
    Dim mymonth As Integer = Month(mydate)
    Dim myday As Integer = Day(mydate)
    Dim myhour As Integer = Hour(mydate)
    Dim myminute As Integer = Minute(mydate)
    Dim mysecond As Integer = Second(mydate)
    Select Case Unit
        Case "year"
            Console.WriteLine("year")
            EndOf = New Date(myyear, 12, 31, 23, 59, 59)
        Case "month"
            Console.WriteLine("month")
            Dim NextMonth As Date = DateAdd(DateInterval.Month, 1, mydate)
            Dim Date2 As Date = DateAdd(DateInterval.Day, -1, setDay(1, NextMonth))
            Dim myyear2 As Integer = Year(Date2)
            Dim mymonth2 As Integer = Month(Date2)
            Dim myday2 As Integer = Day(Date2)
            EndOf = New Date(myyear2, mymonth2, myday2, 23, 59, 59)

        Case "week"
            Dim Date1 = DateAdd(DateInterval.Day, 6, StartOf("week"))
            Dim myyear1 As Integer = Year(Date1)
            Dim mymonth1 As Integer = Month(Date1)
            Dim myday1 As Integer = Day(Date1)
            EndOf = New Date(myyear1, mymonth1, myday1, 23, 59, 59)
        Case "day"
            EndOf = New Date(myyear, mymonth, myday, 23, 59, 59)
        Case "hour"
            EndOf = New Date(myyear, mymonth, myday, myhour, 59, 59)
        Case "minute"
            EndOf = New Date(myyear, mymonth, myday, myhour, myminute, 59)

    End Select
End Function
EndOf("year")
EndOf("month")
EndOf("week")
EndOf("day")
EndOf("hour")
EndOf("minute")

这个月有多少天?

 Function DaysInMonth() As Integer
     Dim lastDay As Date = EndOf("month")
     DaysInMonth = Day(lastDay)
 End Function

Date1是否在Date2之前?

 Function IsBefore(Date1 As Date, Date2 As Date) As Boolean
     IsBefore = DateDiff(DateInterval.Second, Date1, Date2) > 0
 End Function

两个日期是否相等?

Function IsSame(Date1 As Date, Date2 As Date) As Boolean
    IsSame = DateDiff(DateInterval.Second, Date1, Date2) = 0
End Function

Date1是否在Date2之后?

 Function IsAfter(Date1 As Date, Date2 As Date) As Boolean
     IsAfter = DateDiff(DateInterval.Second, Date1, Date2) < 0
 End Function

Date1是否小于或等于Date2?

Function IsSameOrBefore(Date1 As Date, Date2 As Date) As Boolean
    IsSameOrBefore = IsSame(Date1, Date2) Or IsBefore(Date1, Date2)
End Function

Target是否在Date1和Date2之间?

Function IsBetween(Target As Date, Date1 As Date, Date2 As Date)
    IsBetween = IsSameOrAfter(Target, Date1) And IsSameOrBefore(Target, Date2)
End Function

判断是否是闰年?

Function IsLeapYear(MyDate As Date) As Boolean
    Dim YearNum As Integer = Year(MyDate)
    Dim Result As Boolean
    If (YearNum Mod 4 = 0 And YearNum Mod 100 <> 0) Or YearNum Mod 400 = 0 Then
        Result = True
    Else
        Result = False

    End If
    IsLeapYear = Result
End Function
举报

相关推荐

0 条评论