Wednesday, September 19, 2012

C# DateTime Format

Many DateTime formats are available. C# programs use the ToString method on the DateTime type. ToString receives many useful formats. These formats have confusing syntax forms. Correct formatting of dates and times is essential to many programs.

Format string

To start, we see an example of how you can use a specific formatting string with DateTime and ToString to obtain a special DateTime string. This is useful when interacting with other systems, or when you require a precise format.

Program that uses DateTime format [C#]  using System;  class Program
{static void Main()
{ DateTime time = DateTime.Now;// Use current time
string format = "MMM ddd d HH:mm yyyy"; // Use this format
Console.WriteLine(time.ToString(format)); // Write to console } }
Output Feb Fri 27 11:41 2009 Format string pattern MMM
three-letter month ddd display three-letter day of the WEEK d
display day of the MONTH HH display two-digit hours on 24-hour scale mm
display two-digit minutes yyyy display four-digit year

Note: The letters in the format string above specify the output you want to display. The final comment shows what the MMM, ddd, d, HH, mm, and yyyy will do.

Modify format


Continuing on, we see how you can modify the DateTime format string in the above example to get different output with ToString. We change some of the fields so the resulting value is shorter.

Program that uses different format [C#]
using System;
class Program {static void Main()
{ DateTime time = DateTime.Now; // Use current time string format = "M d h:mm yy";
// Use this format Console.WriteLine(time.ToString(format)); // Write to console } }
Output 2 27 11:48 09 Format string pattern M display one-digit month number
[changed] d display one-digit day of the MONTH [changed] h
display one-digit hour on 12-hour scale [changed] mm
display two-digit minutes yy display two-digit year
[changed]

Format string usages. You will also need to specify a format string when using DateTime.ParseExact and DateTime.ParseExact. This is because those methods require a custom pattern to parse.



Next you can use a single character with ToString or DateTime.ParseExact to specify a preset format available in the framework. These are standard formats and useful in many programs. They can eliminate typos in the custom format strings.

Program that tests formats [C#]
using System;
class Program {
static void Main()
{ DateTime now = DateTime.Now;
Console.WriteLine(now.ToString("d"));
Console.WriteLine(now.ToString("D"));
Console.WriteLine(now.ToString("f"));
Console.WriteLine(now.ToString("F"));
Console.WriteLine(now.ToString("g"));
Console.WriteLine(now.ToString("G"));
Console.WriteLine(now.ToString("m"));
Console.WriteLine(now.ToString("M"));
Console.WriteLine(now.ToString("o"));
Console.WriteLine(now.ToString("O"));
Console.WriteLine(now.ToString("s"));
Console.WriteLine(now.ToString("t"));
Console.WriteLine(now.ToString("T"));
Console.WriteLine(now.ToString("u"));
Console.WriteLine(now.ToString("U"));
Console.WriteLine(now.ToString("y"));
Console.WriteLine(now.ToString("Y"));} }
Output
d 2/27/2009 D
Friday, February 27, 2009 f
Friday, February 27, 2009 12:11 PM F
Friday, February 27, 2009 12:12:22 PM g
2/27/2009 12:12 PM G 2/27/2009 12:12:22 PM m
February 27 M February 27 o 2009-02-27T12:12:22.1020000-08:00 O
2009-02-27T12:12:22.1020000-08:00 s 2009-02-27T12:12:22 t
12:12 PM T 12:12:22 PM u 2009-02-27 12:12:22Z U
Friday, February 27, 2009 8:12:22 PM y February, 2009 Y February, 2009

Date strings

Here we see the ToLongDateString, ToLongTimeString, ToShortDateString, and ToShortTimeString methods on DateTime. These methods are equivalent to the lowercase and uppercase D and T methods shown in the example above.

Program that uses ToString methods [C#]
using System; class Progra{ static void Main()
{ DateTime now = DateTime.Now; Console.WriteLine(now.ToLongDateString());
// Equivalent to D Console.WriteLine(now.ToLongTimeString());
// Equivalent to T Console.WriteLine(now.ToShortDateString());
// Equivalent to d Console.WriteLine(now.ToShortTimeString());
// Equivalent to t
Console.WriteLine(now.ToString()); } }
Output ToLongDateString
Friday, February 27, 2009 ToLongTimeString
12:16:59 PM ToShortDateString 2/27/2009 ToShortTimeString
12:16 PM ToString 2/27/2009 12:16:59 PM

SOURCE-http://www.dotnetperls.com/datetime-format

1 comment: