简体   繁体   中英

VB.net How to Calculate the 15th and 30th of the month?

For example, the 15th of January is from Jan 1 to 15, and the 30th of January is Jan 1 to Jan 30. Since not all months are composed of 30 days, therefore the 15th of February will be from Jan 31 to February 14. So what could be the formula in getting the 15th and 30th of the PRESENT month? Thanks in advance.

If I understand your question correctly, you want the number for which day of the year it is? If so you can do this

Sub Main()
    Dim FIFTEENTH As DateTime = New DateTime(Now.Year, Now.Month, 15)
    Dim THIRTIETH As DateTime
    Dim hasThirty As Boolean = (DateTime.DaysInMonth(Now.Year, Now.Month) >= 30)

    Console.WriteLine("The fifteenth: ")
    Console.WriteLine($"Day of the Year: {FIFTEENTH.DayOfYear}")
    Console.WriteLine($"Day of the Week: {FIFTEENTH.DayOfWeek.ToString("D")} / {FIFTEENTH.DayOfWeek.ToString("g")}")

    If (hasThirty) Then
        Console.WriteLine("The Thirtieth: ")

        Console.WriteLine($"Day of the Year: {THIRTIETH.DayOfYear}")
        Console.WriteLine($"Day of the Week: {THIRTIETH.DayOfWeek.ToString("D")} / {THIRTIETH.DayOfWeek.ToString("g")}")
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM