chinese
date
This commit is contained in:
@@ -8,6 +8,9 @@ using System.Drawing.Imaging;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using tyme.solar;
|
||||
using tyme.lunar;
|
||||
using tyme.sixtycycle;
|
||||
|
||||
/// <summary>
|
||||
/// publicFun 的摘要描述
|
||||
@@ -86,27 +89,35 @@ public class publicFun
|
||||
public static string chagenDate(DateTime? date)//因為欄位值是nullable, 所以把null的處理拉進函數做
|
||||
{ //可改公用
|
||||
//農曆年
|
||||
ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
|
||||
if (date == null) return null;
|
||||
|
||||
DateTime d = date.Value;
|
||||
|
||||
// 使用 tyme4net 方法
|
||||
try
|
||||
{
|
||||
if (date != null)
|
||||
var solarDay = SolarDay.FromYmd(d.Year, d.Month, d.Day);
|
||||
var lunarDay = solarDay.GetLunarDay();
|
||||
|
||||
int minYear = lunarDay.Year - 1911;
|
||||
return $"民{minYear}年/{lunarDay.ToString().Substring(2)}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果 tyme4net 完全失敗,回退到原本的方法
|
||||
try
|
||||
{
|
||||
DateTime d = date ?? DateTime.Now;
|
||||
ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
|
||||
int yy = chineseDate.GetYear(d) - 1911;
|
||||
int mm = chineseDate.GetMonth(d);
|
||||
int dd = chineseDate.GetDayOfMonth(d);
|
||||
string dstr = $"民{yy}年{mm:00}月{dd:00}日";
|
||||
return dstr;
|
||||
return $"民{yy}年{mm:00}月{dd:00}日";
|
||||
}
|
||||
catch
|
||||
{
|
||||
return $"農曆轉換錯誤: {ex.Message}";
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ //try catch可能會很慢, 已知條件先判斷
|
||||
//return null;
|
||||
}
|
||||
return null;
|
||||
//民國年
|
||||
//System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar();
|
||||
// tc.GetYear(d), tc.GetMonth(d), tc.GetDayOfMonth(d)
|
||||
}
|
||||
|
||||
#region 刪除檔案 for api
|
||||
|
||||
Reference in New Issue
Block a user