[Conversion] C # method for converting string format datetime string to datetime type

Source: Internet
Author: User

Method 1: Convert. todatetime (string)

The string format must be yyyy-mm-dd hh: mm: Ss.

========================================================== ==========

Method 2: Convert. todatetime (string, iformatprovider)

Datetime DT;

Datetimeformatinfo dtformat = new system. globalizationdatetimeformatinfo ();

Dtformat. Export datepattern = "yyyy/mm/DD ";

Dt = convert. todatetime ("2011/05/26", dtformat );

========================================================== ==========

Method 2: datetime. parseexact ()

String datestring = "20110526 ";

Datetime dt = datetime. parseexact (datestring, "yyyymmdd", system. Globalization. cultureinfo. currentculture );

Or

Datetime dt = datetime. parseexact (datestring, "yyyymmdd", system. Globalization. cultureinfo. invariantculture );

 

References:

Cultureinfo = cultureinfo. createspecificculture ("En-us"); string format = "DDD Mmm D hh: mm: SS zz00 YYYY"; string stringvalue = datetime. now. tostring (format, cultureinfo); // obtain the date string datetime = datetime. parseexact ("Wed Aug 25 16:28:03 + 0800 2010", format, cultureinfo); // converts a string to a date
 
Others

Date Format: yyyymmdd hh: mm: SS (note that the uppercase and lowercase letters of this string are strict)
Yyyy: indicates the year.
MM: Month
DD: represents the day
Hh: represents the hour (in 24-hour format)
MM: minutes
SS: seconds
Datetime. Now. tow.timestring ()
Datetime dt = datetime. now;
DT. tostring (); // 13:21:25
DT. tofiletime (). tostring (); // 127756416859912816
DT. tofiletimeutc (). tostring (); // 127756704859912816
DT. tolocaltime (). tostring (); // 21:21:25
DT. tolongdatestring (). tostring (); // November 5, 2005
DT. tolongtimestring (). tostring (); // 13:21:25
DT. tooadate (). tostring (); // 38661.5565508218
DT. toshortdatestring (). tostring (); // 2005-11-5
DT. toshorttimestring (). tostring (); // 13:21
DT. touniversaltime (). tostring (); // 5:21:25
DT. year. tostring (); // 2005
DT. Date. tostring (); // 0:00:00
DT. dayofweek. tostring (); // Saturday
DT. dayofyear. tostring (); // 309
DT. Hour. tostring (); // 13
DT. millisecond. tostring (); // 441
DT. Minute. tostring (); // 30
DT. Month. tostring (); // 11
DT. Second. tostring (); // 28
DT. ticks. tostring (); // 632667942284412864
DT. timeofday. tostring (); // 13:30:28. 4412864
DT. tostring (); // 13:47:04
DT. addyears (1). tostring (); // 13:47:04
DT. adddays (1.1). tostring (); // 16:11:04
DT. addhours (1.1). tostring (); // 14:53:04
DT. addmilliseconds (1.1). tostring (); // 13:47:04
DT. addmonths (1). tostring (); // 13:47:04
DT. addseconds (1.1). tostring (); // 13:47:05
DT. addminutes (1.1). tostring (); // 13:48:10
DT. addticks (1000). tostring (); // 13:47:04
DT. compareto (DT). tostring (); // 0
DT. Add (?). Tostring (); // question mark is a time period
DT. Equals ("16:11:04"). tostring (); // false
DT. Equals (DT). tostring (); // true
DT. gethashcode (). tostring (); // 1474088234
DT. GetType (). tostring (); // system. datetime
DT. gettypecode (). tostring (); // datetime

DT. getdatetimeformats ('s ') [0]. tostring (); // 2005-11-05t14: 06: 25
DT. getdatetimeformats ('T') [0]. tostring (); //
DT. getdatetimeformats ('y') [0]. tostring (); // January 1, November 2005
DT. getdatetimeformats ('D') [0]. tostring (); // January 1, November 5, 2005
DT. getdatetimeformats ('D') [1]. tostring (); // 2005 11 05
DT. getdatetimeformats ('D') [2]. tostring (); // Saturday 2005 11 05
DT. getdatetimeformats ('D') [3]. tostring (); // Saturday, January 1, November 5, 2005
DT. getdatetimeformats ('M') [0]. tostring (); // January 1, November 5
DT. getdatetimeformats ('F') [0]. tostring (); // November 5, 2005
DT. getdatetimeformats ('G') [0]. tostring (); //
DT. getdatetimeformats ('R') [0]. tostring (); // sat, 05 Nov 2005 14:06:25 GMT
String. Format ("{0: d}", DT); // 2005-11-5
String. Format ("{0}", DT); // January 1, November 5, 2005
String. Format ("{0: f}", DT); // November 5, 2005
String. Format ("{0: f}", DT); // November 5, 2005 14:23:23
String. Format ("{0: g}", DT ); //
String. Format ("{0: g}", DT); // 2005-11-5 14:23:23
String. Format ("{0: m}", DT); // January 1, November 5
String. Format ("{0: R}", DT); // sat, 05 Nov 2005 14:23:23 GMT
String. Format ("{0: s}", DT); // 2005-11-05t14: 23: 23
String. Format ("{0: t}", DT); // 14: 23
String. Format ("{0: t}", DT); // 14:23:23
String. Format ("{0: u}", DT); // 2005-11-05 14: 23: 23z
String. Format ("{0: u}", DT); // November 5, 2005 6:23:23
String. Format ("{0: y}", DT); // January 1, November 2005
String. Format ("{0}", DT); // 2005-11-5 14:23:23
String. Format ("{0: yyyymmddhhmmssffff}", DT );
Calculates the number of days between two dates.
-----------------------------------------------
Datetime dt1 = convert. datetime ("2007-8-1 ");
Datetime dt2 = convert. datetime ("2007-8-15 ");
Timespan span = dt2.subtract (dt1 );
Int daydiff = span. Days + 1;
Calculates the number of days in a month.
-----------------------------------------------
Int days = datetime. daysinmonth (2007, 8 );
Days = 31;
Increase or decrease the number of days by one day
-----------------------------------------------
Datetime dt = datetime. now;
DT. adddays (1); // Add one day
DT. adddays (-1); // Reduce by one day
The method for other years is similar...
Convert date functions in Oracle SQL
-----------------------------------------------
To_date ("2007-6-6", 'yyyy-MM-DD ");
To_date ("2007/6/6", 'yyyy/MM/DD ");

Related Article

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.