Time and date calculation code in asp.net C

Source: Internet
Author: User

[Topic description] There are two dates, and the number of days between the two dates is calculated. If the two dates are consecutive, we set the number of days between them to two days. [input] There are multiple groups of data, each data set has two rows, representing two dates in the form of YYYYMMDD.

[Output] Output a row of data in each group, that is, the date difference.

[Example input] 2011041220110422

[Sample output] 11

A very simple question. The idea is very simple. Calculate the number of days between the two dates respectively to the one-year month, January 1, and then subtract and add one. The number of days between calculation and 00010101 contains the following common date calculation contents.

Leap year judgment Expression

According to the Gregorian calendar: the second day of the year, the second day of the year, but the four-year leap surplus, only 23 hours 15 minutes 4 seconds, a day, not too much, more than 44 minutes 56 seconds, accumulated to 25 minutes, 17 hours, 58 minutes, 24 seconds, about 3/4 of a day, so every hundred years of waste a year, to 400th years without waste.

The expression used to judge a leap year is as follows (in C)

The Code is as follows: Copy code

! (Y % 400) | (! (Y % 4) & y % 100 ))

 

Or

Y % 4? 0 :( y % 100? 1 :( y % 400? ))

Number of days from January 1, January 1

First, use the number of days in the first few months of the array index, and add the number of days in the current month. If it is a month greater than 3 and a leap year (pay attention to the judgment order, use | short circuit), add one day, then, the total number of days in the previous year is added. Here, the formula r + = -- y * 365 + y/4-y/100 + y/400 is used for calculation.

The Code is as follows: Copy code

Int R [] = {120,151,181,212,243,273,304,334 };
Int calc (int y ){
Int m = y % 10000/100;
Int r = M-1] + y % 100;
Y/= 10000;
R + = (m> 2 &&(! (Y % 400) | (! (Y % 4) & amp; y % 100 )))? 1:0;
R + = -- y * 365 + y/4-y/100 + y/400;
Return r;
}

The original program code is as follows:

The Code is as follows: Copy code

# Include <stdio. h>
Int R [] = {120,151,181,212,243,273,304,334 };
Int calc (int y ){
Int m = y % 10000/100;
Int r = M-1] + y % 100;
Y/= 10000;
R + = (m> 2 &&(! (Y % 400) | (! (Y % 4) & amp; y % 100 )))? 1:0;
Return r + -- y * 365 + y/4-y/100 + y/400;
}
Int main (){
Int x, y;
While (scanf ("% d", & x, & y )! = EOF) printf ("% dn", calc (y)-calc (x) + 1 );
}

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.