using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Common
{
public class LunarCalendarHelper
{
//C# 获取农历日期
///
/// 实例化一个 ChineseLunisolarCalendar
///
private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
///
/// 十天干
///
private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
///
/// 十二地支
///
private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
///
/// 十二生肖
///
private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
///
/// 返回农历天干地支年
///
///农历年
///
public static string GetLunisolarYear(int year)
{
if (year > 3)
{
int tgIndex = (year - 4) % 10;
int dzIndex = (year - 4) % 12;
return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
}
throw new ArgumentOutOfRangeException("无效的年份!");
}
///
/// 农历月
///
///
private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
///
/// 农历日
///
private static string[] days1 = { "初", "十", "廿", "三" };
///
/// 农历日
///
private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
private static int[] daysInt = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
///
/// 返回农历月
///
///月份
///
public static string GetLunisolarMonth(int month)
{
if (month < 13 && month > 0)
{
return months[month - 1];
}
throw new ArgumentOutOfRangeException("无效的月份!");
}
///
/// 返回农历日
///
///天
///
public static string GetLunisolarDay(int day)
{
if (day > 0 && day < 32)
{
if (day != 20 && day != 30)
{
return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
}
else
{
return string.Concat(days[(day - 1) / 10], days1[1]);
}
}
throw new ArgumentOutOfRangeException("无效的日!");
}
///
/// 根据公历获取农历日期
///
///公历日期
///
public static string GetChineseDateTime(DateTime datetime)
{
int year = ChineseCalendar.GetYear(datetime);
int month = ChineseCalendar.GetMonth(datetime);
int day = ChineseCalendar.GetDayOfMonth(datetime);
//获取闰月, 0 则表示没有闰月
int leapMonth = ChineseCalendar.GetLeapMonth(year);
bool isleap = false;
if (leapMonth > 0)
{
if (leapMonth == month)
{
//闰月
isleap = true;
month--;
}
else if (month > leapMonth)
{
month--;
}
}
return string.Concat(isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
}
public static bool IsHaveNewYear(DateTime datetime)
{
int year = ChineseCalendar.GetYear(datetime);
int month = ChineseCalendar.GetMonth(datetime);
int day = ChineseCalendar.GetDayOfMonth(datetime);
//获取闰月, 0 则表示没有闰月
int leapMonth = ChineseCalendar.GetLeapMonth(year);
bool isleap = false;
if (leapMonth > 0)
{
if (leapMonth == month)
{
//闰月
isleap = true;
month--;
}
else if (month > leapMonth)
{
month--;
}
}
if (GetLunisolarMonth(month) != "正")
{
return true;
}
else
{
if (new List { "初一", "初二", "初三", "初四", "初五", "初六", "初七" }.Contains(GetLunisolarDay(day)))
{
return false;
}
else
{
return true;
}
}
}
}
}