.NET DateTime 显示格式

备注
 
 

format 参数应包含单个格式说明符 (请参阅 标准日期和时间格式字符串) 或自定义格式模式 (请参阅 Cadenas con formato de fecha y hora personalizado),它定义了返回字符串的格式。如果 format 是 null 或使用空字符串,常规格式说明符,G。

此方法使用派生自当前区域性的格式设置信息。有关详细信息,请参阅CurrentCulture

对调用方的说明:

ToString(String) 方法使用由当前区域性的日历中返回的字符串表示形式的日期和时间。如果当前值 DateTime 实例是早于Calendar.MinSupportedDateTime 或更高版本比 Calendar.MaxSupportedDateTime, ,该方法将引发 ArgumentOutOfRangeException。下面的示例进行了这方面的演示。它会尝试设置的范围之外的日期的格式 HebrewCalendar 类在当前区域性为希伯来语 (以色列) 时。

示例
using System;
using System.Globalization;
using System.Threading; public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(, , );
CultureInfo dft;
CultureInfo heIL = new CultureInfo("he-IL");
heIL.DateTimeFormat.Calendar = new HebrewCalendar(); // Change current culture to he-IL.
dft = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = heIL; // Display the date using the current culture's calendar.
try {
Console.WriteLine(date1.ToString("G"));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0} is earlier than {1} or later than {2}",
date1.ToString("d", CultureInfo.InvariantCulture),
heIL.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
heIL.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture));
} // Restore the default culture.
Thread.CurrentThread.CurrentCulture = dft;
}
}
// The example displays the following output:
// 07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239

下面的示例使用的每个标准日期和时间格式字符串和选定的自定义日期和时间格式字符串来显示的字符串表示形式 DateTime 值。该示例的线程当前区域性为 EN-US。

using System;

public class DateToStringExample
{
public static void Main()
{
DateTime dateValue = new DateTime(, , , , , );
// Create an array of standard format strings.
string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o",
"R", "s", "t", "T", "u", "U", "y"};
// Output date and time using each standard format string.
foreach (string standardFmt in standardFmts)
Console.WriteLine("{0}: {1}", standardFmt,
dateValue.ToString(standardFmt));
Console.WriteLine(); // Create an array of some custom format strings.
string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f",
"dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
// Output date and time using each custom format string.
foreach (string customFmt in customFmts)
Console.WriteLine("'{0}': {1}", customFmt,
dateValue.ToString(customFmt));
}
}
// This example displays the following output to the console:
// d: 6/15/2008
// D: Sunday, June 15, 2008
// f: Sunday, June 15, 2008 9:15 PM
// F: Sunday, June 15, 2008 9:15:07 PM
// g: 6/15/2008 9:15 PM
// G: 6/15/2008 9:15:07 PM
// m: June 15
// o: 2008-06-15T21:15:07.0000000
// R: Sun, 15 Jun 2008 21:15:07 GMT
// s: 2008-06-15T21:15:07
// t: 9:15 PM
// T: 9:15:07 PM
// u: 2008-06-15 21:15:07Z
// U: Monday, June 16, 2008 4:15:07 AM
// y: June, 2008
//
// 'h:mm:ss.ff t': 9:15:07.00 P
// 'd MMM yyyy': 15 Jun 2008
// 'HH:mm:ss.f': 21:15:07.0
// 'dd MMM HH:mm:ss': 15 Jun 21:15:07
// '\Mon\t\h\: M': Month: 6
// 'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00

连接:https://msdn.microsoft.com/zh-cn/library/zdtaw1bw(v=vs.110).aspx

上一篇:Windows下免费的屏幕录制软件——EV录屏——推荐


下一篇:ionic 侧栏菜单用法