date
This commit is contained in:
2025-09-11 00:47:51 +08:00
parent 05a7cc3e9b
commit c90d181b5e
7 changed files with 72 additions and 44 deletions

View File

@@ -10,6 +10,8 @@ using System.Reflection.Emit;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Http;
using tyme.solar;
namespace Model
{
@@ -71,12 +73,12 @@ namespace Model
= 1, = 2, = 3, = 4, = 5, = 6,
= 7, = 8, = 9, = 10, = 11, = 12
}
enum heavenlyStems
public enum heavenlyStems
{
= 1, , , , , , , , ,
}
enum earthlyBranches
public enum earthlyBranches
{
= 1, , , , , , , , , , ,
}
@@ -85,28 +87,24 @@ namespace Model
public static string chagenSign(DateTime? date)
{ //可改公用
//生肖
if (date == null) return null;
DateTime d = date.Value;
// 使用 tyme4net 方法
try
{
if (date != null)
{
DateTime d = date ?? DateTime.Now;
int year = d.Year;
int birthpet; //宣告生肖要用的變數
birthpet = year % 12; //西元年除12取餘數
birthpet -= 3;
//餘數-3
if (birthpet <= 0) birthpet += 12;
//判斷餘數是否大於0小於0必須+12
return Enum.GetName(typeof(chinese), birthpet);
//return ((chinese)birthpet).ToString(); //也可以
}
{
var solarDay = SolarDay.FromYmd(d.Year, d.Month, d.Day);
var lunarDay = solarDay.GetLunarDay();
// 使用正確的 tyme4net API
var lunarYear = tyme.lunar.LunarYear.FromYear(lunarDay.Year);
return lunarYear.SixtyCycle.EarthBranch.GetZodiac().GetName();
}
catch
catch (Exception ex)
{
//return null;
return $"生肖計算錯誤: {ex.Message}";
}
return null;
}
public static string sexagenary(DateTime? date)
{

View File

@@ -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