Date Methods and Format Function

Objectives


Discussion

Date Functions and Methods

Format Function

Back to top


Demonstration

In this demo we will explore some of the many date methods and functions. 

1.  Add a form to your Unit 6 project.

2.  Add a DateTimePicker control to your form.  Add a button.  In the click event for the button, type the following and scroll through the list of methods for the date variable dtp1.  We will explore only a few of these.

Dim dtp1 As Date
dtp1.

3.  Now type in the following.  Check out the various overloaded options and the information that this function is expecting. 

DateDiff(

4.  Now delete these three lines of code and add the following code to the click event:

Dim dtp1 As Date
dtp1 = Me.DateTimePicker1.Value
MessageBox.Show(DateDiff(DateInterval.Day, dtp1, Now))
MessageBox.Show(dtp1.AddDays(180))
MessageBox.Show(dtp1.DayOfYear())
MessageBox.Show(dtp1.DayOfWeek)
MessageBox.Show(dtp1.Year)
MessageBox.Show(dtp1.ToLongTimeString)

5.  Run the program, click a date in the datetimepicker, and carefully compare the messageboxes with the code that generates them.  Make sure you understand what each method is doing.  Notice that DateDiff determines the time interval between two dates depending on the DateInterval argument. 

6.  Now comment out the code in the click event and add the following two lines of code:

MessageBox.Show(Format(Now, "dddd, MM d yyyy"))
MessageBox.Show(Format(6.4, "$##.00"))

7.  Run the program.  Notice that you can use the format function for many purposes.  There are many examples in Help. Just look up "Format".  You really should check out this article in Help.

 Back to top
Exercises

1.  Create a program that lets the user:

  1. Select a date in a MonthCalender or DateTimePicker.
  2. Select a value in a NumericUpDown control.
  3. Select either "Day", "Month", or "Year" in a ListBox or ComboBox.
  4. Click a command button to display the date that is the number of days, months, or years (as selected in the ListBox or ComboBox) away from the date selected in the MonthCalender or DateTimePicker.

For example, if the user selects 11/11/02, selects 25 in the NumericUpDown control, and selects "Day" in the ListBox/ComboBox then the date 12/6/2002 should be displayed.

2. Modify your program in #1 so that when the user selects a date in the DateTimePicker or MonthCalender, the day of week (Sunday, Monday,...) of the selected date is displayed in a label.

3.  Modify your program in #1 so that when the user selects a date, the day of the year of that date is displayed in a label.  For example, if the user selects 11/11/02, then 315 should be displayed in the label since 11/11/02 is the 315th day of 2002.

4.  Write a line of code to format a string of numbers as a social security number in the following form: "111-22-3333".  Write a simple program to test your code.

5.  Write a line of code to format a date as "Friday, Dec 13 2002".  Test your code in a program.

Back to top
Links & Help
Back to top