The usage of DateTime.Now function in c

  • 2020-05-10 18:41:42
  • OfStack


//2008 years 4 month 24 day 
     System.DateTime.Now.ToString("D");
     //2008-4-24
     System.DateTime.Now.ToString("d");
     //2008 years 4 month 24 day  16:30:15
     System.DateTime.Now.ToString("F");
     //2008 years 4 month 24 day  16:30
     System.DateTime.Now.ToString("f");
     //2008-4-24 16:30:15
     System.DateTime.Now.ToString("G");
     //2008-4-24 16:30
     System.DateTime.Now.ToString("g");
     //16:30:15
     System.DateTime.Now.ToString("T");
     //16:30
     System.DateTime.Now.ToString("t");
     //2008 years 4 month 24 day  8:30:15
     System.DateTime.Now.ToString("U");
     //2008-04-24 16:30:15Z
     System.DateTime.Now.ToString("u");
     //4 month 24 day 
     System.DateTime.Now.ToString("m");
     System.DateTime.Now.ToString("M");
     //Tue, 24 Apr 2008 16:30:15 GMT
     System.DateTime.Now.ToString("r");
     System.DateTime.Now.ToString("R");
     //2008 years 4 month 
     System.DateTime.Now.ToString("y");
     System.DateTime.Now.ToString("Y");
     //2008-04-24T15:52:19.1562500+08:00
     System.DateTime.Now.ToString("o");
     System.DateTime.Now.ToString("O");
     //2008-04-24T16:30:15
     System.DateTime.Now.ToString("s");
     //2008-04-24 15:52:19
     System.DateTime.Now.ToString("yyyy-MM-dd HH : mm : ss : ffff");
     //2008 years 04 month 24 15 when 56 points 48 seconds 
     System.DateTime.Now.ToString("yyyy years MM month dd HH when mm points ss seconds ");
     // week 2, 4 month  24 2008
     System.DateTime.Now.ToString("dddd, MMMM dd yyyy");
     //2, 4 month  24 '08
     System.DateTime.Now.ToString("ddd, MMM d \"'\"yy");
     // week 2, 4 month  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");
    // Character conversion to a string 
.ToString("n");  // generate  12,345.00
.ToString("C"); // generate   RMB 12,345.00
.ToString("e"); // generate  1.234500e+004
.ToString("f4"); // generate  12345.0000
.ToString("x"); // generate  3039 (16 Into the system )
.ToString("p"); // generate  1,234,500
     // This year's sales, this quarter's profit, this month's new customers  
   // Today, 
     DateTime.Now.Date.ToShortDateString();
     // Yesterday is today's date minus 1
     DateTime.Now.AddDays(-1).ToShortDateString();
     // Tomorrow, same thing, plus 1
     DateTime.Now.AddDays(1).ToShortDateString();
     // This week, ( Be aware of the week's first 1 You need to know what day of the week it is to know the first day of the week 1 The day was just a few days ago 1 God, notice everything here 1 Week starts from Sunday to week 6 check 
     DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
     DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
     // If you don't understand, look again 1 The next way to show the day of the week in Chinese should be understood 
     // Due to the DayOfWeek It returns the number of days of the week, and we're going to convert it into Chinese characters for us to read, which some people might use switch to 1 a 1 Compare the ground, need not bother so actually              
     string[] Day = new string[]{ " Sunday ", " week 1", " week 2", " week 3", " week 4", " week 5", " week 6" };
     Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
     // Last week, similarly, 1 A week is 7 God, last week was this week minus that 7 God, next week, too 1 sample 
     DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
     DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
     // Next week, 
     DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
     DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
     // This month, , Many people would say the first of the month 1 Oh, my god 1 Number, the last 1 The day is next month 1 Number will be a reduction of 1 Days. Of course this is true 
     //1 The written like 
     DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; // The first 1 day 
     DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();// The last 1 day 
     // Use opportunely C# In the ToString Is easier to format 
     DateTime.Now.ToString("yyyy-MM-01");
     DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();
     // Last month, minus 1 A month 
     DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
     DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
     // Next month, plus 1 A month 
     DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();
     DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();
     //7 Days later 
     DateTime.Now.Date.ToShortDateString();
     DateTime.Now.AddDays(7).ToShortDateString();
     //7 Days ago, 
     DateTime.Now.AddDays(-7).ToShortDateString();
     DateTime.Now.Date.ToShortDateString();
     // This year, use ToString The character formatting we also easily calculate out of the year 1 And the last 1 day 
     DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();
     DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();
     // Last year, no need to explain 
     DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString();
     DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();
     // For the next school year 
     DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString();
     DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();
     // This quarter, many people will find this difficult, need to write a long process to judge. No, we all know that 1 years 4 A quarter, 1 A quarter of 3 months 
     // First, let's push the date to the end of the quarter 1 Month, and then the first of the month 1 Day is the first day of the season 1 days 
     DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 22)).ToString("yyyy-MM-01");
     // Similarly, at the end of the quarter 1 Day is the first day of the next season 1 Day minus 1
     DateTime.Parse(DateTime.Now.AddMonths(22 - ((DateTime.Now.Month - 1) % 22)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
     // Next season, as I'm sure you all know... Call it a day 
     DateTime.Now.AddMonths(22 - ((DateTime.Now.Month - 1) % 22)).ToString("yyyy-MM-01");
     DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 22)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
     // In the last quarter 
     DateTime.Now.AddMonths(-22 - ((DateTime.Now.Month - 1) % 22)).ToString("yyyy-MM-01");
     DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 22)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();

Related articles: