Содержание
- 1 Culture-insensitive DateTime format strings
- 2 Culture-sensitive DateTime format strings
- 3 DateTime Formatting
- 4 Format time and date: {0:hh:mm tt}
- 5 Format time and date: 24 hour time is {0:HH:mm}
- 6 Format time and date: Date is {0:ddd MMM dd, yyyy}
- 7 Format time and date: Date is {0:gg}
- 8 Format time and date information.
- 9 Format time and date: Time with seconds{0:HH:mm:ss tt}
- 10 Format time and date: Use m for day of month{0:m}
- 11 Format time and date: use m for minutes: {0:%m}
- 12 Pre-built date/time specifiers
- 13 Use the ToLongDateString() and ToShortDateString() methods to convert the date parts of a DateTime to long and short date strings
- 14 Use the ToLongTimeString() and ToShortTimeString() methods to convert the time parts of a DateTime to long and short time strings
- 15 Use the ToString() method to convert a DateTime to a string: d, D, f, F, g, G, m ,r, s, t,T, u, U, y
- 16 Use the ToString() method to convert a DateTime to a string: MMMM dd, yyyy
Culture-insensitive DateTime format strings
Format string Meaning Sample output
o Round-trippable 2010-01-02T17:11:30.0000000
r, R RFC 1123 standard Sun, 02 Jan 2000 17:11:30 GMT
s Sortable; ISO 8601 2010-01-02T17:11:30
u "Universal" Sortable 2010-01-02 17:11:30Z
U UTC Sunday, 02 January 2000 08:11:30
Culture-sensitive DateTime format strings
Format string Meaning Sample output
d Short date 01/02/2010
D Long date Sunday, 02 January 2010
t Short time 17:18
T Long time 17:18:19
f Long date + short time Sunday, 02 January 2010 17:18
F Long date + long time Sunday, 02 January 2010 17:18:19
g Short date + short time 01/02/2010 17:18
G (default) Short date + long time 01/02/2010 17:18:19
m, M Month and day January 02
y, Y Year and month 2010 January
DateTime Formatting
Format Character Format Pattern Associated Property/ Description
D MM/dd/yyyy ShortDatePattern
D dddd, MMMM dd, yyyy LongDatePattern
F dddd, MMMM dd, yyyy HH:mm Full date and time
F dddd, MMMM dd, yyyy HH:mm:ss FullDateTimePattern
G MM/dd/yyyy HH:mm General
G MM/dd/yyyy HH:mm:ss General
M, M MMMM dd MonthDayPattern
r, R ddd, dd MMM yyyy HH":"mm":"ss "GMT" RFC1123Pattern
S yyyy-MM-dd HH:mm:ss SortableDateTimePattern using local time
T HH:mm ShortTimePattern
T HH:mm:ss LongTimePattern
U yyyy-MM-dd HH:mm:ss UniversalSortableDateTimePattern using universal time
U dddd, MMMM dd, yyyy HH:mm:ss UniversalSortableDateTimePattern
y, Y MMMM, yyyy YearMonthPattern
Format time and date: {0:hh:mm tt}
using System;
class MainClass {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("Time is {0:hh:mm tt}", dt);
}
}
Time is 01:53 PM
Format time and date: 24 hour time is {0:HH:mm}
using System;
class MainClass {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("24 hour time is {0:HH:mm}", dt);
}
}
24 hour time is 13:53
Format time and date: Date is {0:ddd MMM dd, yyyy}
using System;
class MainClass {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("Date is {0:ddd MMM dd, yyyy}", dt);
}
}
Date is Sun Mar 25, 2007
Format time and date: Date is {0:gg}
using System;
class MainClass {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("Era: {0:gg}", dt);
}
}
Era: A.D.
Format time and date information.
using System;
class MainClass {
public static void Main() {
DateTime dt = DateTime.Now; // obtain current time
Console.WriteLine("d format: {0:d}", dt);
Console.WriteLine("D format: {0:D}", dt);
Console.WriteLine("t format: {0:t}", dt);
Console.WriteLine("T format: {0:T}", dt);
Console.WriteLine("f format: {0:f}", dt);
Console.WriteLine("F format: {0:F}", dt);
Console.WriteLine("g format: {0:g}", dt);
Console.WriteLine("G format: {0:G}", dt);
Console.WriteLine("m format: {0:m}", dt);
Console.WriteLine("M format: {0:M}", dt);
Console.WriteLine("r format: {0:r}", dt);
Console.WriteLine("R format: {0:R}", dt);
Console.WriteLine("s format: {0:s}", dt);
Console.WriteLine("u format: {0:u}", dt);
Console.WriteLine("U format: {0:U}", dt);
Console.WriteLine("y format: {0:y}", dt);
Console.WriteLine("Y format: {0:Y}", dt);
}
}
d format: 25/03/2007
D format: March 25, 2007
t format: 1:53 PM
T format: 1:53:55 PM
f format: March 25, 2007 1:53 PM
F format: March 25, 2007 1:53:55 PM
g format: 25/03/2007 1:53 PM
G format: 25/03/2007 1:53:55 PM
m format: March 25
M format: March 25
r format: Sun, 25 Mar 2007 13:53:55 GMT
R format: Sun, 25 Mar 2007 13:53:55 GMT
s format: 2007-03-25T13:53:55
u format: 2007-03-25 13:53:55Z
U format: March 25, 2007 8:53:55 PM
y format: March, 2007
Y format: March, 2007
Format time and date: Time with seconds{0:HH:mm:ss tt}
using System;
class MainClass {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("Time with seconds: " + "{0:HH:mm:ss tt}", dt);
}
}
Time with seconds: 13:53:57 PM
Format time and date: Use m for day of month{0:m}
using System;
class MainClass {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("Use m for day of month: {0:m}", dt);
}
}
Use m for day of month: March 25
Format time and date: use m for minutes: {0:%m}
using System;
class MainClass {
public static void Main() {
DateTime dt = DateTime.Now;
Console.WriteLine("use m for minutes: {0:%m}", dt);
}
}
use m for minutes: 53
Pre-built date/time specifiers
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class MainClass
{
public static void Main()
{
DateTime dt1 = new DateTime(2004, 10, 9, 22, 47, 35, 259);
DateTimeFormatInfo di = new DateTimeFormatInfo();
for (char c = "a"; c <= "z"; c++)
{
try
{
foreach (string s in di.GetAllDateTimePatterns(c))
{
Console.WriteLine(""{0}": {1} - {2}/{3}", c, s,
dt1.ToString(c.ToString()), dt1.ToString(s));
}
char cUpper = Char.ToUpper(c);
foreach (string s in di.GetAllDateTimePatterns(cUpper))
{
Console.WriteLine(""{0}": {1} - {2}", cUpper, s,
dt1.ToString(cUpper.ToString()), dt1.ToString(s));
}
}
catch (ArgumentException)
{
// Ignore--specifier not found.
}
}
}
}
"d": MM/dd/yyyy - 09/10/2004/10/09/2004
"D": dddd, dd MMMM yyyy - October 9, 2004
"f": dddd, dd MMMM yyyy HH:mm - October 9, 2004 10:47 PM/Saturday, 09 October 2004 22:47
"f": dddd, dd MMMM yyyy hh:mm tt - October 9, 2004 10:47 PM/Saturday, 09 October 2004 10:47 PM
"f": dddd, dd MMMM yyyy H:mm - October 9, 2004 10:47 PM/Saturday, 09 October 2004 22:47
"f": dddd, dd MMMM yyyy h:mm tt - October 9, 2004 10:47 PM/Saturday, 09 October 2004 10:47 PM
"F": dddd, dd MMMM yyyy HH:mm:ss - October 9, 2004 10:47:35 PM
"g": MM/dd/yyyy HH:mm - 09/10/2004 10:47 PM/10/09/2004 22:47
"g": MM/dd/yyyy hh:mm tt - 09/10/2004 10:47 PM/10/09/2004 10:47 PM
"g": MM/dd/yyyy H:mm - 09/10/2004 10:47 PM/10/09/2004 22:47
"g": MM/dd/yyyy h:mm tt - 09/10/2004 10:47 PM/10/09/2004 10:47 PM
"G": MM/dd/yyyy HH:mm:ss - 09/10/2004 10:47:35 PM
"m": MMMM dd - October 09/October 09
"M": MMMM dd - October 09
"o": yyyy"-"MM"-"dd"T"HH":"mm":"ss.fffffffK - 2004-10-09T22:47:35.2590000/2004-10-09T22:47:35.259000
0
"O": yyyy"-"MM"-"dd"T"HH":"mm":"ss.fffffffK - 2004-10-09T22:47:35.2590000
"r": ddd, dd MMM yyyy HH":"mm":"ss "GMT" - Sat, 09 Oct 2004 22:47:35 GMT/Sat, 09 Oct 2004 22:47:35 G
MT
"R": ddd, dd MMM yyyy HH":"mm":"ss "GMT" - Sat, 09 Oct 2004 22:47:35 GMT
"s": yyyy"-"MM"-"dd"T"HH":"mm":"ss - 2004-10-09T22:47:35/2004-10-09T22:47:35
"t": HH:mm - 10:47 PM/22:47
"t": hh:mm tt - 10:47 PM/10:47 PM
"t": H:mm - 10:47 PM/22:47
"t": h:mm tt - 10:47 PM/10:47 PM
"T": HH:mm:ss - 10:47:35 PM
"u": yyyy"-"MM"-"dd HH":"mm":"ss"Z" - 2004-10-09 22:47:35Z/2004-10-09 22:47:35Z
"U": dddd, dd MMMM yyyy HH:mm:ss - October 10, 2004 5:47:35 AM
"y": yyyy MMMM - October, 2004/2004 October
"Y": yyyy MMMM - October, 2004
Use the ToLongDateString() and ToShortDateString() methods to convert the date parts of a DateTime to long and short date strings
using System;
class MainClass
{
public static void Main()
{
DateTime myDateTime11 = new DateTime(2004, 1, 15, 23, 2, 5);
Console.WriteLine("myDateTime11 = " + myDateTime11);
Console.WriteLine("myDateTime11.ToLongDateString() = " + myDateTime11.ToLongDateString());
Console.WriteLine("myDateTime11.ToShortDateString() = " + myDateTime11.ToShortDateString());
}
}
myDateTime11 = 15/01/2004 11:02:05 PM
myDateTime11.ToLongDateString() = January 15, 2004
myDateTime11.ToShortDateString() = 15/01/2004
Use the ToLongTimeString() and ToShortTimeString() methods to convert the time parts of a DateTime to long and short time strings
using System;
class MainClass
{
public static void Main()
{
DateTime myDateTime11 = new DateTime(2004, 1, 15, 23, 2, 5);
Console.WriteLine("myDateTime11 = " + myDateTime11);
Console.WriteLine("myDateTime11.ToLongTimeString() = " + myDateTime11.ToLongTimeString());
Console.WriteLine("myDateTime11.ToShortTimeString() = " + myDateTime11.ToShortTimeString());
}
}
myDateTime11 = 15/01/2004 11:02:05 PM
myDateTime11.ToLongTimeString() = 11:02:05 PM
myDateTime11.ToShortTimeString() = 11:02 PM
Use the ToString() method to convert a DateTime to a string: d, D, f, F, g, G, m ,r, s, t,T, u, U, y
using System;
class MainClass
{
public static void Main()
{
DateTime myDateTime = new DateTime(2004, 1, 12, 22, 2, 10);
Console.WriteLine("myDateTime.ToString() = " + myDateTime.ToString());
Console.WriteLine("myDateTime.ToString(\"d\") = " + myDateTime.ToString("d"));
Console.WriteLine("myDateTime.ToString(\"D\") = " + myDateTime.ToString("D"));
Console.WriteLine("myDateTime.ToString(\"f\") = " + myDateTime.ToString("f"));
Console.WriteLine("myDateTime.ToString(\"F\") = " + myDateTime.ToString("F"));
Console.WriteLine("myDateTime.ToString(\"g\") = " + myDateTime.ToString("g"));
Console.WriteLine("myDateTime.ToString(\"G\") = " + myDateTime.ToString("G"));
Console.WriteLine("myDateTime.ToString(\"m\") = " + myDateTime.ToString("m"));
Console.WriteLine("myDateTime.ToString(\"r\") = " + myDateTime.ToString("r"));
Console.WriteLine("myDateTime.ToString(\"s\") = " + myDateTime.ToString("s"));
Console.WriteLine("myDateTime.ToString(\"t\") = " + myDateTime.ToString("t"));
Console.WriteLine("myDateTime.ToString(\"T\") = " + myDateTime.ToString("T"));
Console.WriteLine("myDateTime.ToString(\"u\") = " + myDateTime.ToString("u"));
Console.WriteLine("myDateTime.ToString(\"U\") = " + myDateTime.ToString("U"));
Console.WriteLine("myDateTime.ToString(\"y\") = " + myDateTime.ToString("y"));
}
}
myDateTime.ToString() = 12/01/2004 10:02:10 PM
myDateTime.ToString("d") = 12/01/2004
myDateTime.ToString("D") = January 12, 2004
myDateTime.ToString("f") = January 12, 2004 10:02 PM
myDateTime.ToString("F") = January 12, 2004 10:02:10 PM
myDateTime.ToString("g") = 12/01/2004 10:02 PM
myDateTime.ToString("G") = 12/01/2004 10:02:10 PM
myDateTime.ToString("m") = January 12
myDateTime.ToString("r") = Mon, 12 Jan 2004 22:02:10 GMT
myDateTime.ToString("s") = 2004-01-12T22:02:10
myDateTime.ToString("t") = 10:02 PM
myDateTime.ToString("T") = 10:02:10 PM
myDateTime.ToString("u") = 2004-01-12 22:02:10Z
myDateTime.ToString("U") = January 13, 2004 6:02:10 AM
myDateTime.ToString("y") = January, 2004
Use the ToString() method to convert a DateTime to a string: MMMM dd, yyyy
using System;
class MainClass
{
public static void Main()
{
DateTime myDateTime = new DateTime(2004, 1, 12, 22, 2, 10);
Console.WriteLine("myDateTime.ToString() = " + myDateTime.ToString());
Console.WriteLine("myDateTime.ToString(\"MMMM dd, yyyy\") = " + myDateTime.ToString("MMMM dd, yyyy"));
}
}
myDateTime.ToString() = 12/01/2004 10:02:10 PM
myDateTime.ToString("MMMM dd, yyyy") = January 12, 2004