using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime d = new DateTime(2012, 2, 29);
//幾種顯示民國年的方法
//1.另外取年減1911
Console.WriteLine(
string.Format("民國{0}年{1:MM月dd日}",
d.Year - 1911, d)
);
//2.借用TaiwanCalender
//(沒想到.NET Library中有這個吧!! 微軟就甘心A)
System.Globalization.TaiwanCalendar tc
= new System.Globalization.TaiwanCalendar();
Console.WriteLine(
"{0}/{1:MM/dd}", tc.GetYear(d), d);
//同場加映,另外有TaiwanLunisolarCalendar可以查農曆哦!
//可惜目前還不支援看吉凶沖煞,無法得知否宜嫁娶動土上樑(大誤)
System.Globalization.TaiwanLunisolarCalendar tlc =
new System.Globalization.TaiwanLunisolarCalendar();
Console.WriteLine("農曆: {0}年{1}月{2}日",
tlc.GetYear(d), tlc.GetMonth(d), tlc.GetDayOfMonth(d));
//3.借用demo寫的擴充方法http://demo.tc/Post/579
Console.WriteLine(d.ToTaiwanCalendar("yyyy/MM/dd"));
Console.Read();
}
}
static class DateExt
{
/// <summary>
/// 轉換為民國年
/// </summary>
///<param name="format">標準格式化語法</param>
/// <returns></returns>
static public string ToTaiwanCalendar(this DateTime x, string format)
{
DateTime now = x;
TaiwanCalendar tc = new TaiwanCalendar();
Regex regex = new System.Text.RegularExpressions.Regex(@"[yY]+");
format = regex.Replace(format, tc.GetYear(x).ToString("000"));
return x.ToString(format);
}
}
}