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" >YYYYMMDDHHMMSS</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");; }}