Working With Dates and Times Using ASP.NET VB

I know we have all have to use dates at some time or another.  I came across this posting and thought I would list it here for you.  Hope it helps.

Dim now As Date = Date.Now ‘ date and time

Dim today As Date = Date.Today ‘ just the date

Dim tomorrow As Date = today.AddDays(1)

Dim yesterday As Date = today.AddDays(-1)

‘ use parse functions to convert from strings

Dim vb1 As Date = Date.Parse(“1991-05-01″)

‘ use culture info to parse dates in regional formats

Dim gb As New CultureInfo(“en-GB”)

vb1 = Date.Parse(“01-05-1991″, gb.DateTimeFormat)

‘ format dates as strings

vb1.ToLongDateString() ‘ Wednesday, May 01, 1991

vb1.ToShortDateString() ‘ 5/1/1991

vb1.ToLongTimeString() ‘ 12:00:00 AM

vb1.ToShortTimeString() ‘ 12:00 AM

vb1.ToString(“u”) ‘ 1991-05-01 00:00:00Z

vb1.ToString(“yyyy-MM-dd”) ‘ 1991-05-01

‘ TimeSpan represents differnce between two dates

Dim time As TimeSpan = tomorrow – today

Dim days As Integer = time.Days

Dim hours As Integer = time.Hours

Dim minutes As Integer = time.Minutes

Dim seconds As Integer = time.Seconds

Dim milliseconds As Integer = time.Milliseconds

‘ Add time using other timespan instances

time.Add(New TimeSpan(days, hours, minutes, seconds,

milliseconds))

‘ or

time += New TimeSpan(days, hours, minutes, seconds,

milliseconds)

‘ use a timespan to add time to a date

today.Add(time)

‘ or

today += time

‘ timespan represented as Days.Hours:Minutes:Seconds:

‘ Subseconds

time = TimeSpan.Parse(“1.2:3:4:5″)

‘ or

If TimeSpan.TryParse(Console.ReadLine(), time) Then

Console.WriteLine(time)

End If

 

As always, if there are any questions or suggestions, they are welcome. Thank you.

Leave a Reply