Skip to main content
Pelajar Koding

follow us

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: Char Description (:) 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. d Date format without zero prefix. e.g. 1, 3, 5, 10. Use "%d" dd Date format with zero prefix. e.g. 01, 03, 05, 10. ddd Abbreviation of day name in English. e.g. Sun, Mon, Tue, etc. dddd Full day name in English. e.g.  Sunday , Monday, Tuesday, etc M Month number without zero prefix. e.g. 1, 3, 5, 10. Use "%M" MM Month number with zero prefixes. (e.g. 01, 03, 05, 10) MMM Abbreviation of month name in English. e.g: Jan, Feb, Mar, etc MMMM Full month name in English. e.g: January , February, March, etc gg To display the masa/period (e.g.  A.D. ). h Display 12 hours format without zero prefix. (e.g.  1:15:15 PM ). Use "%h". hh Display 12 hours format with zero prefix. (e.g.  01:15:15 PM ). H Display 24 hours format without zero prefix. (e.g.  13:15:15 ). Use "%H" HH Display 24 hours format with zero prefix. (e.g. 02 :10:15 ). m Minutes without zero prefix. (e.g.: 12:1:15). Use "%m" mm Minutes with zero prefix. (e.g: 12:01:15) s Second without zero prefix. (e.g: 12:11:5). Use "%s" ss Second with zero prefix. (e.g.  12:11:05). f f would display 1/10 second, ff 1/100, fff 1/1000,  and so on. Use "%f " for 1/10 t In 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. tt In 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. y Displays the year in the last 2 digits without zero for the numbers 0-9. Use "%y" if using a 1 letter format. yy Displays the year in the last 2 digits with zero for the numbers 0-9. yyy Display 4 digits of year. yyyy Display 4 digits of year. z Format to display time zone differences that are used without zero prefixes for differences below 10. Use "%z" to format using 1 letter. Example: +7 zz The format for displaying the time zone difference used zero prefixes for differences below 10. Example: +07 zzz Format 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
Sumber http://rani-irsan.blogspot.com

You Might Also Like:

Comment Policy: Silahkan tuliskan komentar Anda yang sesuai dengan topik postingan halaman ini. Komentar yang berisi tautan tidak akan ditampilkan sebelum disetujui.
Buka Komentar