目前以經習慣直接用GridView自動產生,我要的資料,欄位名稱等什麼什麼的都先處理好在丟給GridView,其中時間是最囉嗦的,因為要用民國年,每次都要呼叫轉民國年的方法,久了覺得煩,試了很久給我找到一個決解的辦法。
說真的其實很簡單,只是要知道用法而以,在.Net微軟很貼心的提供各個國家的歷法,其中就有台灣曆,只要改變目前執行序的文化與曆法就可以了
現在 DateTime.Now.ToString() 輸出為 yy/MM/dd 上(下)午 hh:mm:ss 其中yy為減過1911,現階段就可以直接輸出成民國年,但我的顧客還是不滿意,他要yy年MM月dd日,當然我也想要一勞永益,又試了很久,也找出方法。
查了MSDN中DateTime.ToString()的說明,他是呼叫標準格式化 G(一般可排序日期/時間模式),但從MSDN找不到怎麼去設定G,最後我靠Reflector反組譯從DateTime.ToString()追溯回去,查到。
在設定ShortDatePattern時generalLongTimePattern=null,而generalLongTimePattern=null時使用this.ShortDatePattern + " " + this.LongTimePattern
所以使用設定ShortDatePattern就可以達到需求。
只要下列程式碼及可就直接輸出民國年。
CultureInfo taiwanCI = new CultureInfo("zh-TW");
taiwanCI.DateTimeFormat.Calendar = new TaiwanCalendar();
taiwanCI.DateTimeFormat.ShortDatePattern = "yy年MM月dd日";
有時我又只想輸出日期怎麼辦,如果一頁中只有一種規格我們可以
是不是簡單的過火,真是覺得以前的我怎麼那麼笨,用了一堆笨方法。
不過有一個缺點是年沒辦法輸出為3碼(如096),暫時找不出方法(不要設成"0yy年MM月dd日",100就變成0100永遠多一個零,可以跟顧戶講為了九百年後準備),還沒還沒有這個需求不急著找。
註:因為Web是每一個瀏覽就是個執行序,有A、B兩頁我只在A中改變文化,而同時瀏覽A、B頁只有A頁會改成民國年,而B頁沒有影響,如果不想每頁都要設可以寫在Globa中
說真的其實很簡單,只是要知道用法而以,在.Net微軟很貼心的提供各個國家的歷法,其中就有台灣曆,只要改變目前執行序的文化與曆法就可以了
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-TW");
Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar = new TaiwanCalendar();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar = new TaiwanCalendar();
現在 DateTime.Now.ToString() 輸出為 yy/MM/dd 上(下)午 hh:mm:ss 其中yy為減過1911,現階段就可以直接輸出成民國年,但我的顧客還是不滿意,他要yy年MM月dd日,當然我也想要一勞永益,又試了很久,也找出方法。
查了MSDN中DateTime.ToString()的說明,他是呼叫標準格式化 G(一般可排序日期/時間模式),但從MSDN找不到怎麼去設定G,最後我靠Reflector反組譯從DateTime.ToString()追溯回去,查到。
internal string GeneralLongTimePattern
{
get
{
if (this.generalLongTimePattern == null)
{
this.generalLongTimePattern = this.ShortDatePattern + " " + this.LongTimePattern;
}
return this.generalLongTimePattern;
}
}
而{
get
{
if (this.generalLongTimePattern == null)
{
this.generalLongTimePattern = this.ShortDatePattern + " " + this.LongTimePattern;
}
return this.generalLongTimePattern;
}
}
public string ShortDatePattern
{
get
{
if (this.shortDatePattern == null)
{
this.shortDatePattern = this.GetStringFromCalendarTable(0x1, 0xe);
}
return this.shortDatePattern;
}
set
{
this.VerifyWritable();
if (value == null)
{
throw new ArgumentNullException("ShortDatePattern", Environment.GetResourceString("ArgumentNull_String"));
}
this.shortDatePattern = value;
this.generalLongTimePattern = null;
this.generalShortTimePattern = null;
}
}
{
get
{
if (this.shortDatePattern == null)
{
this.shortDatePattern = this.GetStringFromCalendarTable(0x1, 0xe);
}
return this.shortDatePattern;
}
set
{
this.VerifyWritable();
if (value == null)
{
throw new ArgumentNullException("ShortDatePattern", Environment.GetResourceString("ArgumentNull_String"));
}
this.shortDatePattern = value;
this.generalLongTimePattern = null;
this.generalShortTimePattern = null;
}
}
在設定ShortDatePattern時generalLongTimePattern=null,而generalLongTimePattern=null時使用this.ShortDatePattern + " " + this.LongTimePattern
所以使用設定ShortDatePattern就可以達到需求。
只要下列程式碼及可就直接輸出民國年。
CultureInfo taiwanCI = new CultureInfo("zh-TW");
taiwanCI.DateTimeFormat.Calendar = new TaiwanCalendar();
taiwanCI.DateTimeFormat.ShortDatePattern = "yy年MM月dd日";
有時我又只想輸出日期怎麼辦,如果一頁中只有一種規格我們可以
taiwanCI.DateTimeFormat.LongTimePattern="";
是不是簡單的過火,真是覺得以前的我怎麼那麼笨,用了一堆笨方法。
不過有一個缺點是年沒辦法輸出為3碼(如096),暫時找不出方法(不要設成"0yy年MM月dd日",100就變成0100永遠多一個零,可以跟顧戶講為了九百年後準備),還沒還沒有這個需求不急著找。
註:因為Web是每一個瀏覽就是個執行序,有A、B兩頁我只在A中改變文化,而同時瀏覽A、B頁只有A頁會改成民國年,而B頁沒有影響,如果不想每頁都要設可以寫在Globa中