WHAT'S NEW?
Loading...

Date and time format in VB.Net



A standard date and time format string uses a single format specifies to define the text representation of a date and time value. Any date and time format string that contains more than one character, including white space, is interpreted as a custom date and time format string. More about standard date and time format string visit MSDN Library.

Here are the following codes:

Example 1
Dim time As DateTime = DateTime.Now
        Dim format As String = "MMM " & "," & "ddd d HH:mm yyyy"
        MessageBox.Show(time.ToString(format))
Output: May, Tue 18 16:46 2010
Example 1
Dim now As DateTime = DateTime.Now

        MessageBox.Show(now.ToString("d"))
        MessageBox.Show(now.ToString("D"))
        MessageBox.Show(now.ToString("f"))
        MessageBox.Show(now.ToString("F"))
        MessageBox.Show(now.ToString("g"))
        MessageBox.Show(now.ToString("G"))
        MessageBox.Show(now.ToString("m"))
        MessageBox.Show(now.ToString("M"))
        MessageBox.Show(now.ToString("o"))
        MessageBox.Show(now.ToString("O"))
        MessageBox.Show(now.ToString("s"))
        MessageBox.Show(now.ToString("t"))
        MessageBox.Show(now.ToString("T"))
        MessageBox.Show(now.ToString("u"))
        MessageBox.Show(now.ToString("U"))
        MessageBox.Show(now.ToString("y"))
        MessageBox.Show(now.ToString("Y"))

Output:

5/18/2010
Tuesday, May 18, 2010
Tuesday, May 18, 2010 4:47 PM
Tuesday, May 18, 2010 4:47:55 PM
5/18/2010 4:47 PM
5/18/2010 4:47:55 PM
May 18
May 18
2010-05-18T16:47:55.9620000-06:00
2010-05-18T16:47:55.9620000-06:00
2010-05-18T16:47:55
4:47 PM
4:47:55 PM
2010-05-18 16:47:55Z
Tuesday, May 18, 2010 10:47:55 PM
May, 2010
May, 2010

Example 3
Dim now As DateTime = DateTime.Now

        MessageBox.Show(now.ToLongDateString())
        MessageBox.Show(now.ToLongTimeString())
        MessageBox.Show(now.ToShortDateString())
        MessageBox.Show(now.ToShortTimeString())
        MessageBox.Show(now.ToString())
Output:

Tuesday, May 18, 2010
4:49:57 PM
5/18/2010
4:49 PM
5/18/2010 4:49:57 PM
 
Example 3
Dim now As DateTime = DateTime.Now

        ' Print out all the days.
        For index As Integer = 0 To 6
            MessageBox.Show(now.ToString("ddd"))
            now = now.AddDays(1)
        Next
Output:

Tue
Wed
Thu
Fri
Sat
Sun
Mon

Example 4
Dim now As DateTime = DateTime.Now
        For index As Integer = 0 To 6
            MessageBox.Show(now.ToString("dddd"))
            now = now.AddDays(1)
        Next
Output:

Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Monday

Example 5
  Dim now As DateTime = DateTime.Now
        For index As Integer = 0 To 1
            MessageBox.Show(now.ToString("tt "))
            now = now.AddHours(12)
        Next
Output:

PM
AM

Example 6
   Dim now As DateTime = DateTime.Now
        MessageBox.Show(now.ToString("yy"))
        MessageBox.Show(now.ToString("yyyy"))
Output:

10
2010

The following are examples of user-defined date and time formats for December 7, 1958, 8:50 PM, 35 seconds.

Format
Displays
M/d/yy
12/7/58
d-MMM
7-Dec
d-MMMM-yy
7-December-58
d MMMM
7 December
MMMM yy
December 58
hh:mm tt
08:50 PM
h:mm:ss t
8:50:35 P
H:mm
20:50
H:mm:ss
20:50:35
M/d/yyyy H:mm
12/7/1958 20:50
















You can use a variety of format characters on the DateTime type. For more information, visit MSDN Library.

0 comments:

Post a Comment