Date and Time

 

I have shown some date and time handling functions such as Datepart and etc. Today, I will show you other functions that can show and calculate date and time, such as adding up and subtracting date and time. Here are the VBA date and time handling functions:

 

 

 

Private Sub CommandButton1_Click()
Cells(2, 2) = Now()
Cells(3, 2) = Format(Now, "s")
Cells(4, 2) = Format(Now, "n")
Cells(5, 2) = Format(Now, "h")
Cells(6, 2) = Format(Now, "d")
Cells(7, 2) = Format(Now, "mmm")
Cells(8, 2) = Format(Now, "mmmm")
Cells(9, 2) = Format(Now, "yyyy")
Cells(10, 2) = DateValue(Now)
Cells(11, 2) = DateValue(Now + 3)
Cells(12, 2) = DateDiff("d", "Feb 21,2008", "Mar 1,2008")
Cells(13, 2) = DateDiff("m", "Feb 21,2008", "Sep 1,2008")
End Sub

 



 

Explanations

 

The function  Now() will show the date and time

The function Format(Now, "s") will show the time in seconds

The function Format(Now, "n") will show the time in minutes

The function Format(Now, "h") will show the time in hour

The function Format(Now, "d") will show the date in day

The function Format(Now, "mmm") will show the month  in three character

The function Format(Now, "mmmm") will show the month  in full name

The function DateValuet(Now) will show the date according to your computer's setting

The function DateValuet(Now+3) will show the date 3 days from now

The function DateDiff("d", "Feb 21,2008", "Mar 1,2008") will show the number of days between the two dates.

The function DateDiff("m", "Feb 21,2008", "Sep 1,2008") will show the number of months between the two dates.

The outputs are shown in the figure below:

 

 

 

 

 

 

 

 [Back to VBToday]