定义如何根据区域性设置 DateTime 值的格式并显示这些值
来源:互联网 发布:nginx ip映射域名访问 编辑:程序博客网 时间:2024/05/18 03:25
默认格式为:2005-6-6 14:33:34
如果要换成成200506,06-2005,2005-6-6或更多的该怎么办呢
我们要用到:DateTime.ToString的方法(String, IFormatProvider)
using System;
using System.Globalization;
String format="D";
DateTime date=DataTime,Now;
Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));
结果输出
Thursday, June 16, 2005
参数format格式详细用法
格式字符 关联属性/说明
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”是自定义模式。
比较方便的是,上面的参数可以随意组合,并且不会出错,多试试,肯定会找到你要的时间格式
如要得到2005年06月 这样格式的时间
可以这样写:
date.ToString("yyyy年MM月", DateTimeFormatInfo.InvariantInfo)
定义如何根据区域性设置 DateTime 值的格式并显示这些值。
命名空间:System.Globalization
程序集:mscorlib(在 mscorlib.dll 中)
<SerializableAttribute> _<ComVisibleAttribute(True)> _Public NotInheritable Class DateTimeFormatInfo Implements ICloneable, IFormatProvider
Dim instance As DateTimeFormatInfo
[SerializableAttribute] [ComVisibleAttribute(true)] public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class DateTimeFormatInfo sealed : ICloneable, IFormatProvider
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ public final class DateTimeFormatInfo implements ICloneable, IFormatProvider
SerializableAttribute ComVisibleAttribute(true) public final class DateTimeFormatInfo implements ICloneable, IFormatProvider
此类包含各种信息,例如日期模式、时间模式和 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、%d
月中的某一天。一位数的日期没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%d”。
dd
月中的某一天。一位数的日期有一个前导零。
ddd
周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd
周中某天的完整名称,在 DayNames 中定义。
M、%M
月份数字。一位数的月份没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%M”。
MM
月份数字。一位数的月份有一个前导零。
MMM
月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM
月份的完整名称,在 MonthNames 中定义。
y、%y
不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。如果该格式模式没有与其他格式模式组合,则指定“%y”。
yy
不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy
包括纪元的四位数的年份。
gg
时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h、%h
12 小时制的小时。一位数的小时数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%h”。
hh
12 小时制的小时。一位数的小时数有前导零。
H、%H
24 小时制的小时。一位数的小时数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%H”。
HH
24 小时制的小时。一位数的小时数有前导零。
m、%m
分钟。一位数的分钟数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%m”。
mm
分钟。一位数的分钟数有一个前导零。
s、%s
秒。一位数的秒数没有前导零。如果该格式模式没有与其他格式模式组合,则指定“%s”。
ss
秒。一位数的秒数有一个前导零。
f、%f
秒的小数精度为一位。其余数字被截断。如果该格式模式没有与其他格式模式组合,则指定“%f”。
ff
秒的小数精度为两位。其余数字被截断。
fff
秒的小数精度为三位。其余数字被截断。
ffff
秒的小数精度为四位。其余数字被截断。
fffff
秒的小数精度为五位。其余数字被截断。
ffffff
秒的小数精度为六位。其余数字被截断。
fffffff
秒的小数精度为七位。其余数字被截断。
F、%F
显示秒的小数部分的最高有效数字。如果该数字为零,则不显示任何内容。如果该格式模式没有与其他格式模式组合,则指定“%F”。
FF
显示秒的小数部分的两个最高有效数字。但是,不显示尾随的零(两个零数字)。
FFF
显示秒的小数部分的三个最高有效数字。但是,不显示尾随的零(三个零数字)。
FFFF
显示秒的小数部分的四个最高有效数字。但是,不显示尾随的零(四个零数字)。
FFFFF
显示秒的小数部分的五个最高有效数字。但是,不显示尾随的零(五个零数字)。
FFFFFF
显示秒的小数部分的六个最高有效数字。但是,不显示尾随的零(六个零数字)。
FFFFFFF
显示秒的小数部分的七个最高有效数字。但是,不显示尾随的零(七个零数字)。
t、%t
在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。如果该格式模式没有与其他格式模式组合,则指定“%t”。
tt
在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
z、%z
时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。如果该格式模式没有与其他格式模式组合,则指定“%z”。
zz
时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz
完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
:
在 TimeSeparator 中定义的默认时间分隔符。
/
在 DateSeparator 中定义的默认日期分隔符。
% c
其中 c 是格式模式(如果单独使用)。也就是说,若要单独使用格式模式“d”、“f”、“F”、“h”、“m”、“s”、“t”、“y”、“z”、“H”或“M”,请指定“%d”、“%f”、“%F”、“%h”、“%m”、“%s”、“%t”、“%y”、“%z”、“%H”或“%M”。
如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
/ c
其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“//”。
只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,
DateTime.ToString( "d" ) 返回 DateTime 值;“d”是标准短日期模式。
DateTime.ToString( "%d" ) 返回月中的某天;“%d”是自定义模式。
DateTime.ToString( "d " ) 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。
只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。有关固定区域性、特定区域性和非特定区域性的更多信息,请参见 CultureInfo 类。
该类实现 ICloneable 接口以启用 DateTimeFormatInfo 对象的复制。它还实现 IFormatProvider 以便为应用程序提供格式化信息。
下面的代码示例使用格式字符输出 en-US 区域性的不同格式模式。该示例还将显示与格式字符关联的属性值。
Imports SystemImports System.GlobalizationImports Microsoft.VisualBasicPublic 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)'
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 EXAMPLECHAR 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)*/
using namespace System;using namespace System::Globalization;int main(){ // Creates and initializes a DateTimeFormatInfo associated with the en-US culture. CultureInfo^ MyCI = gcnew CultureInfo( "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( "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 EXAMPLECHAR VALUE OF ASSOCIATED PROPERTY, IF ANYd 1/3/2002M/d/yyyy (ShortDatePattern)D Thursday, January 03, 2002dddd, MMMM dd, yyyy (LongDatePattern)f Thursday, January 03, 2002 12:00 AMF Thursday, January 03, 2002 12:00:00 AMdddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)g 1/3/2002 12:00 AMG 1/3/2002 12:00:00 AMm January 03MMMM dd (MonthDayPattern)M January 03MMMM dd (MonthDayPattern)r Thu, 03 Jan 2002 00:00:00 GMTddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)R Thu, 03 Jan 2002 00:00:00 GMTddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)s 2002-01-03T00:00:00yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)t 12:00 AMh:mm tt (ShortTimePattern)T 12:00:00 AMh:mm:ss tt (LongTimePattern)u 2002-01-03 00:00:00Zyyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)U Thursday, January 03, 2002 8:00:00 AMy January, 2002MMMM, yyyy (YearMonthPattern)Y January, 2002MMMM, yyyy (YearMonthPattern)*/
import System.* ;import System.Globalization.* ;public class SamplesDTFI{ public static void main(String[] args) { // Creates and initializes a DateTimeFormatInfo associated with // the en-US culture. DateTimeFormatInfo myDTFI = (new CultureInfo("en-US", false)).get_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.get_ShortDatePattern(), "(ShortDatePattern)"); Console.WriteLine(" D {0}", myDT.ToString("D")); Console.WriteLine(" {0} {1}/n", myDTFI.get_LongDatePattern(), "(LongDatePattern)"); Console.WriteLine(" f {0}/n", myDT.ToString("f")); Console.WriteLine(" F {0}", myDT.ToString("F")); Console.WriteLine(" {0} {1}/n", myDTFI.get_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.get_MonthDayPattern(), "(MonthDayPattern)"); Console.WriteLine(" M {0}", myDT.ToString("M")); Console.WriteLine(" {0} {1}/n", myDTFI.get_MonthDayPattern(), "(MonthDayPattern)"); Console.WriteLine(" r {0}", myDT.ToString("r")); Console.WriteLine(" {0} {1}/n", myDTFI.get_RFC1123Pattern(), "(RFC1123Pattern)"); Console.WriteLine(" R {0}", myDT.ToString("R")); Console.WriteLine(" {0} {1}/n", myDTFI.get_RFC1123Pattern(), "(RFC1123Pattern)"); Console.WriteLine(" s {0}", myDT.ToString("s")); Console.WriteLine(" {0} {1}/n", myDTFI.get_SortableDateTimePattern(), "(SortableDateTimePattern)"); Console.WriteLine(" t {0}", myDT.ToString("t")); Console.WriteLine(" {0} {1}/n", myDTFI.get_ShortTimePattern(), "(ShortTimePattern)"); Console.WriteLine(" T {0}", myDT.ToString("T")); Console.WriteLine(" {0} {1}/n", myDTFI.get_LongTimePattern(), "(LongTimePattern)"); Console.WriteLine(" u {0}", myDT.ToString("u")); Console.WriteLine(" {0} {1}/n", myDTFI.get_UniversalSortableDateTimePattern(), "(UniversalSortableDateTimePattern)"); Console.WriteLine(" U {0}/n", myDT.ToString("U")); Console.WriteLine(" y {0}", myDT.ToString("y")); Console.WriteLine(" {0} {1}/n", myDTFI.get_YearMonthPattern(), "(YearMonthPattern)"); Console.WriteLine(" Y {0}", myDT.ToString("Y")); Console.WriteLine(" {0} {1}/n", myDTFI.get_YearMonthPattern(), "(YearMonthPattern)"); } //main } //SamplesDTFI/*This code produces the following output.FORMAT en-US EXAMPLECHAR 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)*/
System.Globalization.DateTimeFormatInfo
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0、1.1、1.0.NET Compact Framework
受以下版本支持:2.0、1.0参考
DateTimeFormatInfo 成员System.Globalization 命名空间
DateTime
Calendar 类
CultureInfo 类
其他资源
格式化类型格式化概述
标准 DateTime 格式字符串
自定义 DateTime 格式字符串
- 定义如何根据区域性设置 DateTime 值的格式并显示这些值。
- 定义如何根据区域性设置 DateTime 值的格式并显示这些值
- gdb调试宏定义并显示宏定义的值
- DateTime.ToString()的几种显示格式
- gdb如何显示宏定义的值
- 【C# DateTime】如何一次性修改DateTime的默认ToString格式
- ajax根据ID查询数据库并返回Json格式数据返回js,使用append显示到页面。判断json值为[]或者[[]]的问题。
- 如何:为 ASP.NET 网页全球化设置区域性和 UI 区域性
- 定义xml为drawable文件并根据状态改变显示的图片资源
- MpAndroidChart如何根据Y轴值设置不同的颜色
- 根据时间戳获取剩余时间,并显示时分秒格式
- 如何设置jfreechart周报曲线图的时间轴显示格式
- flex中如何对数据的显示格式进行设置
- 如何使DataGrid(Web)的不同行根据不同的条件有不同的显示格式?
- silverlight 设置线程的区域性来设置语言
- Linux -gdb如何显示宏定义的值
- js获取并设置<p></p>的显示的值。
- 根据输入值定义的数组大小
- proc g3d VS scatterplot3d (R)
- VSTO:无法安装此应用程序,因为已安装具有相同标识的应用程序
- android 4.0为系统增加虚拟导航栏
- java collections和collection的区别
- aperte-reports 介绍
- 定义如何根据区域性设置 DateTime 值的格式并显示这些值
- word中visio只显示边框,不显示内容解决
- ubuntu 下中文目录变为英文
- word2003图表编号
- 突然听到一只公鸡的局域网聊天软件
- Android单元测试
- 一个css javascript的二级横向菜单
- WPF入门教程系列(一) 创建你的第一个WPF项目
- java中OOA,OOT, OOP, OOD, OOSM,OOM英文表示的含义是什么?