2011年9月15日 星期四

vb.net 格式化日期成為書寫格式

轉載這篇
http://www.dotnetperls.com/datetime-format-vbnet

The DateTime type can be formatted with a string pattern in the VB.NET language. Conceptually, this is simple, but in practice there are some details you must account for if you want a functional program. As shown in this article, formatting your DateTime as a string is straightforward with an understanding of the syntax.

Example 1

As we begin, please notice how the current DateTime is acquired through the DateTime.Now property; when you execute these code examples, the current DateTime will be different. In this example, a format string beginning with MMM (for the month) is used. Look at how the pattern matches up to the output of the program.
Program that uses format string with DateTime [VB.NET]

Module Module1
Sub Main()
' Use current time.
' ... Use a format.
' ... Write to console.
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))
End Sub
End Module

Output

May Tue 18 16:46 2010

Description of format

MMM Three-letter month.
ddd Three-letter day of the week.
d Day of the month.
HH Two-digit hours on 24-hour scale.
mm Two-digit minutes.
yyyy Four-digit year.
Example 2

It is possible to change the pattern used in the formatting string in many different ways. To explore this, we try to shorten the output by changing the MMM to a single M, along with several other similar changes. Again, notice how the pattern matches up to the output of the program. Further, the description of the formatting string is shown in tabular format.
Another program that uses format string [VB.NET]

Module Module1
Sub Main()
' Use current time.
' ... Use a format.
' ... Write to console.
Dim time As DateTime = DateTime.Now
Dim format As String = "M d HH:mm yy"
Console.WriteLine(time.ToString(format))
End Sub
End Module

Output

5 18 16:46 10

Description of format

M One-digit month number.
d One-digit day of the month.
HH Two-digit hours on 24-hour scale.
mm Two-digit minutes.
yy Two-digit year.
One-character format strings

Another feature of the DateTime formatting mechanism in VB.NET, which is accessible through the ToString function, is the single-character format string. For this feature, you pass a one-character string to the ToString function; this encodes a specific formatting style. Sometimes, it might be worth memorizing the ones you like the most, but in other cases you can simply look up the table on the Internet or MSDN.
Program that uses one-character formats [VB.NET]

Module Module1
Sub Main()
' Acquire current time and then try format strings.
Dim now As DateTime = DateTime.Now
Console.WriteLine(now.ToString("d"))
Console.WriteLine(now.ToString("D"))
Console.WriteLine(now.ToString("f"))
Console.WriteLine(now.ToString("F"))
Console.WriteLine(now.ToString("g"))
Console.WriteLine(now.ToString("G"))
Console.WriteLine(now.ToString("m"))
Console.WriteLine(now.ToString("M"))
Console.WriteLine(now.ToString("o"))
Console.WriteLine(now.ToString("O"))
Console.WriteLine(now.ToString("s"))
Console.WriteLine(now.ToString("t"))
Console.WriteLine(now.ToString("T"))
Console.WriteLine(now.ToString("u"))
Console.WriteLine(now.ToString("U"))
Console.WriteLine(now.ToString("y"))
Console.WriteLine(now.ToString("Y"))
End Sub
End Module

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
Helper string functions

There are also several helper functions available on a DateTime instance that allow you to format a string in a specific way. For rapid application development, these are ideal because they are easy to remember and guess. Notice how the methods with the word Long in them produce considerably more verbose output.
Program that uses DateTime string functions [VB.NET]

Module Module1
Sub Main()
' Use string helper functions.
Dim now As DateTime = DateTime.Now
Console.WriteLine(now.ToLongDateString())
Console.WriteLine(now.ToLongTimeString())
Console.WriteLine(now.ToShortDateString())
Console.WriteLine(now.ToShortTimeString())
Console.WriteLine(now.ToString())
End Sub
End Module

Output

Tuesday, May 18, 2010
4:49:57 PM
5/18/2010
4:49 PM
5/18/2010 4:49:57 PM
Short day strings

The .NET Framework gives you the ability to print the representation of a day in short form. The three-letter form used simply takes a substring of the day in English and truncates it to three characters. In this example, we look at a loop construct that loops through seven days and prints out the short form string for each of them.
Program that prints short day strings [VB.NET]

Module Module1
Sub Main()
' Current time.
Dim now As DateTime = DateTime.Now

' Print out all the days.
For index As Integer = 0 To 6
Console.WriteLine(now.ToString("ddd"))
now = now.AddDays(1)
Next
End Sub
End Module

Output

Tue
Wed
Thu
Fri
Sat
Sun
Mon
Long day strings

You can also print out the long day strings using the format string dddd. For more textually-oriented output, the longer day string might seem more professional and authoritative. In this example, we loop over seven consecutive days and print out the long day strings.
Program that prints long day strings [VB.NET]

Module Module1
Sub Main()
Dim now As DateTime = DateTime.Now
For index As Integer = 0 To 6
Console.WriteLine(now.ToString("dddd"))
now = now.AddDays(1)
Next
End Sub
End Module

Output

Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Monday
AM and PM strings

When you are using a 12-hour based time system, you will typically want to display AM or PM to indicate whether it is morning or evening. This example prints out AM and PM for two times that are twelve hours apart, demonstrating the syntax and correctness of this format string.
Program that prints AM and PM [VB.NET]

Module Module1
Sub Main()
Dim now As DateTime = DateTime.Now
For index As Integer = 0 To 1
Console.WriteLine(now.ToString("tt "))
now = now.AddHours(12)
Next
End Sub
End Module

Output

PM
AM
Year strings

The year part of the format string is also very important. Typically, you will want a two or four digit year, but a one-digit year is also possible with a single y. To exemplify this format character, we look at some output in an example program that tests yy and yyyy.
Program that prints year strings [VB.NET]

Module Module1
Sub Main()
' Use the best y patterns.
Dim now As DateTime = DateTime.Now
Console.WriteLine(now.ToString("yy"))
Console.WriteLine(now.ToString("yyyy"))
End Sub
End Module

Output

10
2010

沒有留言:

張貼留言