Implementation of common method classes for C # date format conversions

Source: Internet
Author: User
This article mainly introduces C # to implement date format conversion of the public method class, combined with a complete example of C # for a variety of common date format conversion methods, involving c#http://www.php.cn/wiki/57.html "target=" _blank "> String, date, time-related operation skills, the need for friends can refer to the following

The examples in this article describe the common method classes that C # implements for date format conversions. Share to everyone for your reference, as follows:

This demonstrates the conversion of some date formats in C #.

Create a public method class (UtilityHandle.cs) with the following code:

<summary>///public method class///</summary>public Static class utilityhandle{//<summary>//String date turn Datetim    E///</summary> public static datetime transstrtodatetime (String strdatetime) {datetime now; string[] format = new string[] {"Yyyymmddhhmmss", "Yyyy-mm-dd HH:mm:ss", "yyyy year mm month DD Day HH" mm min ss sec "," YYYYMDHHM MSS "," yyyy year M D Day h mm minute ss seconds "," yyyy. M.D h:mm:ss "," yyyy.    Mm.dd HH:mm:ss "," Yyyy-mm-dd "," YyyyMMdd "," Yyyy/mm/dd "," yyyy/m/d "}; if (datetime.tryparseexact (strdatetime, format, CultureInfo.InvariantCulture, Datetimestyles.none, out now)) {RET    Urn now;  } return datetime.minvalue; }///<summary>///Date conversion///</summary> public static string Transformdatalong (DateTime datetime) {s    Tring result = "";    if (datetime.hasvalue) {result = DateTime.Value.ToString ("Yyyy-mm-dd HH:mm:ss");  } return result; }///<summary>//Date conversion///</summary> public static string TransformdataShort (datetime datetime) {string result = "";    if (datetime.hasvalue) {result = DateTime.Value.ToString ("Yyyy-mm-dd");  } return result; }///<summary>//Convert the date to decimal///</summary> public static decimal transdatetimetodecimal (DateTime dat    e) {decimal ret = 0; ret = Convert.todecimal (date.    ToString ("Yyyymmddhhmmss"));  return ret; }///<summary>//Convert decimal to date format///</summary>//<param name= "date" &GT;YYYYMMDDHHMMSS&LT;/PARAM&G  T <returns>yyyy-mm-dd hh:mm:ss</returns> public static string Transdecimaltodatetime (string date) {Dat Etimeformatinfo Dtfi = new CultureInfo ("ZH-CN", false).    DateTimeFormat;    datetime datetime = DateTime.Now;    Datetime.tryparseexact (date, "Yyyymmddhhmmss", Dtfi, Datetimestyles.none, out dateTime);  Return datetime.tostring ("Yyyy-mm-dd HH:mm:ss");; }}
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.