System.Globalization 命名空间包含定义区域性相关信息的类,这些信息包括语言、国家/地区、使用的日历、日期、货币和数字的格式模式以及字符串的排序顺序。我们可以使用这些类编写全球化(国际化)应用程序。而像 StringInfo 和 TextInfo 这样的类更是为我们提供了诸如代理项支持和文本元素处理等高级全球化功能。

说明
Calendar 将时间分成段来表示,如分成星期、月和年。
CompareInfo 实现一组方法进行区分区域性的字符串比较。
CultureInfo 表示有关特定区域性的信息,包括区域性的名称、书写体系和使用的日历,以及有关对常用操作(如格式化日期和排序字符串)提供信息的区域性特定对象的访问。
DateTimeFormatInfo 定义如何根据区域性设置 DateTime 值的格式并显示这些值。
DaylightTime 定义夏时制周期。
GregorianCalendar 表示公历。
HebrewCalendar 表示犹太历。
HijriCalendar 表示回历。
JapaneseCalendar 表示日本历。
JulianCalendar 表示儒略历。
KoreanCalendar 表示朝鲜历。
NumberFormatInfo 根据区域性定义如何设置数值格式以及如何显示数值。
RegionInfo 包含有关国家/地区的信息。
SortKey 表示将字符串映射到其排序关键字的映射结果。
StringInfo 提供功能将字符串拆分为文本元素并循环访问这些文本元素。
TaiwanCalendar 表示台湾地区日历。
TextElementEnumerator 枚举字符串的文本元素。
TextInfo 定义特定于书写体系的属性和行为(如大小写)。
ThaiBuddhistCalendar 表示泰国佛历。

枚举

枚举 说明
CalendarWeekRule 定义确定年份第一周的不同规则。
CompareOptions 定义要用于 CompareInfo 的字符串比较选项。
CultureTypes 定义可以使用 CultureInfo.GetCultures 检索的区域性列表的类型。
DateTimeStyles 定义一些格式设置选项,这些选项自定义 DateTime.Parse 和 DateTime.ParseExact方法如何分析一个字符串。
GregorianCalendarTypes 定义公历的不同语言版本。
NumberStyles 确定数字字符串参数中允许的样式,这些参数被传递到数字基类型类的 Parse 方法。
UnicodeCategory 定义字符的 Unicode 类别。

CultureInfo 类

表示有关特定区域性的信息,包括区域性的名称、书写体系和使用的日历,以及有关对常用操作(如格式化日期和排序字符串)提供信息的区域性特定对象的访问。

有关此类型所有成员的列表,请参阅 CultureInfo 成员。

System.Object
   System.Globalization.CultureInfo

