VB.NET: Working with Date and Time in Visual Basic .NET

Years of experience in application programming, I always have to deal with dates and times formating. I want to compile details about this complete with examples of their use.

Creating DateTime Object.
In VB.NET there is data type named DateTime. When we declare this type of variable without assigning any value, the default value will be 1/1/0001 12:00 AM.

Dim dt As DateTime
MsgBox(dt.ToShortDateString & " " & dt.ToShortTimeString, , "Message")


Getting Current Date and Time
How to get current date and time? We can use methods "Time" and "Today". The following is an example of its use.

Dim strDate As String = Today
Dim strTime As String= Now
MsgBox("Current Date : " & strDate & vbCrLf & _
       "Current Time: " & strTime , , "Message")




If we want to display time only (without date), this is an example:
MsgBox("Current time: " & Now.ToLongTimeString, , "Message")





Date and Time Format
We can also format the DateTime variable with the existing format list as below sample.

 Dim strDateTime As String = String.Empty
strDateTime = strDateTime & "Format G :" & Format(Now, "G") & vbCrLf
strDateTime = strDateTime & "Format D :" & Format(Now, "D") & vbCrLf
strDateTime = strDateTime & "Format d :" & Format(Now, "d") & vbCrLf
strDateTime = strDateTime & "Format T :" & Format(Now, "T") & vbCrLf
strDateTime = strDateTime & "Format t :" & Format(Now, "t") & vbCrLf
strDateTime = strDateTime & "Format F :" & Format(Now, "F") & vbCrLf
strDateTime = strDateTime & "Format f :" & Format(Now, "f") & vbCrLf
strDateTime = strDateTime & "Format M :" & Format(Now, "M") & vbCrLf
strDateTime = strDateTime & "Format m :" & Format(Now, "m") & vbCrLf
strDateTime = strDateTime & "Format R :" & Format(Now, "R") & vbCrLf
strDateTime = strDateTime & "Format r :" & Format(Now, "r") & vbCrLf
strDateTime = strDateTime & "Format s :" & Format(Now, "s") & vbCrLf
strDateTime = strDateTime & "Format U :" & Format(Now, "U") & vbCrLf
strDateTime = strDateTime & "Format u :" & Format(Now, "u") & vbCrLf
strDateTime = strDateTime & "Format Y :" & Format(Now, "Y") & vbCrLf
strDateTime = strDateTime & "Format y :" & Format(Now, "y") & vbCrLf
MsgBox(strDateTime, , "Message")




Visual Basic .NET allow us to manually format DateTime Variable, using the following character:

CharDescription
(:)Time separator hours, minutes and seconds. Example: 15:32:01
(/)Date separator. Example: 11/24/2015
(%)A prefix that used for formats that only use 1 letter.
Example: The "d" format will return 11/24/2015, while "%d" will be 24.
dDate format without zero prefix. e.g. 1, 3, 5, 10. Use "%d"
ddDate format with zero prefix. e.g. 01, 03, 05, 10.
dddAbbreviation of day name in English. e.g. Sun, Mon, Tue, etc.
ddddFull day name in English. e.g. Sunday, Monday, Tuesday, etc
MMonth number without zero prefix. e.g. 1, 3, 5, 10. Use "%M"
MMMonth number with zero prefixes. (e.g. 01, 03, 05, 10)
MMMAbbreviation of month name in English. e.g: Jan, Feb, Mar, etc
MMMMFull month name in English. e.g: January, February, March, etc
ggTo display the era/period (e.g. A.D.).
hDisplay 12 hours format without zero prefix. (e.g. 1:15:15 PM). Use "%h".
hhDisplay 12 hours format with zero prefix. (e.g. 01:15:15 PM).
HDisplay 24 hours format without zero prefix. (e.g. 13:15:15). Use "%H"
HHDisplay 24 hours format with zero prefix. (e.g. 02:10:15).
mMinutes without zero prefix. (e.g.: 12:1:15). Use "%m"
mmMinutes with zero prefix. (e.g: 12:01:15)
sSecond without zero prefix. (e.g: 12:11:5). Use "%s"
ssSecond with zero prefix. (e.g. 12:11:05).
ff would display 1/10 second, ff 1/100, fff 1/1000,  and so on. Use "%f " for 1/10
tIn the 12 hour format, it will display A for the time of 12 p.m. to 11:59 p.m. And P for 12 noon until 11:59 p.m. Use "%t" if only using 1 letter format.
ttIn the 12 hour format, it will display AM for the time of 12 p.m. to 11:59 p.m. And PM for 12 noon until 11:59 p.m. If placed in a 24 hours format it will not be displayed.
yDisplays the year in the last 2 digits without zero for the numbers 0-9. Use "%y" if using a 1 letter format.
yyDisplays the year in the last 2 digits with zero for the numbers 0-9.
yyyDisplay 4 digits of year.
yyyyDisplay 4 digits of year.
zFormat to display time zone differences that are used without zero prefixes for differences below 10. Use "%z" to format using 1 letter. Example: +7
zzThe format for displaying the time zone difference used zero prefixes for differences below 10. Example: +07
zzzFormat for displaying time zone differences in time display hh: mm. Example: +07: 00

Example:
Dim strDateTime As String = String.Empty
strDateTime = strDateTime & "Format M/d/yy :" & Format(Now, "M/d/yy") & vbCrLf
strDateTime = strDateTime & "Format d-MMM :" & Format(Now, "d-MMM") & vbCrLf
strDateTime = strDateTime & "Format d-MMMM-yy :" & Format(Now, "d-MMMM-yy") & vbCrLf
strDateTime = strDateTime & "Format d MMMM :" & Format(Now, "d MMMM") & vbCrLf
strDateTime = strDateTime & "Format MMMM yy :" & Format(Now, "MMMM yy") & vbCrLf
strDateTime = strDateTime & "Format hh:mm tt :" & Format(Now, "hh:mm tt") & vbCrLf
strDateTime = strDateTime & "Format h:mm:ss t :" & Format(Now, "h:mm:ss t") & vbCrLf
strDateTime = strDateTime & "Format H:mm :" & Format(Now, "H:mm") & vbCrLf
strDateTime = strDateTime & "Format H:mm:ss :" & Format(Now, "H:mm:ss") & vbCrLf
strDateTime = strDateTime & "Format M/d/yyyy H:mm :" & Format(Now, "M/d/yyyy H:mm")
MsgBox(strDateTime, , "Message")




1/1/0001, 12:00 AM

Post a Comment

0 Comments