C # Conversion between date and string __c#

Source: Internet
Author: User
Tags iso 8601 local time

1. Date-turn string (reproduced)

In programming, it is often used to convert a date variable to a string, and a string that you want to convert to a different format at different times.
Here are some common conversion and conversion results: (View format description)

Take the date as an example: 2009-09-06 10:56:13.383

private void Form1_Load (object sender, System.EventArgs e)
{
Textbox1.text=datetime.now.tostring ("D"); 2009-09-06
Textbox2.text=datetime.now.tostring ("D"); September 06, 2009
Textbox3.text=datetime.now.tostring ("G"); 2009-09-06 10:56:13
Textbox6.text=datetime.now.tostring ("G"); 2009-09-06 10:56
Textbox5.text=datetime.now.tostring ("T"); 10:56:13
Textbox4.text=datetime.now.tostring ("T"); 10:56

Textbox12.text=datetime.now.tostring ("F"); September 06, 2009 10:56:13
Textbox11.text=datetime.now.tostring ("F"); September 06, 2009 10:56
Textbox10.text=datetime.now.tostring ("M"); September 06
Textbox9.text=datetime.now.tostring ("R"); Sun, SEP 2009 10:56
Textbox8.text=datetime.now.tostring ("s"); 2009-09-06t10:56:13
Textbox7.text=datetime.now.tostring ("U"); 2009-09-06 10:56:13z

Textbox13.text=datetime.now.tostring ("Y"); September 2009
Textbox14.text=datetime.now.tostring ("Y"); September 2009
}

Effect comparison:

The following is the result of another method conversion:
      
private void Form2_load (object sender, System.EventArgs e)
{
   textbox1.text=datetime.now.tostring ("m/d/yyyy");              //09-06-2009
   textbox2.text= DateTime.Now.ToString ("Mm/dd/yy");            // 09-06-09
   textbox3.text=datetime.now.tostring ("D-mmmm-yy");          //06-3 month -09
   textbox6.text=datetime.now.tostring ("ddmmyyyy");            //060909
   textbox5.text=datetime.now.tostring (" DD Day mm month yyyy year ");  //06 Day September 2009
   textbox4.text=datetime.now.tostring (" YYYY year mm month DD Day ");   //September 06, 2009

Textbox12.text=datetime.now.tostring ("yyyy year"); 2009
Textbox11.text=datetime.now.tostring ("mm month"); September
Textbox10.text=datetime.now.tostring ("DD Day"); 06th
Textbox9.text=datetime.now.tostring ("Hh:mm:ss"); 10:56:13
Textbox8.text=datetime.now.tostring ("m/d/yyyy h:mm"); 09/06/2009 10:56
Textbox7.text=datetime.now.tostring ("H:mm:ss"); 10:56:13
}

2. String Turn date (original)

A. Method One:convert.todatetime (String)

Convert.todatetime ("10-03-20"); 2010-3-20 0:00:00

B. Method Two:datetime.parse (String)

DateTime.Parse ("10-03-20"); 2010-3-20 0:00:00

C. Method III:datetime.parseexact (string,string date format, null) This function can customize the date conversion format

DateTime.ParseExact ("March 20, 2010 15:14 56 Seconds", "yyyy mm month DD Day hh point mm minute ss seconds", NULL); 2010-3-20 15:14:56

DateTime.ParseExact ("2010-03-20" "", "YYYY-MM-DD", null); 2010-3-20 0:00:00

3. Appendix Format type Description

Format Character Association attribute/description
D ShortDatePattern
D Longdatepattern
F Full Date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
G General (short date and short time)
G General (short date and long time)
M, M Monthdaypattern
R, R Rfc1123pattern
s use local time Sortabledatetimepattern (based on ISO 8601)
T Shorttimepattern
T Longtimepattern
U universalsortabledatetimepattern format for displaying Universal Time
U use the full date and time of the universal Time (long date and long time)
Y, y Yearmonthpattern

Hope, useful to everyone.

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.