//C# 获取农历日期

///<summary>
/// 实例化一个 ChineseLunisolarCalendar
///</summary>
private static ChineseLunisolarCalendar ChineseCalendar =new ChineseLunisolarCalendar(); ///<summary>
/// 十天干
///</summary>
private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; ///<summary>
/// 十二地支
///</summary>
private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; ///<summary>
/// 十二生肖
///</summary>
private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; ///<summary>
/// 返回农历天干地支年
///</summary>
///<param name="year">农历年</param>
///<return s></return s>
public static string GetLunisolarYear(int year)
{
if (year >)
{
int tgIndex = (year -) %;
int dzIndex = (year -) %; return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
} throw new ArgumentOutOfRangeException("无效的年份!");
} ///<summary>
/// 农历月
///</summary> ///<return s></return s>
private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" }; ///<summary>
/// 农历日
///</summary>
private static string[] days1 = { "初", "十", "廿", "三" };
///<summary>
/// 农历日
///</summary>
private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" }; ///<summary>
/// 返回农历月
///</summary>
///<param name="month">月份</param>
///<return s></return s>
public static string GetLunisolarMonth(int month)
{
if (month < && month >)
{
return months[month -];
} throw new ArgumentOutOfRangeException("无效的月份!");
} ///<summary>
/// 返回农历日
///</summary>
///<param name="day">天</param>
///<return s></return s>
public static string GetLunisolarDay(int day)
{
if (day > && day <)
{
if (day != && day !=)
{
return string.Concat(days1[(day -) /], days[(day -) %]);
}
else
{
return string.Concat(days[(day -) /], days1[]);
}
} throw new ArgumentOutOfRangeException("无效的日!");
} ///<summary>
/// 根据公历获取农历日期
///</summary>
///<param name="datetime">公历日期</param>
///<return s></return s>
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 >)
{
if (leapMonth == month)
{
//闰月
isleap =true;
month--;
}
else if (month > leapMonth)
{
month--;
}
} return string.Concat(GetLunisolarYear(year), "年", isleap ?"闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
}

使用方法:把这段代码写进去后,在要使用的地方引用:

 string lunar = GetChineseDateTime(DateTime.Now);

C#获取农历的日期(转)的更多相关文章

  1. js 根据年月获取当月有多少天_js获取农历日期_及Js其它常用有用函数

    //根据年月获取当月有多少天 function getDaysInMonth(year, month) { debugger; //parseInt(number,type)这个函数后面如果不跟第2个 ...

  2. C# 获取农历日期

    //C# 获取农历日期 ///<summary> /// 实例化一个 ChineseLunisolarCalendar ///</summary> private static ...

  3. iOS获取时间、日期

    //获取当前时间 NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease]; [formatter setLoc ...

  4. php获取农历、节日、节气

    /* * 农历 节气 节日 * edit: www.jbxue.com */ header("Content-Type:text/html;charset=utf-8"); cla ...

  5. Oracle 获取当前日期及日期格式

    http://blog.sina.com.cn/s/blog_6168ee920100l2ye.html Oracle 获取当前日期及日期格式 获取系统日期:  SYSDATE()   格式化日期:  ...

  6. PHP如何获取二个日期的相差天数?

    我们经常需要获取二个日期之间相差的天数,方便客户知道距离某个时间段是相差了多少天数,这样的显示结果现在是越来越流行的了.不再像以前那样呆板的显示日期的了.我们这里就分享了二种方法可以获取到二个日期之间 ...

  7. Java 获取两个日期之间的日期

    1.前期需求,两个日期,我们叫他startDate和endDate,然后获取到两个日期之间的日期 /** * 获取两个日期之间的日期 * @param start 开始日期 * @param end ...

  8. 【转】js写显示农历的日期

    网上查找了个,记录下. <body> <!-- 中国农历开始 --> <SCRIPT language=JavaScript> <!-- var lunarI ...

  9. sql语句中获取datetime的日期部分或时间部分

    sql语句中获取datetime的日期部分 sql语句中 经常操作操作datetime类型数据.今天在写一个存储过程的时候需要将 一个datetime的值的 日期部分提取出来.网上有许多这方面的介绍. ...

随机推荐

  1. stdin stdout stderr 标准I/O流

    Unix中一切皆文件,磁盘等设备在操作系统来看都是文件. 对文件进行操作时,需要打开这个文件,并获得文件描述符(file descriptor, fd) 而每个进程生来就有三个文件描述符绑定在它身上, ...

  2. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_发布者策略控制

    在 读经典——<CLR via C#>(Jeffrey Richter著) 笔记_高级管理控制(配置)中,是由程序集的发布者将程序集的一个新版本发送给管理员,后者安装程序集,并手动编辑应用 ...

  3. c# mvc会话过期设置

    configuration 元素(常规设置架构)   system.web 元素(ASP.NET 设置架构)     sessionState 元素(ASP.NET 设置架构) <session ...

  4. lnmp 架构

    Mysql安装 tar zxf mysql-boost-5.7.17.tar.gz yum install -y gcc gcc-c++ yum install cmake-2.8.12.2-4.el ...

  5. EXCEL 导入SQL SERVER 方法

    1.注意:确认是否已安装 AccessDatabaseEngine.exe 2.可视化按提示操作.

  6. Linux UDEV提权过程

    1.下载攻击脚本 [test@H0f ~]$ wget http://www.extmail .org/source/exploit-udev-8478 --2018-04-02 01:21:00-- ...

  7. 爬虫(GET)——add_header()和get_header()

    import urllib.request import random url = "http://www.baidu.com/" # 可以是User-Agent列表,也可以是代理 ...

  8. sqlt 之 分析 DB upgrade 导致SQL 性能下降 的方法 xplore

    https://blog.csdn.net/lukeUnique/article/details/79331779 https://mauro-pagano.com/2014/10/27/when-t ...

  9. Mybatis学习笔记5 - 参数处理

    1.单个参数:mybatis不会做特殊处理,#{参数名}:取出参数值. 2.多个参数:mybatis会做特殊处理. 多个参数会被封装成 一个map, key:param1...paramN,或者参数的 ...

  10. Django重新整理3

    Forms组件 1.在models.py中我们建立一个新的表关系: class UserInfo(models.Model): user=models.CharField(max_length=32) ...