C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類實(shí)例
本文實(shí)例講述了C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類。分享給大家供大家參考,具體如下:
這里演示了C#中一些日期格式的轉(zhuǎn)換。
創(chuàng)建公共方法類(UtilityHandle.cs),代碼如下:
/// <summary> /// 公共方法類 /// </summary> public static class UtilityHandle { /// <summary> /// 字符串日期轉(zhuǎn)DateTime /// </summary> public static DateTime TransStrToDateTime(string strDateTime) { DateTime now; string[] format = new string[] { "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyy年MM月dd日 HH時mm分ss秒", "yyyyMdHHmmss","yyyy年M月d日 H時mm分ss秒", "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)) { return now; } return DateTime.MinValue; } /// <summary> /// 日期轉(zhuǎn)換 /// </summary> public static string TransformDataLong(DateTime? dateTime) { string result = ""; if (dateTime.HasValue) { result = dateTime.Value.ToString("yyyy-MM-dd HH:mm:ss"); } return result; } /// <summary> /// 日期轉(zhuǎn)換 /// </summary> public static string TransformDataShort(DateTime? dateTime) { string result = ""; if (dateTime.HasValue) { result = dateTime.Value.ToString("yyyy-MM-dd"); } return result; } /// <summary> /// 將日期轉(zhuǎn)換成decimal /// </summary> public static decimal TransDateTimeToDecimal(DateTime date) { decimal ret = 0; ret = Convert.ToDecimal(date.ToString("yyyyMMddHHmmss")); return ret; } /// <summary> /// 將decimal轉(zhuǎn)換成日期格式 /// </summary> /// <param name="date">yyyyMMddHHmmss</param> /// <returns>yyyy-MM-dd HH:mm:ss</returns> public static string TransDecimalToDateTime(string date) { DateTimeFormatInfo 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"); ; } }
PS:這里再為大家推薦幾款日期與時間相關(guān)工具供大家參考使用:
在日期天數(shù)差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
在線日期計算器/相差天數(shù)計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線陰歷/陽歷轉(zhuǎn)換工具:
http://tools.jb51.net/bianmin/yinli2yangli
在線天數(shù)計算器(flash版):
http://tools.jb51.net/jisuanqi/datejsq
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#字符串操作技巧總結(jié)》、《C#數(shù)組操作技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)SSE(Server-Sent Events)服務(wù)端和客戶端的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)SSE(Server-Sent Events)服務(wù)端和客戶端的相關(guān)知識,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-03-03C#實(shí)現(xiàn)將一個矩陣分解為對稱矩陣與反稱矩陣之和的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將一個矩陣分解為對稱矩陣與反稱矩陣之和的方法,較為詳細(xì)的分析了矩陣分解運(yùn)算的原理與C#實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-08-08C# 中const,readonly,static的使用小結(jié)
這篇文章主要介紹了C# 中使用const,readonly,static的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01C#實(shí)現(xiàn)獲取程序路徑方法小結(jié)
這篇文章主要介紹了C#實(shí)現(xiàn)獲取程序路徑方法,實(shí)例分析了C#獲取文件路徑的各種常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C#實(shí)現(xiàn)rabbitmq 延遲隊列功能實(shí)例代碼
本篇文章主要介紹了C#實(shí)現(xiàn)rabbitmq 延遲隊列功能實(shí)例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04WPF開發(fā)txt閱讀器實(shí)現(xiàn)目錄提取功能
這篇文章主要為大家詳細(xì)介紹了WPF開發(fā)txt閱讀器時如何實(shí)現(xiàn)目錄提取功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06