2010年7月28日 星期三

C# extend 方法

透過extension方式,可以擴充System.DateTime的方法,
coding時, 直接輸入DateTime類型物件,intelligence就會自動出現這個自訂義的方法囉..

//將西元日期轉換為帶format的國曆日期
public static class Datetime
{
     public static string ConvertToCDate( this DateTime dt)
     {
         return string.Format("{1}{0}{2:D2}{0}{3:D2}", new object[] { @"/" (dt.Year - 1911), dt.Month, dt.Day });}
     }
}

使用方法:
return DateTime.Now.ConvertToCDate();

回傳99/07/28

沒有留言:

張貼留言

VS CODE離線安裝套件方式-續

去年 vscode離線安裝 powershell nupkg 今年在一台無外網的 windwos 2025上安裝時遇到了powershell版本一直讀取到了5.1的版本,鬼打牆了一下午,在VM還原重測測了好幾回,應該是可行了。 將下載到的免安裝powershell 解壓縮到...