C#实现计算今天是农历几号的代码
2012-08-08 17:09
453 查看
代码说明:
1.说明:在东亚各国,除了通用的公元纪年之外,还有各自以前使用的阴历纪年法,在.net2.0种增加了针对东亚各国的日历类EastAsianLunisolarCalendar,
2.它是一个抽象类,有各种针对不同国家的的子类,其中ChineseLunisolarCalendar就是针对中国的日历类,它提公元纪年与中国传统农历纪年之间的相互转换,并能计算今天是农历几月几日星期几。
3.利用它可以计算天干地支等有关农历的信息,本程序就是来简单展示这个类的用法。它能计算的农历范围从公历1901-2-19至2101-1-28。
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
namespace ChineseCalendar
{
public class Calendar
{
private static ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
static void Main(string[] args)
{
//ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
ShowYearInfo();
ShowCurrentYearInfo();
Console.ReadLine();
}
/// <summary>
/// 展示阴历年份信息
/// </summary>
public static void ShowYearInfo()
{
for (int i = chineseDate.MinSupportedDateTime.Year; i < chineseDate.MaxSupportedDateTime.Year; i++)
{
Console.WriteLine("年份:{0},月份总数:{1},总天数:{2},干支序号:{3}", i, chineseDate.GetMonthsInYear(i),chineseDate.GetDaysInYear(i)
,chineseDate.GetSexagenaryYear(new DateTime(i,3,1)));
}
}
/// <summary>
/// 展示当前年份信息
/// </summary>
public static void ShowCurrentYearInfo()
{
int lYear=chineseDate.GetYear(DateTime.Now);
int lMonth=chineseDate.GetMonth(DateTime.Now);
int lDay=chineseDate.GetDayOfMonth(DateTime.Now);
int leapMonth = chineseDate.GetLeapMonth(lYear);//获取第几个月是闰月,等于0表示本年无闰月
//如果今年有闰月
if (leapMonth > 0)
{
//闰月数等于当前月份
if (lMonth == leapMonth)
{
Console.WriteLine("今年的阴历日期:{0}年闰{1}月{2}日。", lYear, lMonth - 1, lDay);
}
else if (lMonth > leapMonth)//
{
Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth - 1, lDay);
}
else
{
Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);
}
}
else
{
Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);
}
Console.WriteLine("今天的公历日期:" + DateTime.Now.ToString("yyyy-MM-dd"));
Console.WriteLine("今年阴历天数:{0},今年{1}闰年", chineseDate.GetDaysInYear(DateTime.Now.Year),(chineseDate.IsLeapYear(DateTime.Now.Year)==true)?"是":"不是");
Console.WriteLine("今年农历每月的天数:");//注意:如果有13个数字表示当年有闰月
for (int i = 1; i <= chineseDate.GetMonthsInYear(DateTime.Now.Year); i++)
{
Console.Write("{0,-5}",chineseDate.GetDaysInMonth(DateTime.Now.Year,i));
}
}
}
}
1.说明:在东亚各国,除了通用的公元纪年之外,还有各自以前使用的阴历纪年法,在.net2.0种增加了针对东亚各国的日历类EastAsianLunisolarCalendar,
2.它是一个抽象类,有各种针对不同国家的的子类,其中ChineseLunisolarCalendar就是针对中国的日历类,它提公元纪年与中国传统农历纪年之间的相互转换,并能计算今天是农历几月几日星期几。
3.利用它可以计算天干地支等有关农历的信息,本程序就是来简单展示这个类的用法。它能计算的农历范围从公历1901-2-19至2101-1-28。
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
namespace ChineseCalendar
{
public class Calendar
{
private static ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
static void Main(string[] args)
{
//ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();
ShowYearInfo();
ShowCurrentYearInfo();
Console.ReadLine();
}
/// <summary>
/// 展示阴历年份信息
/// </summary>
public static void ShowYearInfo()
{
for (int i = chineseDate.MinSupportedDateTime.Year; i < chineseDate.MaxSupportedDateTime.Year; i++)
{
Console.WriteLine("年份:{0},月份总数:{1},总天数:{2},干支序号:{3}", i, chineseDate.GetMonthsInYear(i),chineseDate.GetDaysInYear(i)
,chineseDate.GetSexagenaryYear(new DateTime(i,3,1)));
}
}
/// <summary>
/// 展示当前年份信息
/// </summary>
public static void ShowCurrentYearInfo()
{
int lYear=chineseDate.GetYear(DateTime.Now);
int lMonth=chineseDate.GetMonth(DateTime.Now);
int lDay=chineseDate.GetDayOfMonth(DateTime.Now);
int leapMonth = chineseDate.GetLeapMonth(lYear);//获取第几个月是闰月,等于0表示本年无闰月
//如果今年有闰月
if (leapMonth > 0)
{
//闰月数等于当前月份
if (lMonth == leapMonth)
{
Console.WriteLine("今年的阴历日期:{0}年闰{1}月{2}日。", lYear, lMonth - 1, lDay);
}
else if (lMonth > leapMonth)//
{
Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth - 1, lDay);
}
else
{
Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);
}
}
else
{
Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);
}
Console.WriteLine("今天的公历日期:" + DateTime.Now.ToString("yyyy-MM-dd"));
Console.WriteLine("今年阴历天数:{0},今年{1}闰年", chineseDate.GetDaysInYear(DateTime.Now.Year),(chineseDate.IsLeapYear(DateTime.Now.Year)==true)?"是":"不是");
Console.WriteLine("今年农历每月的天数:");//注意:如果有13个数字表示当年有闰月
for (int i = 1; i <= chineseDate.GetMonthsInYear(DateTime.Now.Year); i++)
{
Console.Write("{0,-5}",chineseDate.GetDaysInMonth(DateTime.Now.Year,i));
}
}
}
}
相关文章推荐
- 计算计算机网络的可靠度---因子定理计算网络可靠度的算法C#代码实现
- JavaScript查询"今天是农历几号"的代码
- 通过C#代码实现空间离散点的克里金(kriging)插值(一) 计算原理
- C#代码行数的计算工具(递归的实现)
- C# 实现农历计算和转换
- C#和SQL实现的字符串相似度计算代码分享
- C#和SQL实现的字符串相似度计算代码分享
- C#通过经纬度计算2个点之间距离的实现代码
- 计算计算机网络的可靠度---因子定理计算网络可靠度的算法C#代码实现
- 基于.Net平台应用程序唯一运行实例C#代码实现
- 代码实现C#中最大化最小化按钮方法
- asp.net(c#) RSS功能实现代码
- c#计算时间差实现方法
- C# 播放铃声最简短的代码实现方式
- 如何编写出高效的数据库连接池(附带完整代码C#和Java实现)
- 如何在C#代码中实现在Sqlserver2000中添加用户?以及附加数据库?
- C#发送邮箱实现代码
- Atitit.http代理的实现 代码java php c# python
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]