https://blog.csdn.net/cstester/article/details/7407044

  1. using System.Globalization;
  2. class CnCanlendar_nongli
  3. {
  4. /// <summary>
  5. /// 实例化一个  ChineseLunisolarCalendar
  6. /// </summary>
  7. private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
  8. /// <summary>
  9. /// 十天干
  10. /// </summary>
  11. private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
  12. /// <summary>
  13. ///  十二地支
  14. ///  </summary>
  15. private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
  16. /// <summary>
  17. /// 十二生肖
  18. /// </summary>
  19. private static string[] sx = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
  20. /// <summary>
  21. ///  返回农历天干地支年
  22. ///   </summary>
  23. ///    <param name="year">农历年</param>
  24. ///    <returns></returns>
  25. public static string GetLunisolarYear(int year)
  26. {
  27. if (year > 3)
  28. {
  29. int tgIndex = (year - 4) % 10;
  30. int dzIndex = (year - 4) % 12;
  31. return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
  32. }
  33. throw new ArgumentOutOfRangeException("无效的年份!");
  34. }
  35. /// <summary>
  36. /// 农历月
  37. /// </summary>
  38. /// <returns></returns>
  39. private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
  40. /// <summary>
  41. /// 农历日
  42. /// </summary>
  43. private static string[] days1 = { "初", "十", "廿", "三" };
  44. /// <summary>
  45. ///  农历日
  46. /// </summary>
  47. private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
  48. /// <summary>
  49. /// 返回农历月
  50. /// </summary>
  51. /// <param name="month">月份</param>
  52. /// <returns></returns>
  53. public static string GetLunisolarMonth(int month)
  54. {
  55. if (month < 13 && month > 0)
  56. {
  57. return months[month - 1];
  58. }
  59. throw new ArgumentOutOfRangeException("无效的月份!");
  60. }
  61. /// <summary>
  62. /// 返回农历日
  63. /// </summary>
  64. /// <param name="day">天</param>
  65. /// <returns></returns>
  66. public static string GetLunisolarDay(int day)
  67. {
  68. if (day > 0 && day < 32)
  69. {
  70. if (day != 20 && day != 30)
  71. {
  72. return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
  73. }
  74. else
  75. {
  76. return string.Concat(days[(day - 1) / 10], days1[1]);
  77. }
  78. }
  79. throw new ArgumentOutOfRangeException("无效的日!");
  80. }
  81. /// <summary>
  82. /// 根据公历获取农历日期
  83. /// </summary>
  84. /// <param name="datetime">公历日期</param>
  85. /// <returns></returns>
  86. public static string GetChineseDateTime(DateTime datetime)
  87. {
  88. //农历的年月日
  89. int year = ChineseCalendar.GetYear(datetime);
  90. int month = ChineseCalendar.GetMonth(datetime);
  91. int day = ChineseCalendar.GetDayOfMonth(datetime);
  92. //获取闰月, 0 则表示没有闰月
  93. int leapMonth = ChineseCalendar.GetLeapMonth(year);
  94. bool isleap = false;
  95. if (leapMonth > 0)
  96. {
  97. if (leapMonth == month)
  98. {
  99. //闰月
  100. isleap = true;
  101. month--;
  102. }
  103. else if (month > leapMonth)
  104. {
  105. month--;
  106. }
  107. }
  108. return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
  109. }
  110. }

C#项目获取当前时间的农历时间的更多相关文章

  1. 代码空间项目 -- 获取当前时间之前的某一天-Calender类的使用

    Calendar类的静态方法getInstance()可以初始化一个日历对象:Calendar now = Calendar.getInstance(); 1.Calendar的基本用法calenda ...

  2. js获取当前农历时间

    <template> <div class="gaia-header"> <img alt="gaia_logo" src=&qu ...

  3. ASP.NET农历时间显示(两)

    在拍摄的月球时前(http://blog.csdn.net/yysyangyangyangshan/article/details/6802950),只是没有进行封装使用起来须要手动改动. 本次进行简 ...

  4. ASP.NET如何显示农历时间

    ASP.NET如何显示农历时间 CS部分代码如下: 代码如下: public string ChineseTimeNow = "";  public string ForignTi ...

  5. 项目心得——按照指定的日期/时间创建Date对象

    项目心得——按照指定的日期/时间创建Date对象 有时,在做项目中,需要获得指定日期的Date对象,这个指定的日期或者时间可能不是当前的时间.下面讲解两种获取指定日期/时间的Date对象的方法: pa ...

  6. Java 获取当前时间及实现时间倒计时功能

    引言 在一些项目中或是一些特殊的业务场景中,需要用到显示系统的当前时间,以及一些固定的时间倒计时,时间到后做一些什么事情的业务 .接下来咱们就具体看看代码是怎么实现的: <%@ page lan ...

  7. 用PHP获取系统时间时,时间比当前时间少8个小时

    自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...

  8. [WinAPI] API 14 [获取、设置文件属性和时间]

    >_< 为了获取文件属性,用户可以使用GetFileAttributes与GetFileAttributesEx函数. GetFileAttributesEx函数除了返回文件属性外,还返回 ...

  9. Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数

    格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: /** * 获取指定时间到格林威治时间的秒数 ...

随机推荐

  1. python3 写的一个压测脚本(有待开发)

    import requests import queue import threading import time status_code_list = [] exec_time = 0 class ...

  2. 图像识别opencv学习自修第一天【opencv的安装】

    1. 安装步骤 (1)安装python (2)安装xcode (3)使用macports现成包安装opencv (4)安装scipy 2. 安装实战 (1)已经安装好了python,并安装好了virt ...

  3. 如何在Mac系统安装MySQL

    方法一: (1)使用brew install mysql (2)使用mysql -uroot连接时报错: Authentication plugin 'caching_sha2_password' c ...

  4. Ubuntu16.04网络不能访问解决办法

      问题: 系统重启后,网络不能正常使用,加载网络配置失败,且重启网络时也提示错误. 解决方法: 在定位的过程中发现是配置中的网络设备号与实际设备号不符. 1.查看网络配置中的配备号: vi /etc ...

  5. 2018-南京网络赛icpc-L题(分层最短路)

    题意:给你n个点,m条边的有向带权图,然后你每次可以选<=k条边的边权变成0,问你1到n的最短路: 解题思路:这道题基本上就是原题了呀,bzoj2763(无向图),解法就是拆点跑分层的最短路,比 ...

  6. Nginx 接受上游缓存流程

    L:101 这个指令主要是由上游服务器来决定是否缓存 详见博客Nginx 针对上游服务器缓存

  7. Vmware 给虚拟机传脚本并执行

    #_*_ coding:utf8 _*_ from pysphere import VIServer import ssl import re import sys import os import ...

  8. 五一培训 清北学堂 DAY2

    今天还是冯哲老师的讲授~~ 今日内容:简单数据结构(没看出来简单qaq) 1.搜索二叉树 前置技能 一道入门题在初学OI的时候,总会遇到这么一道题.给出N次操作,每次加入一个数,或者询问当前所有数的最 ...

  9. haar的简单应用(1)

    又双叒叕出现问题了,还是重置系统的原因,弄半天终于弄好了,就不说这个了 直接记录一下该做的 如下教程中的文件我倒是都找到了(但是这些文件在目前的代码中都用不到..) 在这里 令我无语的他只用了一个.x ...

  10. MT【257】任意存在并存

    函数$f(x)=\dfrac{4x}{x+1}(x>0),g(x)=\dfrac{1}{2}(|x-a|-|x-b|),(a<b)$, 若对任意$x_1>0$,存在$x_2\le x ...