VB.Net/Development/Date Time
Содержание
Assign a value according to the current day of the week.
Module Module1
Sub Main()
If (Weekday(Now) = vbSaturday) Or (Weekday(Now) = vbSunday) Then
System.Console.WriteLine("weekend")
Else
System.Console.WriteLine("working day")
End If
End Sub
End Module
Date Time Detail: Long Date String, day of week, days in month
Imports System.IO
Module Module1
Sub Main()
Dim DateInfo As New DateTime(Now.Ticks)
Console.WriteLine("Date: " & DateInfo.Month & "/" & _
DateInfo.Day & "/" & DateInfo.Year)
Console.WriteLine("Date: " & DateInfo.ToLongDateString())
Console.WriteLine("Today is: " & DateInfo.DayOfWeek)
Console.WriteLine("Days in April, 2002: " & DateInfo.DaysInMonth(2002, 4))
End Sub
End Module
Date time: Now, Today and UTC Now
Public Class MainClass
Public Shared Sub Main()
"Date Time examples
Dim dteNow As Date = Now()
Dim dteToday As Date = Today()
Dim dteGMT As DateTime = DateTime.UtcNow()
Console.WriteLine(dteNow)
Console.WriteLine(dteToday)
Console.WriteLine(dteGMT)
End Sub
End Class
Display the various properties for a Date
Imports System
Public Class MainClass
Shared Sub Main()
Dim dteData As Date
"Get the current date and time
dteData = Now
"Display the various properties
System.Console.WriteLine("Month: " & dteData.Month)
System.Console.WriteLine("Day: " & dteData.Day)
System.Console.WriteLine("Year: " & dteData.Year)
System.Console.WriteLine("Hour: " & dteData.Hour)
System.Console.WriteLine("Minute: " & dteData.Minute)
System.Console.WriteLine("Second: " & dteData.Second)
System.Console.WriteLine("Day of week: " & dteData.DayOfWeek)
System.Console.WriteLine("Day of year: " & dteData.DayOfYear)
End Sub
End Class
Finding the Last Day of the Month
Module MainModule
Public Function LastDayOfMonth(ByVal MonthYear As String) As Integer
Return DatePart("d ", DateAdd("d ", -1, DateAdd("m ", 1, DateAdd("d ", -DatePart("d ", MonthYear) + 1, MonthYear))))
End Function
End Module
Get Date Time Now
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim currentTime as System.DateTime = System.DateTime.Now
Console.WriteLine("currentTime Year :" & currentTime.Year)
Console.WriteLine("currentTime Month:" & currentTime.Month)
Console.WriteLine("currentTime Date:" & currentTime.Date)
Console.WriteLine("currentTime Hour:" & currentTime.Hour)
Console.WriteLine("currentTime Minute:" & currentTime.Minute)
Console.WriteLine("currentTime Second:" & currentTime.Second)
End Sub
End Class
Is Leap Year
Imports System.IO
Module Module1
Sub Main()
Dim Dt As DateTime = Now()
If (Dt.IsLeapYear(Dt.Year)) Then
Console.WriteLine("This is a leap year")
Else
Console.WriteLine("This is not a leap year")
End If
End Sub
End Module