Visual Basic

Dates and Times

Dealing with dates and times is a bit more complex than you may think. VB provides many helpful functions that handle date and time related processes. For declaring date or time related variables, use Date data type.

For example:

Dim dtmOpeningTime As Date

* One thing to point out is that the return values from Dates and Times functions may be either Date data type or other data types such as string and integer. If you assign the return values into a variable, make sure the the variable has the appropriate data type depending on the value that is being returned from the function. One common mistake could be assigning an returning value in integer to a Date variable - it causes conversion of the integer into a date and time.

 

Here are some sample codes of basic date and time functions:

Private Sub Command1_Click( )
Picture1.Print "Current date and time: " & Now
Picture1.Print "Today's Date in Full: " & Date
Picture1.Print "Today's Date Only: " & Day(Date)
Picture1.Print "Weekday Number: " & Weekday(Date)
Picture1.Print "Weekday Name (Today's Day ): " & WeekdayName(Weekday(Date))
Picture1.Print "Weekday Name Abbreviated: " & WeekdayName(Weekday(Date), True)
Picture1.Print "Month Number: " & Month(Date)
Picture1.Print "Month Name: " & MonthName(Month(Date))
Picture1.Print "Month Name Abbreviated: " & MonthName(Month(Date), True)
Picture1.Print "Year: " & Year(Date)
Picture1.Print #3/12/2003#
End Sub

Form | Project

Sometimes, you want to use a portion of the Now string. Here are some examples using DatePart function:

Private Sub Command1_Click( )
Picture1.Print "Current date and time: "; Now
Picture1.Print "Year: " & DatePart("yyyy", Now)
Picture1.Print "Quarter: " & DatePart("q", Now)
Picture1.Print "Month: " & DatePart("m", Now)
Picture1.Print "Day of the Year: " & DatePart("y", Now)
Picture1.Print "Day of the Month: " & DatePart("d", Now)
Picture1.Print "Weekday: " & DatePart("w", Now)
Picture1.Print "Week of the Year: " & DatePart("ww", Now)
Picture1.Print "Hour: " & DatePart("h", Now)
Picture1.Print "Minute: " & DatePart("n", Now)     'It's not m
Picture1.Print "Second: " & DatePart("s", Now)
End Sub

Form | Project

Now, you may wander how do I change the time? Or how do I find the difference between two times? Use DateAdd and DateDiff functions.

Private Sub Command1_Click( )
Picture1.Print "Current date and time (Now): "; Now
Picture1.Print "Now + 5 years: " & DateAdd("yyyy", 5, Now)
Picture1.Print "Now + 5 days: " & DateAdd("d", 5, Now)
Picture1.Print "Now + 5 hours: " & DateAdd("h", 5, Now)
Picture1.Print "Days between now and 12/31/02: " & DateDiff("d", Now, "12/31/02")
Picture1.Print "Days between 7/1/02 and 12/31/02: " & DateDiff("d", "7/1/02", "12/31/02")
Picture1.Print "Hours between 8 AM and 5 PM: " & DateDiff("h", "8:00:00", "17:00:00")
Picture1.Print "Minutes between 8 AM and 5 PM: " & DateDiff("n", "8:00:00", "17:00:00")
End Sub

Form | Project

For particular part of the time, you have a few function to utilize. They are: Time, Hour, Minute, Second, Timer, TimeSerial, and TimeValue functions. The Timer function is different from the Timer control. To find more about Timer control, refer to "Timer control" and "Timer control's Timer event interval" in Help.

Private Sub Command1_Click( )
Picture1.Print "Current date and time (Now): "; Now
Picture1.Print "Time: " & Time
Picture1.Print "Hour: " & Hour(Time)
Picture1.Print "Minute: " & Minute(Time)
Picture1.Print "Second: " & Second(Time)
Picture1.Print "Seconds since the midnight : " & Timer
Picture1.Print "TimeSerial(0,0,31): " & TimeSerial(0, 0, 31)
Picture1.Print "TimeSerial(12,30,0): " & TimeSerial(12, 30, 0)
Picture1.Print "5:55 as a Time is: " & TimeValue("5:55AM")
Picture1.Print "15:55 as a Time is: " & TimeValue("15:55AM")
End Sub

Form | Project

To display the date and time, you may use either Format or FormatDateTime function.

Private Sub Command1_Click( )
Picture1.Print "Current date and time (Now): "; Now
Picture1.Print "vbGeneralDate: " & FormatDateTime(Now, vbGeneralDate)
Picture1.Print "vbLongDate: " & FormatDateTime(Now, vbLongDate)
Picture1.Print "vbLongTime: " & FormatDateTime(Now, vbLongTime)
Picture1.Print "vbShortDate: " & FormatDateTime(Now, vbShortDate)
Picture1.Print "vbShortTime: " & FormatDateTime(Now, vbShortTime)

Picture1.Print "General Date: " & Format(Now, "General Date")
Picture1.Print "Long Date: " & Format(Now, "Long Date")
Picture1.Print "Medium Date: " & Format(Now, "Medium Date")
Picture1.Print "Short Date: " & Format(Now, "Short Date")
Picture1.Print "Long Time: " & Format(Now, "Long Time")
Picture1.Print "Medium Time: " & Format(Now, "Medium Time")
Picture1.Print "Short Time: " & Format(Now, "Short Time")
End Sub

Form | Project

You may use Format function to create your own Date and Time formats.

Private Sub Command1_Click( )
Picture1.Print "Current date and time (Now): "; Now
Picture1.Print "m/d/yy: " & Format(Now, "m/d/yy")
Picture1.Print "d-mmm-yy: " & Format(Now, "d-mmm-yy")
Picture1.Print "d-mmmm-yy: " & Format(Now, "d-mmmm-yy")
Picture1.Print "mmmm d, yyyy: " & Format(Now, "mmmm d, yyyy")
Picture1.Print "ddd: " & Format(Now, "ddd")
Picture1.Print "dddd: " & Format(Now, "dddd")
Picture1.Print "ddddd: " & Format(Now, "ddddd")
Picture1.Print "dddddd: " & Format(Now, "dddddd")

Picture1.Print "Hh:Nn:Ss AM/PM: " & Format(Now, "Hh:Nn:Ss AM/PM")
Picture1.Print "ttttt: " & Format(Now, "ttttt")

Picture1.Print "c: " & Format(Now, "c")
End Sub

Form | Project

 

 

 

The Timer Function: It returns a Single representing the number of seconds elapsed since midnight.

This example uses the Timer function to pause the application. The example also uses DoEvents to yield to other processes during the pause.

Private Sub Command1_Click( )
Dim PauseTime, Start, Finish, TotalTime
If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
   PauseTime = 5    ' Set duration.
   Start = Timer    ' Set start time.
   Do While Timer < Start + PauseTime    ' Timer increments its value automatically.
      DoEvents ' Yield to other processes.
   Loop
   Finish = Timer ' Set end time.
   TotalTime = Finish - Start    ' Calculate total time.
   MsgBox "Paused for " & TotalTime & " seconds"
Else
   End
End If
End Sub

      
Form | Project