Number |
Display Format |
Format Rule Applied |
123 |
00123 |
00000 |
1.2 |
1.20 |
0.00 |
1.2 |
01.20 |
00.00 |
1.2 |
1.2 |
#.## |
123456 |
[12-34-56] |
[##-##-##] |
1234567890 |
(123) 456-7890 |
(###) ###-#### |
.56 |
0.6 |
0.0 |
1234567890 |
1,234,567,890 |
0,0 |
1234567890 |
1,234,567,890 |
#,# |
.086 |
8.6% |
#0.##% |
|
|
|
The single alphabetic character is used for Standard date and time formatting.
The character is case-sensitive. It means that the lower and upper case character will format differently.
The d for short date pattern, D for long date pattern.
The f for short time full date pattern, F for long time full date pattern.
The t for short time pattern, T for long time pattern.
using System;
using System.Globalization;
namespace ConsoleFormat1
{
class Program
{
static void
Main(string[] args)
{
//
Display using current (en-us) culture's short date format
DateTime thisDate = new
DateTime(2008, 3, 15);
Console.WriteLine(thisDate.ToString("d"));// Displays
3/15/2008
//
Display using pt-BR culture's short date format
CultureInfo culture = new
CultureInfo("pt-BR");
Console.WriteLine(thisDate.ToString("d", culture)); // Displays
15/3/2008
//
Display using dateformat information from hr-HR culture
DateTimeFormatInfo fmt = (new
CultureInfo("hr-HR")).DateTimeFormat;
Console.WriteLine(thisDate.ToString("d", fmt)); // Displays
15.3.2008
DateTime date1 = new
DateTime(2008, 4, 10);
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("en-US")));
//
Displays Thursday, April 10, 2008
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("pt-BR")));
//
Displays quinta-feira, 10 de abril de 2008
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("es-MX")));
//
Displays jueves, 10 de abril de 2008
Console.WriteLine(date1.ToString("f",
CultureInfo.CreateSpecificCulture("en-US")));
//
Displays Thursday, April 10, 2008 6:30 AM
Console.WriteLine(date1.ToString("f",
CultureInfo.CreateSpecificCulture("fr-FR")));
//
Displays jeudi 10 avril 2008 06:30
Console.WriteLine(date1.ToString("F",
CultureInfo.CreateSpecificCulture("en-US")));
//
Displays Thursday, April 10, 2008 6:30:00 AM
Console.WriteLine(date1.ToString("F",
CultureInfo.CreateSpecificCulture("fr-FR")));
//
Displays jeudi 10 avril 2008 06:30:00
Console.WriteLine(date1.ToString("g",
DateTimeFormatInfo.InvariantInfo));
//
Displays 04/10/2008 06:30
Console.WriteLine(date1.ToString("g",
CultureInfo.CreateSpecificCulture("en-us")));
//
Displays 4/10/2008 6:30 AM
Console.WriteLine(date1.ToString("g",
CultureInfo.CreateSpecificCulture("fr-BE")));
//
Displays 10/04/2008 6:30
Console.WriteLine(date1.ToString("U",
CultureInfo.CreateSpecificCulture("en-US")));
//
Displays Thursday, April 10, 2008 1:30:00 PM
Console.WriteLine(date1.ToString("U",
CultureInfo.CreateSpecificCulture("sv-FI")));
//
Displays den 10 april 2008 13:30:00
Console.WriteLine(date1.ToString("t",
CultureInfo.CreateSpecificCulture("en-us")));
//
Displays 6:30 AM
Console.WriteLine(date1.ToString("t",
CultureInfo.CreateSpecificCulture("es-ES")));
//
Displays 6:30
Console.WriteLine(date1.ToString("T",
CultureInfo.CreateSpecificCulture("en-us")));
//
Displays 6:30:00 AM
Console.WriteLine(date1.ToString("T",
CultureInfo.CreateSpecificCulture("es-ES")));
//
Displays 6:30:00
Console.WriteLine(date1.ToString("Y",
CultureInfo.CreateSpecificCulture("en-US")));
//
Displays April, 2008
Console.WriteLine(date1.ToString("y",
CultureInfo.CreateSpecificCulture("af-ZA")));
//
Displays April 2008
Console.ReadKey();
}
}
}
No comments:
Post a Comment