2015年11月18日 星期三

[C#][擴充方法] 取得西元日期的民國年月日

&nbsp; &nbsp; &nbsp; &nbsp; /// <summary> 將西元日期轉民國日期 回傳 字串陣列[0]民國年YYY [1]月MM [2]日DD</summary>
&nbsp; &nbsp; &nbsp; &nbsp; /// <param name="datetime">要轉換的日期</param>
&nbsp; &nbsp; &nbsp; &nbsp; /// <returns>字串陣列[0]民國年YYY [1]月MM [2]日DD</returns>
&nbsp; &nbsp; &nbsp; &nbsp; public static string[] ToSimpleTaiwanDate(this DateTime datetime)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new string[]{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taiwanCalendar.GetYear(datetime).ToString(),
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datetime.Month.ToString().PadLeft(2,'0'),
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datetime.Day.ToString().PadLeft(2,'0')};
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; /// <summary> 將西元日期轉民國日期 回傳 字串陣列[0]民國年YYY [1]月MM [2]日DD</summary>
&nbsp; &nbsp; &nbsp; &nbsp; /// <param name="datetime">要轉換的日期</param>
&nbsp; &nbsp; &nbsp; &nbsp; /// <returns>字串陣列[0]民國年YYY [1]月MM [2]日DD</returns>
&nbsp; &nbsp; &nbsp; &nbsp; public static string ToSimpleTaiwanDate_YYYMMDD(this DateTime datetime)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taiwanCalendar.GetYear(datetime).ToString()+
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datetime.Month.ToString().PadLeft(2,'0')+
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datetime.Day.ToString().PadLeft(2,'0');
&nbsp; &nbsp; &nbsp; &nbsp; }

沒有留言:

張貼留言