C#日期类型转化总结【转化,农历,节气,星期】

转为日期类型

将8位日期字符串转换为日期格式

dateStr = "20220203";
System.IFormatProvider format=new System.Globalization.CultureInfo("zh-CN",true); 
string strDateFormat = "yyyyMMdd";//指定转换格式
date = DateTime.ParseExact(dateStr,strDateFormat,format, DateTimeStyles.AllowWhiteSpaces); 

将6位字符串转化为日期格式

dateStr = "202202";
System.IFormatProvider format=new System.Globalization.CultureInfo("zh-CN",true); 
string strDateFormat = "yyyyMM";//指定转换格式 
date = DateTime.ParseExact(dateStr,strDateFormat,format, DateTimeStyles.AllowWhiteSpaces); 

var str = "2022/02/03";
var time = new DateTime();
if (DateTime.TryParse(str, out time))
{
   Console.WriteLine($"转化成功{time}");
}

采用拼接连接符的方式进行日期转换(不建议采用)

 //分别获取20051126中的年、月、日字符串     
string yyyy="20051126".Substring(0,4);     
string mm="20051126".Substring(4,2);     
string dd="20051126".Substring(5,2); //拼写符合日期格式的字符串     
string riqi=yyyy+"-"+mm+"-"+dd;  //将符合日期格式的字符串转化为DateTime数据类型     
DateTime dt = Convert.ToDateTime(riqi); 

使用字符串插入函数进行日期转换

// 把20051126变为2005/11/26
DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo();
dtFormat.ShortDatePattern = "yyyy/MM/dd";
string s = "20051126";     
s = s.Insert(4,"/");     
s = s.Insert(7,"/");
DateTime dt = Convert.ToDateTime(s, dtFormat);

获取星期几

string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
var a = Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
Console.WriteLine(a);

DateTime时间格式转换为Unix时间戳格式

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000;  //除10000调整为13位

优雅的获取农历

ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
int year = ChineseCalendar.GetYear(DateTime.Now);
int day = ChineseCalendar.GetDayOfMonth(DateTime.Now);
int month = ChineseCalendar.GetMonth(DateTime.Now);
int leapMonth = ChineseCalendar.GetLeapMonth(year);
string date = string.Format("农历{0}{1}({2})年{3}{4}月{5}{6}"
, "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10]
, "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12]
, "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12]
, month == leapMonth ? "闰" : ""
, "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]
, "初十廿三"[day / 10]
, "十一二三四五六七八九"[day % 10]);
 Console.WriteLine($"当前时间:{DateTime.Now},农历是:"+date);
//输出:当前时间:2022/10/18 18:27:00,农历是:农历壬寅(虎)年九月廿三

获取二十四节气

//输入new DateTime(2022,10,23) 输出 霜降
//输入new DateTime(2022,10,8) 输出 寒露
//输入new DateTime(2022,10,18) 输出  “”  【暂无节气】
public static string ChineseTwentyFourDay(DateTime date1)
{
        string[] SolarTerm = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
        int[] sTermInfo = new int[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
        DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
        DateTime newDate;
        double num;
        int y;
        string tempStr = "";
 
        y = date1.Year;
 
        for (int i = 1; i <= 24; i++)
        {
            num = 525948.76 * (y - 1900) + sTermInfo[i - 1];
 
            newDate = baseDateAndTime.AddMinutes(num);//按分钟计算
            if (newDate.DayOfYear == date1.DayOfYear)
            {
                tempStr = SolarTerm[i - 1];
                break;
            }
        }
                public static string ChineseTwentyFourDay(DateTime date1)
        {
            string[] SolarTerm = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
            int[] sTermInfo = new int[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
            DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
            DateTime newDate;
            double num;
            int y;
            string tempStr = "";

            y = date1.Year;

            for (int i = 1; i <= 24; i++)
            {
                num = 525948.76 * (y - 1900) + sTermInfo[i - 1];

                newDate = baseDateAndTime.AddMinutes(num);//按分钟计算
                if (newDate.DayOfYear == date1.DayOfYear)
                {
                    tempStr = SolarTerm[i - 1];
                    break;
                }
            }
            return tempStr;
        }return tempStr;
}

转为字符串类型

 //2021年4月24日
 System.DateTime.Now.ToString("D");
 //2021-4-24
 System.DateTime.Now.ToString("d");
 //2021年4月24日 16:30:15
 System.DateTime.Now.ToString("F");
 //2021年4月24日 16:30
 System.DateTime.Now.ToString("f");
 //2021-4-24 16:30:15
 System.DateTime.Now.ToString("G");
 //2021-4-24 16:30
 System.DateTime.Now.ToString("g");
 //16:30:15
 System.DateTime.Now.ToString("T");
 //16:30
 System.DateTime.Now.ToString("t");
 //2021年4月24日 8:30:15
 System.DateTime.Now.ToString("U");
 //2021-04-24 16:30:15Z
 System.DateTime.Now.ToString("u");
 //4月24日
 System.DateTime.Now.ToString("m");
 System.DateTime.Now.ToString("M");
 //Tue, 24 Apr 2021 16:30:15 GMT
 System.DateTime.Now.ToString("r");
 System.DateTime.Now.ToString("R");
 //2021年4月
 System.DateTime.Now.ToString("y");
 System.DateTime.Now.ToString("Y");
 //2021-04-24T15:52:19.1562500+08:00
 System.DateTime.Now.ToString("o");
 System.DateTime.Now.ToString("O");
 //2021-04-24T16:30:15
 System.DateTime.Now.ToString("s");
 //2021-04-24 15:52:19
 System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
 //2021年04月24 15时56分48秒
 System.DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");
 //星期二, 四月 24 2021
 System.DateTime.Now.ToString("dddd, MMMM dd yyyy");
 //二, 四月 24 ’08
 System.DateTime.Now.ToString("ddd, MMM d \"’\"yy");
 //星期二, 四月 24
 System.DateTime.Now.ToString("dddd, MMMM dd");
 //4-08
 System.DateTime.Now.ToString("M/yy");
 //24-04-08
 System.DateTime.Now.ToString("dd-MM-yy");

 //今天 获取短时间类型   2022/10/18 0:00:00
 DateTime.Now.Date.ToShortDateString();

 //7天后
 DateTime.Now.Date.ToShortDateString();
 DateTime.Now.AddDays(7).ToShortDateString();
 //7天前
 DateTime.Now.AddDays(-7).ToShortDateString();
 DateTime.Now.Date.ToShortDateString();


推荐阅读

资料参考

欢迎关注我的公众号“Net分享”,技术文章第一时间推送 , 分享一些你可能注意不到的细节。

posted @ 2022-10-18 17:50  今晚打老虎!  阅读(331)  评论(0编辑  收藏  举报