Summary: Detailed explanation of the DateTime.Now function of C#
??? //April 24, 2008
??? System.DateTime.Now.ToString( “D”);
??? //2008-4-24
??? System.DateTime.Now.ToString(“d”);
??? //April 24, 2008 Day 16:30:15
??? System.DateTime.Now.ToString(“F”);
??? //16:30, April 24, 2008
??? 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/4/24 8:30:15
??? System.DateTime.Now.ToString(“U”);
??? //2008-04-24 16:30: 15Z
??? System.DateTime.Now.ToString(“u”);
??? //April 24th
??? 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”);
??? //April 2008
??? 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< br> ??? System.DateTime.Now.ToString(“s”);
??? //2008-04-24 15:52:19
??? System.DateTime.Now.ToString( “yyyy-MM-dd HH: mm: ss: ffff”);
??? //2008-04-24 15:56:48
??? System.DateTime.Now.ToString(” yyyy year MM month dd HH hour mm minute ss second”);
??? //Tuesday, April 24 2008
??? System.DateTime.Now.ToString(“dddd, MMMM dd yyyy”) ;
??? //Tue, April 24 ’08
??? System.DateTime.Now.ToString(“ddd, MMM d “‘”yy”);
??? // Tuesday, April 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 type conversion to string
?? 12345.ToString(“n”);? //Generate 12,345.00
?? 12345.ToString(“C”); //Generate ¥12,345.00
?? 12345.ToString(“e”); //Generate 1.234500e+004
?? 12345.ToString(“f4”); //Generate Into 12345.0000
?? 12345.ToString(“x”); //generate 3039 (hexadecimal)
?? 12345.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 one
??? DateTime.Now.AddDays(-1).ToShortDateString();
??? //Tomorrow, in the same way, add one
??? DateTime.Now.AddDays(1 ).ToShortDateString();
??? //this week (to know the first day of the week, you must first know what day today is, so that the first day of the week is the day a few days ago , It should be noted that every week here is from Sunday to Saturday
??? 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 still don’t understand, look at the method of displaying the day of the week in Chinese.
??? //Since DayOfWeek returns a numeric day of the week, we have to convert it into Chinese characters for easy reading. Some people You may use switch to compare one by one, but it doesn’t take so much trouble?????????????
??? string[] Day = new string[]{ “Sunday”, “week One”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday” };
??? Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
??? //Last week, for the same reason, a week is 7 days, last week is this week and then 7 days are subtracted, and the next week is the same.
??? DateTime.Now.AddDays(Convert.ToDoub le((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 will say that the first day of the month must be the 1st, and the last day is the 1st of the next month minus one day. Of course this is correct.
??? //General way of writing
??? DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + “1”; //First Days
??? DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + “1”).AddMonths(1).AddDays(-1).ToShortDateString() ;//The last day
??? //It’s easier to use ToString character formatting in C#
??? DateTime.Now.ToString(“yyyy-MM-01”);
?? ? DateTime.Parse(DateTime.Now.ToString(“yyyy-MM-01”)).AddMonths(1).AddDays(-1).ToShortDateString();
??? //Last month, minus 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, add 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
??? Da teTime.Now.AddDays(-7).ToShortDateString();
??? DateTime.Now.Date.ToShortDateString();
??? //This year, it is easy for us to format with ToString characters Calculate the first and last day of the year
??? 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 it
??? DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(-1).ToShortDateString();
??? DateTime.Parse(DateTime.Now.ToString(” yyyy-01-01″)).AddDays(-1).ToShortDateString();
??? //Next 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 it difficult to write a long process to judge. Actually, we all know that there are four quarters in a year, and three months in a quarter.
??? //First, we first push the date to the first month of the quarter, and then the first day of the month is the quarter
??? DateTime.Now.AddMonths(0-((DateTime.Now.Month-1)% 22)).ToString(“yyyy-MM-01”);
?? ? //Similarly, the last day of the quarter is the first day of the next quarter minus one
??? DateTime.Parse(DateTime.Now.AddMonths(22-((DateTime.Now.Month-1)% 22 )).ToString(“yyyy-MM-01”)).AddDays(-1).ToShortDateString();
??? //Next quarter, I believe you all know. . . . Closed
??? 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();
? ?? //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( );
The above article is for your own notes only
This article is reproduced from: http://big5.china.com/gate/big5/wawdlphez.blog.china.com/201007 /6621544.html
Article signature file
Original text: Large column C#’s DateTime.Now function detailed explanation