Working With Date and Time

Here are some good tools:
(original on vbcity)

At some point, it seems that everyone wants or needs to work with Dates and Time. In this FAQ we will take a look at Date and Time validation as well as adding and subtracting to the various Date/Time intervals.

Dates

  1. Validating Dates

While there are many different ways of validating dates, I am just going to touch on two, DateTime.Parse and using Regular Expressions.

DateTime.Parse

Using DateTime.Parse is probably one of the easiest ways of checking if the user has input a valid Date or not.

Dim MyDate As System.DateTime 

Try 
    MyDate = System.DateTime.Parse(TextBox1.Text) 
    MessageBox.Show("That is a valid Date", Me.Text, MessageBoxButtons.OK) 
Catch ex As Exception 
    MessageBox.Show("That is not a valid Date", Me.Text, MessageBoxButtons.OK) 
End Try 

Regular Expressions

Using Regular Expressions are a little harder to code. (Many thanks go to Hamburger1984 and Hotdog for their invaluable knowledge and assistance with this part of the FAQ.)

Note: The Regular Expression code in this FAQ only shows you how to check the date in one Format that being Day/Month/Year. To learn more about Regular Expressions check out this FAQ by Hamburger1984.

If you plan on using Regular Expressions to validate Dates, you will need to import the System.Text.RegularExpressions NameSpace

Imports System.Text.RegularExpressions 
  
  
If Not Regex.Match(TextBox3.Text, "\A(\d/([0-2]\d)/(3[01]))/(\d/(0\d)/(1[0-2]))/(\d{4})\Z").Success Then 
    MessageBox.Show("Your Date must be in the DD/MM/YYYY Format.") 
Else 
    MessageBox.Show("That is a good Date.") 
End If 

(Note: The Forward slashes (/) used in these places:
(\d/([0-2]\d)/(3[01]))
(\d/(0\d)/(1[0-2]))
should be the Pipe (straight up and down line) character.)

  1. Adding/Subtracting Days, Months and Years

This FAQ will just show adding and subtracting dates from the current date, but you can add/subtract from any date that you choose.

a) Adding/Subtracting Days

To add or subtract days, you need to call the AddDays Function.

Dim MyDate As System.DateTime 

MyDate = System.DateTime.Today.AddDays(10) ' Set the number of days that you want to add.

or if you want to use just one line of code;

Dim MyDate As System.DateTime = System.DateTime.Today.AddDays(10) 

I know this sounds silly, but to subtract days, you still call the AddDays Function. The difference is that you enter a negative number of days that you subtract.

Dim MyDate As System.DateTime = System.DateTime.Today.AddDays(-10) ' Set the number of days that you want to subtract.

b) Adding/Subtracting Months

Adding/Subtracting Months is virtually identical to what we did with Days, you just use the AddMonths instead of AddDays.

Dim MyDate As System.DateTime = System.DateTime.Today.AddMonths(3) 

' or to subtract Months 

Dim MyDate As System.DateTime = System.DateTime.Today.AddMonths(-4) 

c) Adding/Subtracting Years

Once again adding/subtracting Years is similar to adding Days and Months. This time you use AddYears.

Dim MyDate As System.DateTime = System.DateTime.Today.AddYears(3) 

' or to subtract Years 

Dim MyDate As System.DateTime = System.DateTime.Today.AddYears(-5) 

Time

  1. Validating Times

Regular Expressions

As with validating Dates, Regular Expressions can be used to validate Times. (This Regular Expression will check Time in a 12 hour Format)

Note: The Regular Expression code in this FAQ only shows you how to check the time in one Format. (Hours:Minutes:Seconds) To learn more about Regular Expressions check out this FAQ by Hamburger1984.

As always with using Regular Expressions you must Import the System.Text.RegularExpressions NameSpace

Imports System.Text.RegularExpressions 

If Not Regex.Match(TextBox4.Text, "\A(\d/(0\d)/(1[0-2])) : (\d/([0-5]\d)) : (\d/([0-5]\d))\Z").Success Then 
    MessageBox.Show("Your Time must be a valid Time and in the HH:MM:SS Format. ", Me.Text, MessageBoxButtons.OK) 
Else 
    MessageBox.Show("That is a good Time. ", Me.Text, MessageBoxButtons.OK) 
End If 

(Note: To use this regular Expression, you should remove the spaces I put on either side of the :
I had to add the spaces to keep from having 's in the code.)
(Note: The Forward slashes (/) used in these places:
(\d/(0\d)/(1[0-2]))
(\d/([0-5]\d))
(\d/([0-5]\d))
should be the Pipe (straight up and down line) character.)

  1. Adding/Subtracting Hours, Minutes and Seconds

Like the examples of adding/subtracting Dates, this FAQ will just show adding and subtracting Times from the current System Time, but you can add/subtract from any Time that you choose.

a) Adding/Subtracting Hours

To add or subtract Hours, you need to call the AddHours Function.

Dim MyTime As System.DateTime 

MyTime = TimeString ' Get the System Time that hours are going to be added. 
MyTime = MyTime.AddHours (10) ' Set the number of hours that you want to add. 

As with Dates to subtract Time, you still call the AddHours Function. The difference is that you enter a negative number of hours that you subtract.

Dim MyTime As System.DateTime 

MyTime = TimeString ' Get the System Time that hours are going to be subtracted. 
MyTime = MyTime.AddHours (-5) ' Set the number of hours that you want to subtract. 

b) Adding/Subtracting Minutes

Adding/Subtracting Minutes is virtually identical to what we did with Hours, you just use the AddMinutes instead of AddHours.

Dim MyTime As System.DateTime 

MyTime = TimeString 
MyTime = MyTime.AddMinutes (3) 

' or to subtract Minutes 

Dim MyTime As System.DateTime 

MyTime = TimeString 
MyTime = MyTime.AddMinutes (-4) 

c) Adding/Subtracting Seconds

Once again adding/subtracting Seconds is similar to adding Hours and Minutes. This time you use AddSeconds.

Dim MyTime As System.DateTime 

MyTime = TimeString 
MyTime = MyTime.AddSeconds (3) 

' or to subtract Seconds 

Dim MyTime As System.DateTime 

MyTime = TimeString 
MyTime = MyTime.AddSeconds (-5) 

  1. Number of days in a Month

I am sure that most of you were taught the 30 days has September, April, June and November rhyme when you were in school. (Not that I could even remember it at all. I had to use the knuckle method of figuring out days in months.J) Let’s not even get started on February and which year is a leap year.

The System.DateTime Namespace has a built in Function to determine the number of days in a user selected month. The Function is DaysInMonth. You use DaysInMonth with the Parameters Year, Date

System.DateTime.DaysInMonth(Year,Month) 

(For this I populated a ComboBox with the months to choose from and a TextBox for year.)

Dim MyMonth As Integer 
Dim MyYear As Integer 

MyYear = CInt(TextBox1.Text) 

Dim MyDays As Integer 

MyDays = System.DateTime.DaysInMonth(MyYear, ComboBox1.SelectedIndex + 1) 

MessageBox.Show(MyDays) 

TextBox1.Text = "" 
ComboBox1.Text = "Select a Month" 

End Sub