C # skillfully use datetime preset optional date range (such as this year, this quarter, this month, etc.) _c# tutorials

Source: Internet
Author: User
Tags datetime

This article examples for you to share the C # datetime preset Optional date range of the relevant code, you can choose this year, this quarter, this month, etc. for your reference, the specific content as follows

Effect:

When you make a report or query, you will have to preset some optional date range for the user (pictured above)
Such as this year's sales, this quarter profit, new customers this month
The built-in datetime in C # can basically implement these functions, and using DateTime will make it easier for you to deal with these things.

        
        Today DateTime.Now.Date.ToShortDateString (); Yesterday, that is today's date minus one DateTime.Now.AddDays (-1).
        ToShortDateString (); Tomorrow, by the same token, add a DateTime.Now.AddDays (1).

        ToShortDateString (); This week (to know that the first day of the week is the day of the week, knowing that the first day of the week is a few days ago, note that every week is from Sunday to Saturday DateTime.Now.AddDays (convert.todouble) (0-con Vert. ToInt16 (DateTime.Now.DayOfWeek))).
        ToShortDateString (); DateTime.Now.AddDays (Convert.todouble (6-convert.toint16 (DateTime.Now.DayOfWeek))).
        ToShortDateString ();       
        If you do not understand, and then look at the Chinese to show the day of the week should understand the method//Because DayOfWeek returns the number of days of the week, we have to convert it to Chinese characters to facilitate our reading, some people may use switch to one by one to control, in fact, not so troublesome
        string[] Day = new string[] {"Sunday", "Monday", "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 minus 7 days this week, and next week is the same DateTime.Now.AddDays (Convert.todouble (0-convert.toint16) (DateTime.Now.DayOfWe EK))-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 this month is definitely number 1th, the last day is next month, one more day. Of course, this is the right//general wording DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"; First day DateTime.Parse (DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + "1"). Addmonths (1). AddDays (-1).
        ToShortDateString ()//The last day///The most convenient DateTime.Now.ToString ("yyyy-mm-01") in the character of ToString in C #; DateTime.Parse (DateTime.Now.ToString ("yyyy-mm-01")). Addmonths (1). AddDays (-1).

        ToShortDateString (); Last month, subtract one 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 after DateTime.Now.Date.ToShortDateString (); DateTime.Now.AddDays (7).
        ToShortDateString (); 7 days ago DateTime.Now.AddDays (-7).
        ToShortDateString ();

        DateTime.Now.Date.ToShortDateString (); This year, it is easy to figure out the first and last day of the year DateTime.Parse (DateTime.Now.ToString ("yyyy-01-01")) with the character of ToString.
        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 feel the difficulty here, need to write a long process to judge. In fact, we all know that four quarters a year, a quarter three months//first we push the date to the first month of the quarter, then the first day of the month is the first day of the quarter DateTime.Now.AddMonths (0-(datetime.no W.MONTH-1) (% 3)).
        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 3-((datetime.now.month-1)% 3)). ToString ("yyyy-mm-01")). AddDays (-1).
        ToShortDateString (); Next quarter, I'm sure you all know .... Wrap up DateTime.Now.AddMonths (3-((datetime.now.month-1)% 3)).
        ToString ("yyyy-mm-01"); DateTime.Parse (DateTime.Now.AddMonths (6-(datetime.now.month-1)% 3)). ToString ("yyyy-mm-01")). AddDays (-1).
        ToShortDateString (); Last quarter DateTime.Now.AddMonths ( -3-((datetime.now.month-1)% 3)).
        ToString ("yyyy-mm-01"); DateTime.Parse (DateTime.Now.AddMonths (0-(datetime.now.month-1)% 3)). ToString ("yyyy-mm-01")). AddDays (-1). ToshortdatestrING ();
 

DateTime comparison
I used the date comparison in the Log digest, I need to compare the time between the post and the current date, if it was released within three days, then I will add the word "new" in front of the log.
My previous algorithm is very troublesome, is to convert the string into plastic, and then calculate.

I'd like to use DateTime to compare directly. I first convert the release date (string) to the date type:

/Calculate Release date
   datetime pubdate=new datetime ();
   Pubdate=item["creator"];
   String strpubdate=item["creator". ToString ();
    DateTime dt1=new datetime (DateTime.Parse (strpubdate));

At the very beginning, I added three days directly to the above statement:
DateTime dt1=new datetime (int. Parse (stry), Int. Parse (STRM), (int. Parse (STRD) +3), 0,0,0,0);
As a result, there is always an error. What's the reason?
I followed the debugging and found out the problem. When the published log after 28th of each month, because directly in the conversion time added 3, such as 29+3=32,32 is no way to convert to date type.
So I corrected the error and then, in comparison, DateTime.AddDays (3) To do the operation:

 Calculates the current date
   datetime currentdate=new datetime ();
   Currentdate=datetime.now;
   
   if (currentdate<dt1. ADDDAYS (3))
   {
    str=str+ "! New ";
   }
 

If you only want to compare the dates to be equal, you can use Datetime.compare (DT1,DT2).

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.