C# DateTime.Now.ToString() 用法

string dateTime1 = DateTime.Now.ToString("D");

            Console.WriteLine(dateTime1);//2022年2月24日


            string dateTime2 = DateTime.Now.ToString("d");

            Console.WriteLine(dateTime2);//2022/2/24

            string dateTime3 = DateTime.Now.ToString("F");

            Console.WriteLine(dateTime3);//2022年2月24日 14:50:07

            string dateTime4 = DateTime.Now.ToString("f");

            Console.WriteLine(dateTime4);//2022年2月24日 14:50

            string dateTime5 = DateTime.Now.ToString("G");

            Console.WriteLine(dateTime5);//2022/2/24 14:50:07

            string dateTime6 = DateTime.Now.ToString("g");

            Console.WriteLine(dateTime6);//2022/2/24 14:50

            string dateTime7 = DateTime.Now.ToString("T");

            Console.WriteLine(dateTime7);//14:50:07

            string dateTime8 = DateTime.Now.ToString("t");

            Console.WriteLine(dateTime8);//14:50

            string dateTime9 = DateTime.Now.ToString("U");

            Console.WriteLine(dateTime9);//2022年2月24日 6:50:07

            string dateTime10 = DateTime.Now.ToString("u");

            Console.WriteLine(dateTime10);//2022-02-24 14:50:07Z

            string dateTime11 = DateTime.Now.ToString("m");

            Console.WriteLine(dateTime11);//2月24日

            string dateTime12 = DateTime.Now.ToString("M");

            Console.WriteLine(dateTime12);//2月24日

            string dateTime13 = DateTime.Now.ToString("r");

            Console.WriteLine(dateTime13);//Thu, 24 Feb 2022 14:50:07 GMT

            string dateTime14 = DateTime.Now.ToString("R");

            Console.WriteLine(dateTime14);//Thu, 24 Feb 2022 14:50:07 GMT

            string dateTime15 = DateTime.Now.ToString("y");

            Console.WriteLine(dateTime15);//2022年2月

            string dateTime16 = DateTime.Now.ToString("Y");

            Console.WriteLine(dateTime16);//2022年2月

            string dateTime17 = DateTime.Now.ToString("o");

            Console.WriteLine(dateTime17);//2022-02-24T14:50:07.6460955+08:00

            string dateTime18 = DateTime.Now.ToString("O");

            Console.WriteLine(dateTime18);//2022-02-24T14:50:07.6460955+08:00

            string dateTime19 = DateTime.Now.ToString("s");

            Console.WriteLine(dateTime19);//2022-02-24T14:50:07

            string dateTime20 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");

            Console.WriteLine(dateTime20);//022-02-24 14:50:07:6471

            string dateTime21 = DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");

            Console.WriteLine(dateTime21);//2022年02月24 14时50分07秒

            string dateTime22 = DateTime.Now.ToString("dddd, MMMM dd yyyy");

            Console.WriteLine(dateTime22);//星期四, 二月 24 2022

            string dateTime23 = DateTime.Now.ToString("ddd, MMM d \"'\"yy");

            Console.WriteLine(dateTime23);//周四, 2月 24 '22

            string dateTime24 = DateTime.Now.ToString("dddd, MMMM dd");

            Console.WriteLine(dateTime24);//星期四, 二月 24

            string dateTime25 = DateTime.Now.ToString("M/yy");

            Console.WriteLine(dateTime25);//2/22

            string dateTime26 = DateTime.Now.ToString("dd-MM-yy");

            Console.WriteLine(dateTime26);//24-02-22

            string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

            // 昨天
            string dateTime27 = DateTime.Now.AddDays(-1).ToShortDateString();
            Console.WriteLine(dateTime27);//2022/2/23

            // 明天
            string dateTime28 = DateTime.Now.AddDays(1).ToShortDateString();

            Console.WriteLine(dateTime28);//2022/2/25

            //本周星期天
            string dateTime29 = DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
            Console.WriteLine(dateTime29);//2022/2/20

            //本周星期六

            string dateTime30 = DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
            Console.WriteLine(dateTime30);//2022/2/26

            //今天周几
            string dateTime31 = Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
            Console.WriteLine(dateTime31);//星期四

            //上周日
            string dateTime32 = DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
            Console.WriteLine(dateTime32);//2022/2/13

            //上周六
            string dateTime33 = DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
            Console.WriteLine(dateTime33);//2022/2/19

            // 下周日
            string dateTime34 = DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
            Console.WriteLine(dateTime34);//2022/2/27

            // 下周六
            string dateTime35 = DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
            Console.WriteLine(dateTime35);//2022/3/5

            // 本月一号
            string dateTime36 = DateTime.Now.ToString("yyyy-MM-01");
            Console.WriteLine(dateTime36);//2022-02-01

            // 本月最后一天
            string dateTime37 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();
            Console.WriteLine(dateTime37);//2022/2/28

            // 上个月一号
            string dateTime38 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
            Console.WriteLine(dateTime38);//2022/1/1

            //下个月一号
            string dateTime39 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();
            Console.WriteLine(dateTime39);//2022/3/1

            Console.ReadKey();

代码执行结果

C# DateTime.Now.ToString() 用法_第1张图片 

 

你可能感兴趣的:(c#学习,c#)