格式字符串 結果
{0:d} YY-MM-DD
{0:p} 百分比00.00%
{0:N2} 12.68
{0:N0} 13
{0:c2} $12.68
{0:d} 3/23/2003
{0:T} 12:00:00 AM
{0:男;;女}
格式字符串 資料 結果
"{0:C}" 12345.6789 -> $12,345.68
"{0:C}" -12345.6789 -> ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 -> 00012345
"{0:E}" 12345.6789 -> 1234568E+004
"{0:E10}" 12345.6789 -> 1.2345678900E+004
"{0:F}" 12345.6789 -> 12345.68
"{0:F0}" 12345.6789 -> 12346
"{0:G}" 12345.6789 -> 12345.6789
"{0:G7}" 123456789 -> 1.234568E8
"{0:N}" 12345.6789 -> 12,345.68
"{0:N4}" 123456789 -> 123,456,789.0000
"Total: {0:C}" 12345.6789 -> Total: $12345.68
格式字符串
1、常用的數值格式字符串:
C或c 貨幣格式
D或d 十進制格式
E或e 科學(指數)格式
F或f 固定小數位格式
G或g 一般格式
N或n 數字格式
P或p 百分比格式
X或x 十六進制格式
2、常用的日期格式字符串:
d 短日期
D 長日期
f 完整(長日期和短時間)
F 完整(長日期和長時間)
g 一般(短日期和短時間)
G 一般(短日期和長時間)
M或m 月和日
R或r RFC1123格式
s 使用本地時間的ISO8601可分類
t 短時間
T 長時間
u 使用通用時間的ISO8601可分類
U 通用可分類日期/時間
Y或y 年和月
3、數字的圖片格式字符
0 沒有有效值則顯示0
# 數字占位符
. 小數點
, 分隔符
% 百分比
E+0,E-0,e+0或e-0 將輸出格式為科學或指數含義
\ 轉義字符
"或' 它們之間的任何字符都被解釋為字符串
{和} 用于顯示單文字性花括號,如"{{"顯示為"{"
;分隔格式字符串中正、負和零值的兩個或三個部分
標準 DateTime 格式字符串包含下表中的一個格式說明符字符。如果下表中沒有該格式說明符,將引發(fā)運行時異常。如果格式字符串在長度上比單個字符長(即使多出的字符是空白),則格式字符串被解釋為自定義格式字符串。
請注意,這些格式說明符產生的輸出字符串受“區(qū)域選項”控制面板中的設置的影響。計算機的區(qū)域性設置或日期和時間設置不同,將生成不同的輸出字符串。
格 式字符串顯示的時間和日期分隔符由與當前區(qū)域性的 DateTimeFormat 屬性關聯的 DateSeparator 和 TimeSeparator 字符定義。然而,如果 InvariantCulture 被“r”、“s”和“u”說明符引用,與 DateSeparator 和 TimeSeparator 字符關聯的字符不隨當前區(qū)域性更改。
下表描述了用來格式化 DateTime 對象的標準格式說明符。
格式說明符 名稱 說明
d 短日期模式 顯示由與當前線程關聯的 DateTimeFormatInfo.ShortDatePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
D 長日期模式 顯示由與當前線程關聯的 DateTimeFormatInfo.LongDatePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
t 短時間模式 顯示由與當前線程關聯的 DateTimeFormatInfo.ShortTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
T 長時間模式 顯示由與當前線程關聯的 DateTimeFormatInfo.LongTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
f 完整日期/時間模式(短時間) 顯示長日期和短時間模式的組合,由空格分隔。
F 完整日期/時間模式(長時間) 顯示由與當前線程關聯的 DateTimeFormatInfo.FullDateTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
g 常規(guī)日期/時間模式(短時間) 顯示短日期和短時間模式的組合,由空格分隔。
G 常規(guī)日期/時間模式(長時間) 顯示短日期和長時間模式的組合,由空格分隔。
M 或 m 月日模式 顯示由與當前線程關聯的 DateTimeFormatInfo.MonthDayPattern 屬性定義的模式或者由指定格式提供程序定義的模式。
R 或 r RFC1123 模式 顯示由與當前線程關聯的 DateTimeFormatInfo.RFC1123Pattern 屬性定義的模式或者由指定格式提供程序定義的模式。這是定義的標準,并且屬性是只讀的;因此,無論所使用的區(qū)域性或所提供的格式提供程序是什么,它總是相 同的。屬性引用 CultureInfo.InvariantCulture 屬性并遵照自定義模式“ddd, dd MMM yyyy HH:mm:ss G\MT”。請注意,“GMT”中的“M”需要轉義符,因此它不被解釋。格式化并不修改 DateTime 的值,所以您必須在格式化之前將值調整為 GMT。
s 可排序的日期/時間模式;符合 ISO 8601 顯示由與當前線程關聯的 DateTimeFormatInfo.SortableDateTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。屬性引用 CultureInfo.InvariantCulture 屬性,格式遵照自定義模式“yyyy-MM-ddTHH:mm:ss”。
u 通用的可排序日期/時間模式 顯示由與當前線程關聯的 DateTimeFormatInfo.UniversalSortableDateTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。因為它是定義的標準,并且屬性是只讀的,因此無論區(qū)域性或格式提供程序是什么,模式總是相同的。格式化 遵照自定義模式“yyyy-MM-dd HH:mm:ssZ”。格式化日期和時間時不進行時區(qū)轉換;所以,請在使用格式說明符之前將本地日期和時間轉換為通用時間。
U 通用的可排序日期/時間模式 顯示由與當前線程關聯的 DateTimeFormatInfo.FullDateTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。請注意,顯示的時間是通用時間,而不是本地時間。
Y 或 y 年月模式 顯示由與當前線程關聯的 DateTimeFormatInfo.YearMonthPattern 屬性定義的模式或者由指定格式提供程序定義的模式。
任何其他單個字符 未知說明符
下面的示例說明了標準格式字符串如何與 DateTime 對象一起使用。
[Visual Basic]
Dim dt As DateTime = DateTime.Now
Dim dfi As DateTimeFormatInfo = New DateTimeFormatInfo()
Dim ci As CultureInfo = New CultureInfo("de-DE")
' Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd"
' Use the DateTimeFormat from the culture associated
' with the current thread.
Console.WriteLine( dt.ToString("d") )
Console.WriteLine( dt.ToString("m") )
' Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) )
' Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) )
' Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-BE")
Console.WriteLine( dt.ToString("d") )
[C#]
DateTime dt = DateTime.Now;
DateTimeFormatInfo dfi = new DateTimeFormatInfo();
CultureInfo ci = new CultureInfo("de-DE");
// Make up a new custom DateTime pattern, for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd";
// Use the DateTimeFormat from the culture associated
// with the current thread.
Console.WriteLine( dt.ToString("d") );
Console.WriteLine( dt.ToString("m") );
// Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) );
// Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) );
// Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
Console.WriteLine( dt.ToString("d") );
format函數
Dim MyDateTime As Date = #1/27/2001 5:04:23 PM#
Dim MyStr As String
' Returns current system time in the system-defined long time format.
MyStr = Format(Now(), "Long Time")
' Returns current system date in the system-defined long date format.
MyStr = Format(Now(), "Long Date")
' Also returns current system date in the system-defined long date
' format, using the single letter code for the format.
MyStr = Format(Now(), "D")
' Returns the value of MyDateTime in user-defined date/time formats.
MyStr = Format(MyDateTime, "h:m:s") ' Returns "5:4:23".
MyStr = Format(MyDateTime, "hh:mm:ss tt") ' Returns "05:04:23 PM".
MyStr = Format(MyDateTime, "dddd, MMM d yyyy") ' Returns "Saturday,
' Jan 27 2001".
MyStr = Format(MyDateTime, "HH:mm:ss") ' Returns "17:04:23"
MyStr = Format(23) ' Returns "23".
' User-defined numeric formats.
MyStr = Format(5459.4, "##,##0.00") ' Returns "5,459.40".
MyStr = Format(334.9, "###0.00") ' Returns "334.90".
MyStr = Format(5, "0.00%") ' Returns "500.00%".
string類型的轉換
CBool 示例
本示例使用 CBool 函數將表達式轉換為 Boolean 值。如果表達式的計算結果為非零值,CBool 將返回 True;否則返回 False。
Dim A, B, C As Integer
Dim Check As Boolean
A = 5
B = 5
Check = CBool(A = B) ' Check is set to True.
' ...
C = 0
Check = CBool(C) ' Check is set to False.
CByte 示例
本示例使用 CByte 函數將表達式轉換為 Byte。
Dim MyDouble As Double
Dim MyByte As Byte
MyDouble = 125.5678
MyByte = CByte(MyDouble) ' MyByte is set to 126.
CChar 示例
本示例使用 CChar 函數將 String 表達式的第一個字符轉換為 Char 類型。
Dim MyString As String
Dim MyChar As Char
MyString = "BCD" ' CChar converts only first character of string.
MyChar = CChar(MyString) ' MyChar is set to "B".
CChar 的輸入參數必須是 String 數據類型。由于 CChar 不能接受數值數據類型,因此無法使用 CChar 將數字轉換為字符。本示例獲取一個表示代碼數據點(字符代碼)的數字,然后將其轉換為對應的字符。它使用 InputBox 獲取數字字符串,并使用 CInt 將該字符串轉換為 Integer 類型,然后使用 ChrW 將該數字轉換為 Char 類型。
Dim MyDigits As String ' Input string of digits to be converted.
Dim CodePoint As Integer ' Number to be represented as a character.
Dim MyChar As Char
MyDigits = InputBox("Enter code point of character:")
CodePoint = CInt(MyDigits) ' Convert entire string to Integer.
MyChar = ChrW(CodePoint) ' MyChar is set to Char value of code point.
CDate 示例
本示例使用 CDate 函數將字符串轉換為 Date 值。通常,建議不要使用硬編碼的日期和時間作為字符串(如下例所示)。而應使用日期文本和時間文本,如 #Feb 12、1969# 和 #4:45:23 PM#。
Dim MyDateString, MyTimeString As String
Dim MyDate, MyTime As Date
MyDateString = "February 12, 1969"
MyTimeString = "4:35:47 PM"
' ...
MyDate = CDate(MyDateString) ' Convert to Date data type.
MyTime = CDate(MyTimeString) ' Convert to Date data type.
CDbl 示例
本示例使用 CDbl 函數將表達式轉換為 Double。
Dim MyDec As Decimal
Dim MyDouble As Double
MyDec = 234.456784D ' Literal type character D makes MyDec a Decimal.
MyDouble = CDbl(MyDec * 8.2D * 0.01D) ' Convert result to a Double.
CDec 示例
本示例使用 CDec 函數將數值轉換為 Decimal。
Dim MyDouble As Double
Dim MyDecimal As Decimal
MyDouble = 10000000.0587
MyDecimal = CDec(MyDouble) ' Convert to Decimal.
CInt 示例
本示例使用 CInt 函數將一個值轉換為 Integer。
Dim MyDouble As Double
Dim MyInt As Integer
MyDouble = 2345.5678
MyInt = CInt(MyDouble) ' MyInt is set to 2346.
CLng 示例
本示例使用 CLng 函數將多個值轉換為 Long。
Dim MyDbl1, MyDbl2 As Double
Dim MyLong1, MyLong2 As Long
MyDbl1 = 25427.45
MyDbl2 = 25427.55
MyLong1 = CLng(MyDbl1) ' MyLong1 contains 25427.
MyLong2 = CLng(MyDbl2) ' MyLong2 contains 25428.
CObj 示例
本示例使用 CObj 函數將一個數值轉換為 Object。Object 變量本身只包含一個四字節(jié)的指針,該指針指向賦給 Object 變量的 Double 值。
Dim MyDouble As Double
Dim MyObject As Object
MyDouble = 2.7182818284
MyObject = CObj(MyDouble) ' Double value is pointed to by MyObject.
CShort 示例
本示例使用 CShort 函數將一個數值轉換為 Short。
Dim MyByte as Byte
Dim MyShort as Short
MyByte = 100
MyShort = CShort(MyByte) ' Convert to Short.
CSng 示例
本示例使用 CSng 函數將多個值轉換為 Single。
Dim MyDouble1, MyDouble2 As Double
Dim MySingle1, MySingle2 As Single
MyDouble1 = 75.3421105
MyDouble2 = 75.3421567
MySingle1 = CSng(MyDouble1) ' MySingle1 is set to 75.34211.
MySingle2 = CSng(MyDouble2) ' MySingle2 is set to 75.34216.
CStr 示例
本示例使用 CStr 函數將一個數值轉換為字符串。
Dim MyDouble As Double
Dim MyString As String
MyDouble = 437.324
MyString = CStr(MyDouble) ' MyString is set to "437.324".
本示例使用 CStr 函數將多個 Date 值轉換為 String 值。
Dim MyDate As Date
Dim MyString As String
' ...
MyDate = #February 12, 1969 00:00:00# ' INVALID format.
' Date literals must be in the format #m/d/yyyy# or they are invalid.
' ...
MyDate = #2/12/69 00:00:00# ' Time is midnight.
' The neutral time value of 00:00:00 is suppressed in the conversion.
MyString = CStr(MyDate) ' MyString is set to "2/12/1969".
' ...
MyDate = #2/12/69 00:00:01# ' Time is one second past midnight.
' The time component becomes part of the converted value.
MyString = CStr(MyDate) ' MyString is set to "2/12/1969 12:00:01 AM".
CStr 始終以符合當前區(qū)域設置的標準短格式呈現 Date 值,如“6/15/2003 4:35:47 PM”。