[Visual Basic]
<Serializable>
Public Class CultureInfo
Implements ICloneable, IFormatProvider
[C#]
[Serializable]
public class CultureInfo : ICloneable, IFormatProvider
[C++]
[Serializable]
public __gc class CultureInfo : public ICloneable, IFormatProvider
[JScript]
public
Serializable
class CultureInfo implements ICloneable, IFormatProvider

线程安全

此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。

备注

CultureInfo 类保存区域性特定的信息,如关联的语言、子语言、国家/地区、日历和区域性约定。此类还提供对 DateTimeFormatInfo、NumberFormatInfo、CompareInfo 和TextInfo 的区域性特定实例的访问。这些对象包含区域性特定操作(如大小写、格式化日期和数字以及比较字符串)所需的信息。

String 类间接地使用此类来获取有关默认区域性的信息。

区域性名称遵循 RFC 1766 标准,格式为“<languagecode2>-<country/regioncode2>”,其中 <languagecode2> 是从 ISO 639-1 派生的由两个小写字母构成的代码,<country/regioncode2> 是从 ISO 3166 派生的由两个大写字母构成的代码。例如,美国英语为“en-US”。在双字母语言代码不可用的情况中,将使用从 ISO 639-2 派生的三字母代码;例如,三字母代码“div”用于使用 Dhivehi 语言的区域。某些区域性名称带有指定书写符号的后缀;例如“-Cyrl”指定西里尔语书写符号,“-Latn”指定拉丁语书写符号。

此类和 System.Globalization 命名空间中的其他类接受并使用下列预定义的 CultureInfo 名称和标识符。

区域性名称 区域性标识符 语言-国家/地区
""(空字符串) 0x007F 固定区域性
af 0x0036 南非荷兰语
af-ZA 0x0436 南非荷兰语 - 南非
sq 0x001C 阿尔巴尼亚语
sq-AL 0x041C 阿尔巴尼亚语 - 阿尔巴尼亚
ar 0x0001 阿拉伯语
ar-DZ 0x1401 阿拉伯语 - 阿尔及利亚
ar-BH 0x3C01 阿拉伯语 - 巴林
ar-EG 0x0C01 阿拉伯语 - 埃及
ar-IQ 0x0801 阿拉伯语 - 伊拉克
ar-JO 0x2C01 阿拉伯语 - 约旦
ar-KW 0x3401 阿拉伯语 - 科威特
ar-LB 0x3001 阿拉伯语 - 黎巴嫩
ar-LY 0x1001 阿拉伯语 - 利比亚
ar-MA 0x1801 阿拉伯语 - 摩洛哥
ar-OM 0x2001 阿拉伯语 - 阿曼
ar-QA 0x4001 阿拉伯语 - 卡塔尔
ar-SA 0x0401 阿拉伯语 - 沙特阿拉伯
ar-SY 0x2801 阿拉伯语 - 叙利亚
ar-TN 0x1C01 阿拉伯语 - 突尼斯
ar-AE 0x3801 阿拉伯语 - 阿拉伯联合酋长国
ar-YE 0x2401 阿拉伯语 - 也门
hy 0x002B 亚美尼亚语
hy-AM 0x042B 亚美尼亚语 - 亚美尼亚
az 0x002C 阿泽里语
az-AZ-Cyrl 0x082C 阿泽里语(西里尔语)- 阿塞拜疆
az-AZ-Latn 0x042C 阿泽里语(拉丁)- 阿塞拜疆
eu 0x002D 巴斯克语
eu-ES 0x042D 巴斯克语 - 巴斯克地区
be 0x0023 白俄罗斯语
be-BY 0x0423 白俄罗斯语 - 白俄罗斯
bg 0x0002 保加利亚语
bg-BG 0x0402 保加利亚语 - 保加利亚
ca 0x0003 加泰罗尼亚语
ca-ES 0x0403 加泰罗尼亚语 - 加泰罗尼亚地区
zh-HK 0x0C04 中文 - 香港特别行政区
zh-MO 0x1404 中文 - 澳门特别行政区
zh-CN 0x0804 中文 - 中国
zh-CHS 0x0004 中文(简体)
zh-SG 0x1004 中文 - 新加坡
zh-TW 0x0404 中文 - 台湾
zh-CHT 0x7C04 中文(繁体)
hr 0x001A 克罗地亚语
hr-HR 0x041A 克罗地亚语 - 克罗地亚
cs 0x0005 捷克语
cs-CZ 0x0405 捷克语 - 捷克共和国
da 0x0006 丹麦语
da-DK 0x0406 丹麦语 - 丹麦
div 0x0065 马尔代夫语
div-MV 0x0465 马尔代夫语 - 马尔代夫
nl 0x0013 荷兰语
nl-BE 0x0813 荷兰语 - 比利时
nl-NL 0x0413 荷兰语 - 荷兰
en 0x0009 英语
en-AU 0x0C09 英语 - 澳大利亚
en-BZ 0x2809 英语 - 伯利兹
en-CA 0x1009 英语 - 加拿大
en-CB 0x2409 英语 - 加勒比
en-IE 0x1809 英语 - 爱尔兰
en-JM 0x2009 英语 - 牙买加
en-NZ 0x1409 英语 - 新西兰
en-PH 0x3409 英语 - 菲律宾
en-ZA 0x1C09 英语 - 南非
en-TT 0x2C09 英语 - 特立尼达和多巴哥
en-GB 0x0809 英语 - 英国
en-US 0x0409 英语 - 美国
en-ZW 0x3009 英语 - 津巴布韦
et 0x0025 爱沙尼亚语
et-EE 0x0425 爱沙尼亚语 - 爱沙尼亚
fo 0x0038 法罗语
fo-FO 0x0438 法罗语 - 法罗群岛
fa 0x0029 波斯语
fa-IR 0x0429 波斯语 - 伊朗
fi 0x000B 芬兰语
fi-FI 0x040B 芬兰语 - 芬兰
fr 0x000C 法语
fr-BE 0x080C 法语 - 比利时
fr-CA 0x0C0C 法语 - 加拿大
fr-FR 0x040C 法语 - 法国
fr-LU 0x140C 法语 - 卢森堡
fr-MC 0x180C 法语 - 摩纳哥
fr-CH 0x100C 法语 - 瑞士
gl 0x0056 加利西亚语
gl-ES 0x0456 加利西亚语 - 加利西亚地区
ka 0x0037 格鲁吉亚语
ka-GE 0x0437 格鲁吉亚语 - 格鲁吉亚
de 0x0007 德语
de-AT 0x0C07 德语 - 奥地利
de-DE 0x0407 德语 - 德国
de-LI 0x1407 德语 - 列支敦士登
de-LU 0x1007 德语 - 卢森堡
de-CH 0x0807 德语 - 瑞士
el 0x0008 希腊语
el-GR 0x0408 希腊语 - 希腊
gu 0x0047 古吉拉特语
gu-IN 0x0447 古吉拉特语 - 印度
he 0x000D 希伯来语
he-IL 0x040D 希伯来语 - 以色列
hi 0x0039 印地语
hi-IN 0x0439 印地语 - 印度
hu 0x000E 匈牙利语
hu-HU 0x040E 匈牙利语 - 匈牙利
is 0x000F 冰岛语
is-IS 0x040F 冰岛语 - 冰岛
id 0x0021 印度尼西亚语
id-ID 0x0421 印度尼西亚语 - 印度尼西亚
it 0x0010 意大利语
it-IT 0x0410 意大利语 - 意大利
it-CH 0x0810 意大利语 - 瑞士
ja 0x0011 日语
ja-JP 0x0411 日语 - 日本
kn 0x004B 卡纳达语
kn-IN 0x044B 卡纳达语 - 印度
kk 0x003F 哈萨克语
kk-KZ 0x043F 哈萨克语 - 哈萨克斯坦
kok 0x0057 贡根语
kok-IN 0x0457 贡根语 - 印度
ko 0x0012 朝鲜语
ko-KR 0x0412 朝鲜语 - 韩国
ky 0x0040 吉尔吉斯语
ky-KZ 0x0440 吉尔吉斯语 - 吉尔吉斯坦
lv 0x0026 拉脱维亚语
lv-LV 0x0426 拉脱维亚语 - 拉脱维亚
lt 0x0027 立陶宛语
lt-LT 0x0427 立陶宛语 - 立陶宛
mk 0x002F 马其顿语
mk-MK 0x042F 马其顿语 - FYROM
ms 0x003E 马来语
ms-BN 0x083E 马来语 - 文莱
ms-MY 0x043E 马来语 - 马来西亚
mr 0x004E 马拉地语
mr-IN 0x044E 马拉地语 - 印度
mn 0x0050 蒙古语
mn-MN 0x0450 蒙古语 - 蒙古
no 0x0014 挪威语
nb-NO 0x0414 挪威语(博克马尔)- 挪威
nn-NO 0x0814 挪威语(尼诺斯克)- 挪威
pl 0x0015 波兰语
pl-PL 0x0415 波兰语 - 波兰
pt 0x0016 葡萄牙语
pt-BR 0x0416 葡萄牙语 - 巴西
pt-PT 0x0816 葡萄牙语 - 葡萄牙
pa 0x0046 旁遮普语
pa-IN 0x0446 旁遮普语 - 印度
ro 0x0018 罗马尼亚语
ro-RO 0x0418 罗马尼亚语 - 罗马尼亚
ru 0x0019 俄语
ru-RU 0x0419 俄语 - 俄罗斯
sa 0x004F 梵语
sa-IN 0x044F 梵语 - 印度
sr-SP-Cyrl 0x0C1A 塞尔维亚语(西里尔语)- 塞尔维亚
sr-SP-Latn 0x081A 塞尔维亚语(拉丁)- 塞尔维亚
sk 0x001B 斯洛伐克语
sk-SK 0x041B 斯洛伐克语 - 斯洛伐克
sl 0x0024 斯洛文尼亚语
sl-SI 0x0424 斯洛文尼亚语 - 斯洛文尼亚
es 0x000A 西班牙语
es-AR 0x2C0A 西班牙语 - 阿根廷
es-BO 0x400A 西班牙语 - 玻利维亚
es-CL 0x340A 西班牙语 - 智利
es-CO 0x240A 西班牙语 - 哥伦比亚
es-CR 0x140A 西班牙语 - 哥斯达黎加
es-DO 0x1C0A 西班牙语 - 多米尼加共和国
es-EC 0x300A 西班牙语 - 厄瓜多尔
es-SV 0x440A 西班牙语 - 萨尔瓦多
es-GT 0x100A 西班牙语 - 危地马拉
es-HN 0x480A 西班牙语 - 洪都拉斯
es-MX 0x080A 西班牙语 - 墨西哥
es-NI 0x4C0A 西班牙语 - 尼加拉瓜
es-PA 0x180A 西班牙语 - 巴拿马
es-PY 0x3C0A 西班牙语 - 巴拉圭
es-PE 0x280A 西班牙 - 秘鲁
es-PR 0x500A 西班牙语 - 波多黎各
es-ES 0x0C0A 西班牙语 - 西班牙
es-UY 0x380A 西班牙语 - 乌拉圭
es-VE 0x200A 西班牙语 - 委内瑞拉
sw 0x0041 斯瓦希里语
sw-KE 0x0441 斯瓦希里语 - 肯尼亚
sv 0x001D 瑞典语
sv-FI 0x081D 瑞典语 - 芬兰
sv-SE 0x041D 瑞典语 - 瑞典
syr 0x005A 叙利亚语
syr-SY 0x045A 叙利亚语 - 叙利亚
ta 0x0049 泰米尔语
ta-IN 0x0449 泰米尔语 - 印度
tt 0x0044 鞑靼语
tt-RU 0x0444 鞑靼语 - 俄罗斯
te 0x004A 泰卢固语
te-IN 0x044A 泰卢固语 - 印度
th 0x001E 泰语
th-TH 0x041E 泰语 - 泰国
tr 0x001F 土耳其语
tr-TR 0x041F 土耳其语 - 土耳其
uk 0x0022 乌克兰语
uk-UA 0x0422 乌克兰语 - 乌克兰
ur 0x0020 乌尔都语
ur-PK 0x0420 乌尔都语 - 巴基斯坦
uz 0x0043 乌兹别克语
uz-UZ-Cyrl 0x0843 乌兹别克语(西里尔语)- 乌兹别克斯坦
uz-UZ-Latn 0x0443 乌兹别克语(拉丁)- 乌兹别克斯坦
vi 0x002A 越南语
vi-VN 0x042A 越南语 - 越南

“西班牙语 - 西班牙”的区域性标识符“0x0c0a”使用默认的国际排序顺序;而区域性标识符“0x040A”(也指“西班牙语 - 西班牙”)则使用传统排序顺序。如果使用“es-ES”区域性名称构造 CultureInfo,则新的 CultureInfo 将使用默认的国际排序顺序。要构造一个使用传统排序顺序的 CultureInfo,可在构造函数中使用区域性标识符“0x040A”。有关具有备用排序方法的其他区域性的更多信息,请参见“使用备用排序顺序”。

这些区域性通常分为三个集合:固定区域性、非特定区域性和特定区域性。

固定区域性不区分区域性。可以使用空字符串 ("") 按名称或者按区域性标识符 0x007F 来指定固定区域性。InvariantCulture 检索固定区域性的实例。它与英语语言关联,但不与任何国家/地区关联。它几乎可用在要求区域性的“全局化”命名空间中的所有方法中。如果安全决策取决于字符串比较或大小写更改操作,则使用 InvariantCulture 确保无论如何设置系统的区域性,行为都将保持一致。但是,固定区域性必须仅由需要不依赖区域性的结果的进程(如系统服务)使用;否则,它得到的结果可能在语言上不正确,或者在文化上不合适。

非特定区域性是与某种语言关联但不与国家/地区关联的区域性。特定区域性是与某种语言和某个国家/地区关联的区域性。例如,“fr”是非特定区域性,而“fr-FR”是特定区域性。注意,“zh-CHS”(简体中文)和“zh-CHT”(繁体中文)均为非特定区域性。

区域性有层次结构,即特定区域性的父级是非特定区域性,而非特定区域性的父级是 InvariantCulture。Parent 属性将返回与特定区域性关联的非特定区域性。

如果特定区域性的资源在系统中不可用,则使用非特定区域性的资源;如果非特定区域性的资源也不可用,那么使用主程序集中嵌入的资源。有关资源备用进程的更多信息,请参见“资源备用进程”。

Windows API 中的区域性列表与 .NET Framework 中的区域性列表略有不同。例如,在 Windows API 中未提供带有区域性标识符 0x7C04 的非特定区域性 zh-CHT“中文(繁体)”。如果需要具备同 Windows 进行互操作的能力(例如,通过 p/invoke 机制),可使用在 .NET Framework 中定义的特定区域性。这样可以确保与等效的 Windows 区域设置保持一致,它们使用同一 LCID 进行标识。

只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。

用户可以通过“控制面板”中的“区域和语言选项”(或“区域选项”或“区域设置”)选择重写某些与 Windows 的当前区域性关联的值。例如,用户可能选择以另一种格式显示日期,或选择使用区域性默认设置以外的货币。

如果 UseUserOverride 为 true 并且指定的区域性与当前 Windows 的区域性匹配,则 CultureInfo 使用那些重写,包括以下属性的用户设置:由 DateTimeFormat 属性返回的 DateTimeFormatInfo 实例的属性、由 NumberFormat 属性返回的 NumberFormatInfo 实例的属性,以及由 CompareInfo 属性返回的 CompareInfo 实例的属性。如果用户设置与 CultureInfo 的关联区域性不兼容(例如选定的日历不属于 OptionalCalendars 其中之一),则方法结果和属性值是未定义的。

对于使用欧元的区域性,.NET Framework 和 Windows XP 将默认货币设置为欧元;但是,旧版本的 Windows 却不是这样。因此,如果旧版本 Windows 的用户未通过“控制面板”中的“区域选项”或“区域设置”更改货币设置,则货币可能不正确。若要使用 .NET Framework 的货币的默认设置,请使用接受 useUserOverride 参数的 CultureInfo 构造函数重载,并将该参数设置为 false。

该类实现 ICloneable 接口以启用 CultureInfo 对象的复制。它还实现 IFormatProvider 以便为应用程序提供格式化信息。

示例

[Visual Basic, C#, C++] 下面的代码示例显示如何按国际排序创建一个用于“西班牙语-西班牙”的 CultureInfo,并按传统排序创建另一个 CultureInfo。

[Visual Basic]
Imports System
Imports System.Collections
Imports System.Globalization
Public Class SamplesCultureInfo
Public Shared Sub Main()
' Creates and initializes the CultureInfo which uses the international sort.
Dim myCIintl As New CultureInfo("es-ES", False)
' Creates and initializes the CultureInfo which uses the traditional sort.
Dim myCItrad As New CultureInfo(&H40A, False)
' Displays the properties of each culture.
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "PROPERTY", "INTERNATIONAL", "TRADITIONAL")
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "CompareInfo", myCIintl.CompareInfo, myCItrad.CompareInfo)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "DisplayName", myCIintl.DisplayName, myCItrad.DisplayName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "EnglishName", myCIintl.EnglishName, myCItrad.EnglishName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "IsNeutralCulture", myCIintl.IsNeutralCulture, myCItrad.IsNeutralCulture)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "IsReadOnly", myCIintl.IsReadOnly, myCItrad.IsReadOnly)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "LCID", myCIintl.LCID, myCItrad.LCID)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "Name", myCIintl.Name, myCItrad.Name)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "NativeName", myCIintl.NativeName, myCItrad.NativeName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "Parent", myCIintl.Parent, myCItrad.Parent)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "TextInfo", myCIintl.TextInfo, myCItrad.TextInfo)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "ThreeLetterISOLanguageName", myCIintl.ThreeLetterISOLanguageName, myCItrad.ThreeLetterISOLanguageName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "ThreeLetterWindowsLanguageName", myCIintl.ThreeLetterWindowsLanguageName, myCItrad.ThreeLetterWindowsLanguageName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "TwoLetterISOLanguageName", myCIintl.TwoLetterISOLanguageName, myCItrad.TwoLetterISOLanguageName)
Console.WriteLine()
' Compare two strings using myCIintl.
Console.WriteLine("Comparing ""llegar"" and ""lugar""")
Console.WriteLine("   With myCIintl.CompareInfo.Compare: {0}", myCIintl.CompareInfo.Compare("llegar", "lugar"))
Console.WriteLine("   With myCItrad.CompareInfo.Compare: {0}", myCItrad.CompareInfo.Compare("llegar", "lugar"))
End Sub 'Main
End Class 'SamplesCultureInfo
'This code produces the following output.
'
'PROPERTY                         INTERNATIONAL            TRADITIONAL
'CompareInfo                      CompareInfo - 3082       CompareInfo - 1034
'DisplayName                      Spanish (Spain)          Spanish (Spain)
'EnglishName                      Spanish (Spain)          Spanish (Spain)
'IsNeutralCulture                 False                    False
'IsReadOnly                       False                    False
'LCID                             3082                     1034
'Name                             es-ES                    es-ES
'NativeName                       espaol (Espaa)         espaol (Espaa)
'Parent                           es                       es
'TextInfo                         TextInfo - 3082          TextInfo - 1034
'ThreeLetterISOLanguageName       spa                      spa
'ThreeLetterWindowsLanguageName   ESN                      ESN
'TwoLetterISOLanguageName         es                       es
'
'Comparing "llegar" and "lugar"
'   With myCIintl.CompareInfo.Compare: -1
'   With myCItrad.CompareInfo.Compare: 1
[C#]
using System;
using System.Collections;
using System.Globalization;
public class SamplesCultureInfo  {
public static void Main()  {
// Creates and initializes the CultureInfo which uses the international sort.
CultureInfo myCIintl = new CultureInfo( "es-ES", false );
// Creates and initializes the CultureInfo which uses the traditional sort.
CultureInfo myCItrad = new CultureInfo( 0x040A, false );
// Displays the properties of each culture.
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "PROPERTY", "INTERNATIONAL", "TRADITIONAL" );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "CompareInfo", myCIintl.CompareInfo, myCItrad.CompareInfo );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "DisplayName", myCIintl.DisplayName, myCItrad.DisplayName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "EnglishName", myCIintl.EnglishName, myCItrad.EnglishName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "IsNeutralCulture", myCIintl.IsNeutralCulture, myCItrad.IsNeutralCulture );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "IsReadOnly", myCIintl.IsReadOnly, myCItrad.IsReadOnly );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "LCID", myCIintl.LCID, myCItrad.LCID );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "Name", myCIintl.Name, myCItrad.Name );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "NativeName", myCIintl.NativeName, myCItrad.NativeName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "Parent", myCIintl.Parent, myCItrad.Parent );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "TextInfo", myCIintl.TextInfo, myCItrad.TextInfo );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "ThreeLetterISOLanguageName", myCIintl.ThreeLetterISOLanguageName, myCItrad.ThreeLetterISOLanguageName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "ThreeLetterWindowsLanguageName", myCIintl.ThreeLetterWindowsLanguageName, myCItrad.ThreeLetterWindowsLanguageName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "TwoLetterISOLanguageName", myCIintl.TwoLetterISOLanguageName, myCItrad.TwoLetterISOLanguageName );
Console.WriteLine();
// Compare two strings using myCIintl.
Console.WriteLine( "Comparing \"llegar\" and \"lugar\"" );
Console.WriteLine( "   With myCIintl.CompareInfo.Compare: {0}", myCIintl.CompareInfo.Compare( "llegar", "lugar" ) );
Console.WriteLine( "   With myCItrad.CompareInfo.Compare: {0}", myCItrad.CompareInfo.Compare( "llegar", "lugar" ) );
}
}
/*
This code produces the following output.
PROPERTY                         INTERNATIONAL            TRADITIONAL
CompareInfo                      CompareInfo - 3082       CompareInfo - 1034
DisplayName                      Spanish (Spain)          Spanish (Spain)
EnglishName                      Spanish (Spain)          Spanish (Spain)
IsNeutralCulture                 False                    False
IsReadOnly                       False                    False
LCID                             3082                     1034
Name                             es-ES                    es-ES
NativeName                       espaol (Espaa)         espaol (Espaa)
Parent                           es                       es
TextInfo                         TextInfo - 3082          TextInfo - 1034
ThreeLetterISOLanguageName       spa                      spa
ThreeLetterWindowsLanguageName   ESN                      ESN
TwoLetterISOLanguageName         es                       es
Comparing "llegar" and "lugar"
With myCIintl.CompareInfo.Compare: -1
With myCItrad.CompareInfo.Compare: 1
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
int main()
{
// Creates and initializes the CultureInfo which uses the international sort.
CultureInfo* myCIintl = new CultureInfo(S"es-ES", false);
// Creates and initializes the CultureInfo which uses the traditional sort.
CultureInfo* myCItrad = new CultureInfo(0x040A, false);
// Displays the properties of each culture.
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"PROPERTY", S"INTERNATIONAL", S"TRADITIONAL");
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"CompareInfo", myCIintl -> CompareInfo, myCItrad -> CompareInfo);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"DisplayName", myCIintl -> DisplayName, myCItrad -> DisplayName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"EnglishName", myCIintl -> EnglishName, myCItrad -> EnglishName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"IsNeutralCulture", __box(myCIintl -> IsNeutralCulture), __box(myCItrad -> IsNeutralCulture));
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"IsReadOnly", __box(myCIintl -> IsReadOnly), __box(myCItrad -> IsReadOnly));
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"LCID", __box(myCIintl -> LCID), __box(myCItrad -> LCID));
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"Name", myCIintl -> Name, myCItrad -> Name);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"NativeName", myCIintl -> NativeName, myCItrad -> NativeName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"Parent", myCIintl -> Parent, myCItrad -> Parent);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"TextInfo", myCIintl -> TextInfo, myCItrad -> TextInfo);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"ThreeLetterISOLanguageName", myCIintl -> ThreeLetterISOLanguageName, myCItrad -> ThreeLetterISOLanguageName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"ThreeLetterWindowsLanguageName", myCIintl -> ThreeLetterWindowsLanguageName, myCItrad -> ThreeLetterWindowsLanguageName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"TwoLetterISOLanguageName", myCIintl -> TwoLetterISOLanguageName, myCItrad -> TwoLetterISOLanguageName);
Console::WriteLine();
// Compare two strings using myCIintl ->
Console::WriteLine(S"Comparing \"llegar\" and \"lugar\"");
Console::WriteLine(S"   With myCIintl -> CompareInfo -> Compare: {0}", __box(myCIintl -> CompareInfo -> Compare(S"llegar", S"lugar")));
Console::WriteLine(S"   With myCItrad -> CompareInfo -> Compare: {0}", __box(myCItrad -> CompareInfo -> Compare(S"llegar", S"lugar")));
}
/*
This code produces the following output.
PROPERTY                         INTERNATIONAL            TRADITIONAL
CompareInfo                      CompareInfo - 3082       CompareInfo - 1034
DisplayName                      Spanish (Spain)          Spanish (Spain)
EnglishName                      Spanish (Spain)          Spanish (Spain)
IsNeutralCulture                 False                    False
IsReadOnly                       False                    False
LCID                             3082                     1034
Name                             es-ES                    es-ES
NativeName                       espaol (Espaa)         espaol (Espaa)
Parent                           es                       es
TextInfo                         TextInfo - 3082          TextInfo - 1034
ThreeLetterISOLanguageName       spa                      spa
ThreeLetterWindowsLanguageName   ESN                      ESN
TwoLetterISOLanguageName         es                       es
Comparing S"llegar" and S"lugar"
With myCIintl -> CompareInfo -> Compare: -1
With myCItrad -> CompareInfo -> Compare: 1
*/

