"C #" Analysis of date processing in C #

Source: Internet
Author: User

1. String conversion to date 1.1 the first way

Using the convert.todatetime method, this method has many overloaded methods, here I introduce two common overloaded methods.

The first type:

Use:

Convert.todatetime (string)

there is a limit to the conversion of this method, that is, the format of the string should resemble "Yyyy-mm-dd Hh:mm:ss", for example: "2017-12-12 00:01:11". Why did you say that it should be similar to the "YYYY-MM-DD hh:mm:ss" format, because it can also be recognized like this format "2017/12/12 00:01:11". The method can also convert null values, and NULL will be converted to "1/1/0001 12:00:00", although the method supports a limited number of situations. Using this method, the flexibility is stronger,

The second type:

Convert.todatetime (string, IFormatProvider)

For example:

New"yyyy/mm/dd"= Convert.todatetime ("2011/05/26  ", Dtformat);
1.2 Second Way

Using datetime.parseexact to use this method, you can also achieve the purpose of converting the string to a date, the method also has overloaded methods, here I introduce a:

ParseExact (String, String, IFormatProvider)

The next step is to use the

string " 20110526 "  "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

Or:

" YYYYMMDD ", System.Globalization.CultureInfo.InvariantCulture);
2. Date conversion to string 2.1 The first way

If the date needs to be converted to a string, then use the ToString () method directly.

Like what:

DateTime dt == dt. ToString ("yyyy mm month dd hh when mm min ss seconds ");

Some date format identifiers are given below

YYYY: Year of representation
MM: On behalf of the month
DD: Representative Day
HH: Representative hour (24-hour system)
HH: Representative Hour (12-hour system)
MM: Represents minutes
SS: Represents seconds

"C #" Analysis of date processing in C #

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.