C # datetime formatted output and instance usage

Source: Internet
Author: User

C # datetime formatted output and instance usage

Create Date Time 2008-03-09 16:05:07.123
DateTime dt = new DateTime (2008, 3, 9, 16, 5, 7, 123);

String.Format ("{0:y yy yyy yyyy}", DT); "8 008 2008" Year
String.Format ("{0:m MM MMM MMMM}", DT); "3 Mar March" month
String.Format ("{0:d dd ddd dddd}", DT); "9 Sun Sunday" Day
String.Format ("{0:h hh H hh}", DT); "4" Hour 12/24
String.Format ("{0:m mm}", DT); "5" Minute
String.Format ("{0:s ss}", DT); "7 Modified" second
String.Format ("{0:f ff FFF FFFF}", DT); "1 123 1230" Sec.fraction
String.Format ("{0:f FF FFF FFFF}", DT); "1 123 123" without zeroes
String.Format ("{0:t tt}", DT); "P PM" a.m. or p.m.
String.Format ("{0:z zz zzz}", DT); " -6-06-06:00" time zone

Instance

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 Display 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

Another way of doing things

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]

Abroad in commonly used formats

Using System;

Class program
{
    static void Main ()
    {
 datetime now = Datetim E.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, 2009 12:11 PM
F Friday, February, 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, 2009 8:12:22 PM
Y February, 2009
Y February, 2009

Using System;

Class Program
{
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.