[Visual Basic, C#, C++] 下面的代码示例使用中文确定每个特定区域性的父区域性。

[Visual Basic]
Imports System
Imports System.Globalization
Public Class SamplesCultureInfo
Public Shared Sub Main()
' Prints the header.
Console.WriteLine("SPECIFIC CULTURE                                  PARENT CULTURE")
' Determines the specific cultures that use the Chinese language, and displays the parent culture.
Dim ci As CultureInfo
For Each ci In  CultureInfo.GetCultures(CultureTypes.SpecificCultures)
If ci.TwoLetterISOLanguageName = "zh" Then
Console.Write("0x{0} {1} {2,-37}", ci.LCID.ToString("X4"), ci.Name, ci.EnglishName)
Console.WriteLine("0x{0} {1} {2}", ci.Parent.LCID.ToString("X4"), ci.Parent.Name, ci.Parent.EnglishName)
End If
Next ci
End Sub 'Main
End Class 'SamplesCultureInfo
'This code produces the following output.
'
'SPECIFIC CULTURE                                  PARENT CULTURE
'0x0404 zh-TW Chinese (Taiwan)                     0x7C04 zh-CHT Chinese (Traditional)
'0x0804 zh-CN Chinese (People's Republic of China) 0x0004 zh-CHS Chinese (Simplified)
'0x0C04 zh-HK Chinese (Hong Kong S.A.R.)           0x7C04 zh-CHT Chinese (Traditional)
'0x1004 zh-SG Chinese (Singapore)                  0x0004 zh-CHS Chinese (Simplified)
'0x1404 zh-MO Chinese (Macau S.A.R.)               0x0004 zh-CHS Chinese (Simplified)
[C#]
using System;
using System.Globalization;
public class SamplesCultureInfo  {
public static void Main()  {
// Prints the header.
Console.WriteLine( "SPECIFIC CULTURE                                  PARENT CULTURE" );
// Determines the specific cultures that use the Chinese language, and displays the parent culture.
foreach ( CultureInfo ci in CultureInfo.GetCultures( CultureTypes.SpecificCultures ) )  {
if ( ci.TwoLetterISOLanguageName == "zh" )  {
Console.Write( "0x{0} {1} {2,-37}", ci.LCID.ToString("X4"), ci.Name, ci.EnglishName );
Console.WriteLine( "0x{0} {1} {2}", ci.Parent.LCID.ToString("X4"), ci.Parent.Name, ci.Parent.EnglishName );
}
}
}
}
/*
This code produces the following output.
SPECIFIC CULTURE                                  PARENT CULTURE
0x0404 zh-TW Chinese (Taiwan)                     0x7C04 zh-CHT Chinese (Traditional)
0x0804 zh-CN Chinese (People's Republic of China) 0x0004 zh-CHS Chinese (Simplified)
0x0C04 zh-HK Chinese (Hong Kong S.A.R.)           0x7C04 zh-CHT Chinese (Traditional)
0x1004 zh-SG Chinese (Singapore)                  0x0004 zh-CHS Chinese (Simplified)
0x1404 zh-MO Chinese (Macau S.A.R.)               0x0004 zh-CHS Chinese (Simplified)
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
int main() {
// Prints the header.
Console::WriteLine(S"SPECIFIC CULTURE                                  PARENT CULTURE" );
// Determines the specific cultures that use the Chinese language,
// and displays the parent culture.
System::Collections::IEnumerator* en =
CultureInfo::GetCultures(CultureTypes::SpecificCultures)->GetEnumerator();
while (en->MoveNext())
{
CultureInfo* ci = __try_cast<CultureInfo*>(en->Current);
if (ci->TwoLetterISOLanguageName->Equals(S"zh"))
{
Console::Write(S"0x{0} {1} {2,-37}", ci->LCID.ToString("X4"),
ci->Name, ci->EnglishName);
Console::WriteLine(S"0x{0} {1} {2}", ci->Parent->LCID.ToString(S"X4"),
ci->Parent->Name, ci->Parent->EnglishName);
}
}
}
/*
This code produces the following output.
SPECIFIC CULTURE                                  PARENT CULTURE
0x0404 zh-TW Chinese (Taiwan)                     0x7C04 zh-CHT Chinese (Traditional)
0x0804 zh-CN Chinese (People's Republic of China) 0x0004 zh-CHS Chinese (Simplified)
0x0C04 zh-HK Chinese (Hong Kong S.A.R.)           0x7C04 zh-CHT Chinese (Traditional)
0x1004 zh-SG Chinese (Singapore)                  0x0004 zh-CHS Chinese (Simplified)
0x1404 zh-MO Chinese (Macau S.A.R.)               0x0004 zh-CHS Chinese (Simplified)
*/
 
.NET Framework 类库  

DateTime.ParseExact 方法 (String, String, IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定格式信息和样式,将日期和时间的指定字符串表示形式转换为其等效的 DateTime。字符串表示的格式必须与指定的格式完全匹配。

[Visual Basic]
Overloads Public Shared Function ParseExact( _
ByVal s As String, _
ByVal format As String, _
ByVal provider As IFormatProvider, _
ByVal style As DateTimeStyles _
) As DateTime
[C#]
public static DateTime ParseExact(
string s,
string format,
IFormatProvider provider,
DateTimeStyles style
);
[C++]
public: static DateTime ParseExact(
String* s,
String* format,
IFormatProvider* provider,
DateTimeStyles style
);
[JScript]
public static function ParseExact(
s : String,
format : String,
provider : IFormatProvider,
style : DateTimeStyles
) : DateTime;

参数

s
包含要转换的日期和时间的字符串。
format
s 的预期格式。
provider
一个 IFormatProvider,它提供有关 s 的区域性特定格式设置信息。
style
一个或多个 DateTimeStyles 常数的组合,用以指示 s 的允许格式。

返回值

DateTime,等效于由 format、provider 和 style 所指定的 s 中所包含的日期和时间。

异常

异常类型 条件
ArgumentNullException s 或 format 为空引用(Visual Basic 中为 Nothing)。
FormatException s 或 format 是空字符串

- 或 -

s 不包含与 format 中指定的模式相对应的日期和时间。

备注

如果 s 的格式与 format 中的格式模式所指定的格式不完全匹配,则此方法引发 FormatException。如果 format 由单个标准格式的字符组成,则使用该字符所表示的格式模式。有关更多信息,请参见 DateTimeFormatInfo 主题。

如果 s 中包含时间不包含日期,则 style 参数确定是使用当前日期还是使用默认日期。如果 s 中包含日期不包含时间,则使用午夜 12 点。style 参数还确定是否忽略前置的、内部的或后随的空白字符。

provider 参数提供区域性特定的日期和时间格式信息。例如,用特定语言表示的一周内各天的名称,或年、月和日的惯用表示顺序。如果 provider 为空引用(Visual Basic 中为 Nothing),则使用当前区域性。

示例

[Visual Basic, C#, C++] 下面的示例说明 ParseExact 方法。

[Visual Basic]
Imports System
Imports System.Globalization
Class Class1
Public Shared Sub Main()
' Assume the current culture is en-US.
' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
Dim myDateTimeValue As String = "2/16/1992 12:15:12"
Dim myDateTime As DateTime = DateTime.Parse(myDateTimeValue)
Console.WriteLine("1) myDateTime       = {0}", myDateTime)
' Reverse month and day to conform to a different culture.
' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
Dim culture = New CultureInfo("fr-FR", True)
Dim myDateTimeFrenchValue As String = "    16/02/1992 12:15:12"
Dim myDateTimeFrench As DateTime = _
DateTime.Parse(myDateTimeFrenchValue, _
culture, _
DateTimeStyles.NoCurrentDateDefault)
Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench)
' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
Dim expectedFormats As String() =  {"G", "g", "f", "F"}
myDateTimeFrench = DateTime.ParseExact(myDateTimeFrenchValue, _
expectedFormats, _
culture, _
DateTimeStyles.AllowWhiteSpaces)
Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench)
End Sub 'Main
End Class 'Class1
'
'This example yields the following results:
'
'1) myDateTime       = 2/16/1992 12:15:12 PM
'2) myDateTimeFrench = 2/16/1992 12:15:12 PM
'3) myDateTimeFrench = 2/16/1992 12:15:12 PM
'
[C#]
using System;
using System.Globalization;
namespace Parse
{
class Class1
{
public static void Main(string[] args)
{
// Assume the current culture is en-US.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
string myDateTimeValue = "2/16/1992 12:15:12";
DateTime myDateTime = DateTime.Parse(myDateTimeValue);
Console.WriteLine("1) myDateTime       = {0}", myDateTime);
// Reverse month and day to conform to a different culture.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
IFormatProvider culture = new CultureInfo("fr-FR", true);
string myDateTimeFrenchValue = "    16/02/1992 12:15:12";
DateTime myDateTimeFrench =
DateTime.Parse(myDateTimeFrenchValue,
culture,
DateTimeStyles.NoCurrentDateDefault);
Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench);
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
string[] expectedFormats = {"G", "g", "f" ,"F"};
myDateTimeFrench =
DateTime.ParseExact(myDateTimeFrenchValue,
expectedFormats,
culture,
DateTimeStyles.AllowWhiteSpaces);
Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench);
}
}
}
/*
This example yields the following results:
1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
int main() {
// Assume the current culture is en-US.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
String* myDateTimeValue = S"2/16/1992 12:15:12";
DateTime myDateTime = DateTime::Parse(myDateTimeValue);
Console::WriteLine(S"1) myDateTime       = {0}", __box(myDateTime));
// Reverse month and day to conform to a different culture.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
IFormatProvider* culture = new CultureInfo(S"fr-FR", true);
String* myDateTimeFrenchValue = S"    16/02/1992 12:15:12";
DateTime myDateTimeFrench =
DateTime::Parse(myDateTimeFrenchValue,
culture,
DateTimeStyles::NoCurrentDateDefault);
Console::WriteLine(S"2) myDateTimeFrench = {0}", __box(myDateTimeFrench));
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
String* expectedFormats[] = {S"G", S"g", S"f" , S"F"};
myDateTimeFrench =
DateTime::ParseExact(myDateTimeFrenchValue,
expectedFormats,
culture,
DateTimeStyles::AllowWhiteSpaces);
Console::WriteLine(S"3) myDateTimeFrench = {0}", __box(myDateTimeFrench));
}
/*
This example yields the following results:
1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮

要求

平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版, 公共语言基础结构 (CLI) 标准

请参见

注释:
string[] expectedFormats = {"G", "g", "f" ,"F"};的意思呢?如下

.NET Framework 类库  

DateTimeFormatInfo 类

定义如何根据区域性设置 DateTime 值的格式并显示这些值。

有关此类型所有成员的列表,请参阅 DateTimeFormatInfo 成员。

System.Object
   System.Globalization.DateTimeFormatInfo

[Visual Basic]
<Serializable>
NotInheritable Public Class DateTimeFormatInfo
Implements ICloneable, IFormatProvider
[C#]
[Serializable]
public sealed class DateTimeFormatInfo : ICloneable,
IFormatProvider
[C++]
[Serializable]
public __gc __sealed class DateTimeFormatInfo : public ICloneable,
IFormatProvider
[JScript]
public
Serializable
class DateTimeFormatInfo implements ICloneable,
IFormatProvider

线程安全

此类型的所有公共静态(Visual Basic 中为 Shared)成员是线程安全的。但不保证任何实例成员是线程安全的。

备注

此类包含各种信息,例如日期模式、时间模式和 AM/PM 指示项。

若要为特定区域性创建 DateTimeFormatInfo,请为该区域性创建 CultureInfo 并检索 CultureInfo.DateTimeFormat 属性。若要为当前线程的区域性创建 DateTimeFormatInfo,请使用 CurrentInfo 属性。若要为固定区域性创建 DateTimeFormatInfo,请将 InvariantInfo 属性用于只读版本,或将 DateTimeFormatInfo 构造函数用于可写版本。不可能为非特定区域性创建 DateTimeFormatInfo。

用户可以通过“控制面板”中的“区域和语言选项”(或“区域选项”或“区域设置”)选择重写某些与 Windows 的当前区域性关联的值。例如,用户可能选择以另一种格式显示日期,或选择使用区域性默认设置以外的货币。如果 CultureInfo.UseUserOverride 属性设置为 true,则还将从用户设置中检索 CultureInfo.DateTimeFormat 实例、CultureInfo.NumberFormat 实例和 CultureInfo.TextInfo 实例的属性。如果用户设置与 CultureInfo 的关联区域性不兼容(例如选定的日历不属于 OptionalCalendars 其中之一),则方法结果和属性值是未定义的。

使用在 DateTimeFormatInfo 的属性中存储的标准或自定义模式设置 DateTime 值的格式。

可以通过设置可写 DateTimeFormatInfo 的关联属性用自定义模式替代标准模式。若要确定 DateTimeFormatInfo 是否是可写的,请使用 IsReadOnly 属性。

下表列出了每一种标准模式的标准格式字符以及可以进行设置以修改标准模式的关联 DateTimeFormatInfo 属性。格式字符区分大小写;例如,“g”和“G”所代表的模式稍有不同。

格式字符 关联属性/说明
d ShortDatePattern
D LongDatePattern
f 完整日期和时间(长日期和短时间)
F FullDateTimePattern(长日期和长时间)
g 常规(短日期和短时间)
G 常规(短日期和长时间)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用于显示通用时间的格式
U 使用通用时间的完整日期和时间(长日期和长时间)
y、Y YearMonthPattern

下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。

格式模式 说明
d 月中的某一天。一位数的日期没有前导零。
dd 月中的某一天。一位数的日期有一个前导零。
ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd 周中某天的完整名称,在 DayNames 中定义。
M 月份数字。一位数的月份没有前导零。
MM 月份数字。一位数的月份有一个前导零。
MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM 月份的完整名称,在 MonthNames 中定义。
y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy 包括纪元的四位数的年份。
gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h 12 小时制的小时。一位数的小时数没有前导零。
hh 12 小时制的小时。一位数的小时数有前导零。
H 24 小时制的小时。一位数的小时数没有前导零。
HH 24 小时制的小时。一位数的小时数有前导零。
m 分钟。一位数的分钟数没有前导零。
mm 分钟。一位数的分钟数有一个前导零。
s 秒。一位数的秒数没有前导零。
ss 秒。一位数的秒数有一个前导零。
f 秒的小数精度为一位。其余数字被截断。
ff 秒的小数精度为两位。其余数字被截断。
fff 秒的小数精度为三位。其余数字被截断。
ffff 秒的小数精度为四位。其余数字被截断。
fffff 秒的小数精度为五位。其余数字被截断。
ffffff 秒的小数精度为六位。其余数字被截断。
fffffff 秒的小数精度为七位。其余数字被截断。
t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
: 在 TimeSeparator 中定义的默认时间分隔符。
/ 在 DateSeparator 中定义的默认日期分隔符。
% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
\ c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。

只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,

  • DateTime.ToString( "d") 返回 DateTime 值;“d”是标准短日期模式。
  • DateTime.ToString( "%d") 返回月中的某天;“%d”是自定义模式。
  • DateTime.ToString( "d ") 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。

只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。有关固定区域性、特定区域性和非特定区域性的更多信息,请参见 CultureInfo 类。

该类实现 ICloneable 接口以启用 DateTimeFormatInfo 对象的复制。它还实现 IFormatProvider 以便为应用程序提供格式化信息。

示例

[Visual Basic, C#, C++] 下面的代码示例使用格式字符输出 en-US 区域性的不同格式模式。该示例还将显示与格式字符关联的属性值。

[Visual Basic]
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class SamplesDTFI
Public Shared Sub Main()
' Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
Dim myDTFI As DateTimeFormatInfo = New CultureInfo("en-US", False).DateTimeFormat
' Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
' The Gregorian calendar is the default calendar for the en-US culture.
Dim myDT As New DateTime(2002, 1, 3)
' Displays the format pattern associated with each format character.
Console.WriteLine("FORMAT  en-US EXAMPLE")
Console.WriteLine("CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY")
Console.WriteLine()
Console.WriteLine("  d     {0}", myDT.ToString("d"))
Console.WriteLine("        {0} {1}", myDTFI.ShortDatePattern, "(ShortDatePattern)")
Console.WriteLine()
Console.WriteLine("  D     {0}", myDT.ToString("D"))
Console.WriteLine("        {0} {1}", myDTFI.LongDatePattern, "(LongDatePattern)")
Console.WriteLine()
Console.WriteLine("  f     {0}", myDT.ToString("f"))
Console.WriteLine()
Console.WriteLine("  F     {0}", myDT.ToString("F"))
Console.WriteLine("        {0} {1}", myDTFI.FullDateTimePattern, "(FullDateTimePattern)")
Console.WriteLine()
Console.WriteLine("  g     {0}", myDT.ToString("g"))
Console.WriteLine()
Console.WriteLine("  G     {0}", myDT.ToString("G"))
Console.WriteLine()
Console.WriteLine("  m     {0}", myDT.ToString("m"))
Console.WriteLine("        {0} {1}", myDTFI.MonthDayPattern, "(MonthDayPattern)")
Console.WriteLine()
Console.WriteLine("  M     {0}", myDT.ToString("M"))
Console.WriteLine("        {0} {1}", myDTFI.MonthDayPattern, "(MonthDayPattern)")
Console.WriteLine()
Console.WriteLine("  r     {0}", myDT.ToString("r"))
Console.WriteLine("        {0} {1}", myDTFI.RFC1123Pattern, "(RFC1123Pattern)")
Console.WriteLine()
Console.WriteLine("  R     {0}", myDT.ToString("R"))
Console.WriteLine("        {0} {1}", myDTFI.RFC1123Pattern, "(RFC1123Pattern)")
Console.WriteLine()
Console.WriteLine("  s     {0}", myDT.ToString("s"))
Console.WriteLine("        {0} {1}", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)")
Console.WriteLine()
Console.WriteLine("  t     {0}", myDT.ToString("t"))
Console.WriteLine("        {0} {1}", myDTFI.ShortTimePattern, "(ShortTimePattern)")
Console.WriteLine()
Console.WriteLine("  T     {0}", myDT.ToString("T"))
Console.WriteLine("        {0} {1}", myDTFI.LongTimePattern, "(LongTimePattern)")
Console.WriteLine()
Console.WriteLine("  u     {0}", myDT.ToString("u"))
Console.WriteLine("        {0} {1}", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)")
Console.WriteLine()
Console.WriteLine("  U     {0}", myDT.ToString("U"))
Console.WriteLine()
Console.WriteLine("  y     {0}", myDT.ToString("y"))
Console.WriteLine("        {0} {1}", myDTFI.YearMonthPattern, "(YearMonthPattern)")
Console.WriteLine()
Console.WriteLine("  Y     {0}", myDT.ToString("Y"))
Console.WriteLine("        {0} {1}", myDTFI.YearMonthPattern, "(YearMonthPattern)")
End Sub 'Main
End Class 'SamplesDTFI
'This code produces the following output.
'
'FORMAT  en-US EXAMPLE
'CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY
'
'  d     1/3/2002
'        M/d/yyyy (ShortDatePattern)
'
'  D     Thursday, January 03, 2002
'        dddd, MMMM dd, yyyy (LongDatePattern)
'
'  f     Thursday, January 03, 2002 12:00 AM
'
'  F     Thursday, January 03, 2002 12:00:00 AM
'        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
'
'  g     1/3/2002 12:00 AM
'
'  G     1/3/2002 12:00:00 AM
'
'  m     January 03
'        MMMM dd (MonthDayPattern)
'
'  M     January 03
'        MMMM dd (MonthDayPattern)
'
'  r     Thu, 03 Jan 2002 00:00:00 GMT
'        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
'
'  R     Thu, 03 Jan 2002 00:00:00 GMT
'        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
'
'  s     2002-01-03T00:00:00
'        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
'
'  t     12:00 AM
'        h:mm tt (ShortTimePattern)
'
'  T     12:00:00 AM
'        h:mm:ss tt (LongTimePattern)
'
'  u     2002-01-03 00:00:00Z
'        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
'
'  U     Thursday, January 03, 2002 8:00:00 AM
'
'  y     January, 2002
'        MMMM, yyyy (YearMonthPattern)
'
'  Y     January, 2002
'        MMMM, yyyy (YearMonthPattern)
'
[C#]
using System;
using System.Globalization;
public class SamplesDTFI  {
public static void Main()  {
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;
// Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
// The Gregorian calendar is the default calendar for the en-US culture.
DateTime myDT = new DateTime( 2002, 1, 3 );
// Displays the format pattern associated with each format character.
Console.WriteLine( "FORMAT  en-US EXAMPLE" );
Console.WriteLine( "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n" );
Console.WriteLine( "  d     {0}", myDT.ToString("d") );
Console.WriteLine( "        {0} {1}\n", myDTFI.ShortDatePattern, "(ShortDatePattern)" );
Console.WriteLine( "  D     {0}", myDT.ToString("D") );
Console.WriteLine( "        {0} {1}\n", myDTFI.LongDatePattern, "(LongDatePattern)" );
Console.WriteLine( "  f     {0}\n", myDT.ToString("f") );
Console.WriteLine( "  F     {0}", myDT.ToString("F") );
Console.WriteLine( "        {0} {1}\n", myDTFI.FullDateTimePattern, "(FullDateTimePattern)" );
Console.WriteLine( "  g     {0}\n", myDT.ToString("g") );
Console.WriteLine( "  G     {0}\n", myDT.ToString("G") );
Console.WriteLine( "  m     {0}", myDT.ToString("m") );
Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
Console.WriteLine( "  M     {0}", myDT.ToString("M") );
Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
Console.WriteLine( "  r     {0}", myDT.ToString("r") );
Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
Console.WriteLine( "  R     {0}", myDT.ToString("R") );
Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
Console.WriteLine( "  s     {0}", myDT.ToString("s") );
Console.WriteLine( "        {0} {1}\n", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" );
Console.WriteLine( "  t     {0}", myDT.ToString("t") );
Console.WriteLine( "        {0} {1}\n", myDTFI.ShortTimePattern, "(ShortTimePattern)" );
Console.WriteLine( "  T     {0}", myDT.ToString("T") );
Console.WriteLine( "        {0} {1}\n", myDTFI.LongTimePattern, "(LongTimePattern)" );
Console.WriteLine( "  u     {0}", myDT.ToString("u") );
Console.WriteLine( "        {0} {1}\n", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
Console.WriteLine( "  U     {0}\n", myDT.ToString("U") );
Console.WriteLine( "  y     {0}", myDT.ToString("y") );
Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
Console.WriteLine( "  Y     {0}", myDT.ToString("Y") );
Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
}
}
/*
This code produces the following output.
FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY
d     1/3/2002
M/d/yyyy (ShortDatePattern)
D     Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f     Thursday, January 03, 2002 12:00 AM
F     Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g     1/3/2002 12:00 AM
G     1/3/2002 12:00:00 AM
m     January 03
MMMM dd (MonthDayPattern)
M     January 03
MMMM dd (MonthDayPattern)
r     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s     2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t     12:00 AM
h:mm tt (ShortTimePattern)
T     12:00:00 AM
h:mm:ss tt (LongTimePattern)
u     2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U     Thursday, January 03, 2002 8:00:00 AM
y     January, 2002
MMMM, yyyy (YearMonthPattern)
Y     January, 2002
MMMM, yyyy (YearMonthPattern)
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
int main()
{
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
CultureInfo * MyCI = new CultureInfo(S"en-US", false);
DateTimeFormatInfo* myDTFI = MyCI -> DateTimeFormat;
// Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
// The Gregorian calendar is the default calendar for the en-US culture.
DateTime myDT = DateTime(2002, 1, 3);
// Displays the format pattern associated with each format character.
Console::WriteLine(S"FORMAT  en-US EXAMPLE");
Console::WriteLine(S"CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n");
Console::WriteLine(S"  d {0}", myDT.ToString(S"d"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> ShortDatePattern, S"(ShortDatePattern)");
Console::WriteLine(S"  D {0}", myDT.ToString(S"D"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> LongDatePattern, S"(LongDatePattern)");
Console::WriteLine(S"  f {0}\n", myDT.ToString(S"f"));
Console::WriteLine(S"  F {0}", myDT.ToString(S"F"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> FullDateTimePattern, S"(FullDateTimePattern)");
Console::WriteLine(S"  g {0}\n", myDT.ToString(S"g"));
Console::WriteLine(S"  G {0}\n", myDT.ToString(S"G"));
Console::WriteLine(S"  m {0}", myDT.ToString(S"m"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> MonthDayPattern, S"(MonthDayPattern)");
Console::WriteLine(S"  M {0}", myDT.ToString(S"M"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> MonthDayPattern, S"(MonthDayPattern)");
Console::WriteLine(S"  r {0}", myDT.ToString(S"r"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> RFC1123Pattern, S"(RFC1123Pattern)");
Console::WriteLine(S"  R {0}", myDT.ToString(S"R"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> RFC1123Pattern, S"(RFC1123Pattern)");
Console::WriteLine(S"  s {0}", myDT.ToString(S"s"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> SortableDateTimePattern, S"(SortableDateTimePattern)");
Console::WriteLine(S"  t {0}", myDT.ToString(S"t"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> ShortTimePattern, S"(ShortTimePattern)");
Console::WriteLine(S"  T {0}", myDT.ToString(S"T"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> LongTimePattern, S"(LongTimePattern)");
Console::WriteLine(S"  u {0}", myDT.ToString(S"u"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> UniversalSortableDateTimePattern, S"(UniversalSortableDateTimePattern)");
Console::WriteLine(S"  U {0}\n", myDT.ToString(S"U"));
Console::WriteLine(S"  y {0}", myDT.ToString(S"y"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> YearMonthPattern, S"(YearMonthPattern)");
Console::WriteLine(S"  Y {0}", myDT.ToString(S"Y"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> YearMonthPattern, S"(YearMonthPattern)");
}
/*
This code produces the following output.
FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY
d     1/3/2002
M/d/yyyy (ShortDatePattern)
D     Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f     Thursday, January 03, 2002 12:00 AM
F     Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g     1/3/2002 12:00 AM
G     1/3/2002 12:00:00 AM
m     January 03
MMMM dd (MonthDayPattern)
M     January 03
MMMM dd (MonthDayPattern)
r     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s     2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t     12:00 AM
h:mm tt (ShortTimePattern)
T     12:00:00 AM
h:mm:ss tt (LongTimePattern)
u     2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U     Thursday, January 03, 2002 8:00:00 AM
y     January, 2002
MMMM, yyyy (YearMonthPattern)
Y     January, 2002
MMMM, yyyy (YearMonthPattern)
*/
本文转自kenty博客园博客,原文链接http://www.cnblogs.com/kentyshang/archive/2006/06/15/426503.html如需转载请自行联系原作者
kenty

System.Globalization 命名空间相关推荐

  1. C#中的System.Speech命名空间初探

    本程序是口算两位数乘法,随机生成两个两位数,用语音读出来.然后开启语音识别,接受用户输入,知道答案正确关闭语音识别.用户说答案时,可以说"再说一遍"重复题目. 关键是Grammar ...

  2. System.Globalization.CultureInfo.InvariantCulture失效

    dataset ds; ds.Locale = System.Globalization.CultureInfo.InvariantCulture;  //失效就用下面的  System.Thread ...

  3. System.Globalization.CultureInfo.InvariantCulture 解决不同地域字符串格式不同问题

    同样的DateTime.ToShortDateString()   在不同的地域输出格式不同 如在美国的 日期格式为 :  月-日-年 如在中国的 日期格式为 :  年-月-日 一些时候,这个格式就会 ...

  4. using System.Linq命名空间无法引用!的解决方案

    Visual Studio 2008中当建立新文件时,遇到这样的问题: "using System.Linq命名空间无法引用!!!" 解决方法:在解决方案上点击右键--添加引用-- ...

  5. C# 特性 System.ComponentModel 命名空间属性方法大全,System.ComponentModel 命名空间的特性...

    目录: System.ComponentModel 特性命名空间与常用类 System.ComponentModel.DataAnnotations ComponentModel - Classes ...

  6. System.Configuration命名空间下的关键类

    1.ConfigurationManager和 WebConfigurationManager类:      使用 ConfigurationManager 类,可以访问计算机和应用程序的配置信息.C ...

  7. System.IO命名空间

    从上面那张图可以看出Directory和File类型直接扩展了System.Object,而DirectoryInfo和FileInfo则是从FileSystemInfo抽象类派生的 System.I ...

  8. .NET Framework 类库——C#命名空间大全

    引用地址:https://msdn.microsoft.com/zh-cn/library/gg145045.aspx C# using引用时,不知道有哪些命名空间,这下转载收集一篇,方面查找使用. ...

  9. 一些很酷的.Net技

    一..Net Framework 1.如何获得系统文件夹 使用System.Envioment类的GetFolderPath方法:例如: Environment.GetFolderPath( Envi ...

最新文章

  1. 没有数据的MySql导出架构
  2. 搭建Android上的服务器 “实现隔空取物”
  3. 管理active directiory中的用户和计算机管理磁盘,IP多播桌面视频会议系统媒体流管理与安全机制的分析.pdf...
  4. Citrix xenapp
  5. 关于 Angular view Query 的 id 选择器问题的单步调试
  6. python 爬虫爬取小说信息
  7. Knowledge Test about Match
  8. python函数的继承_Python 继承
  9. 一段话系列-正则基础
  10. C语言和设计模式(工厂模式)
  11. 不是区块链的特征_区块链的四大特征
  12. 七. 并发编程 (进程队列)
  13. STM8S系列学习第一周目:1.用STVD组建第一个项目创建工程
  14. ITIL、COBIT、CMMi和ISO 17799管理新一代数据中心的最佳实践介绍
  15. Pandas:用Python做日报(重构)
  16. java实现发送国际短信的功能
  17. w ndows7家庭版好不好用,windows7怎么样 windows7哪个版本好用【详细介绍】
  18. 半监督分类算法简述,self-trainning,co-trainning
  19. 漂亮,这张 动态可视化 交互大屏图,也是用Python实现的!
  20. 数据库MySQL错误2058_SQLyog连接MySQL8.0报2058错误的完美解决方法

热门文章

  1. Flutter——路由和导航
  2. ASP.NET Core 技术内幕与项目实战读后感
  3. 全国计算机等级考试技巧,全国计算机等级考试二级复习指导及应试技巧
  4. 【Leetcode】1564. Put Boxes Into the Warehouse I
  5. Linux下查看硬盘序列号、设备序列号、操作系统版本和安装时间、系统启用时间等命令
  6. matlab 画图篇(2/3)
  7. 机器学习Python相关套件(np, plt, pd)
  8. tx2 can通信之开机自动加载can模块
  9. 10-230 查询计算机工程专业学生选修但软件工程专业学生没有选修的课程
  10. 页面验证是否是真实有效的身份证号码