25:计算两个日期之间的天数

25:计算两个日期之间的天数

总时间限制: 
1000ms

内存限制: 
65536kB
描述

给定两个日期,计算相差的天数。比如2010-1-1和2010-1-3相差2天。

输入
共两行:
第一行包含三个整数startYear,startMonth,startDay,分别是起始年、月、日。
第二行包含三个整数endYear,endMonth,endDay,分别是结束年、月、日。
相邻两个整数之间用单个空格隔开。

年份范围在1~3000。保证日期正确且结束日期不早于起始日期。

输出
输出一个整数,即是两个日期相差的天数。
样例输入
2008 1 1
2009 1 1
样例输出
366
提示
闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外,它们不是闰年。闰年的2月份有29天。

 1 #include<iostream>
 2 using namespace std;
 3 int bgyear,bgmonth,bgday;
 4 int enyear,enmonth,enday;
 5 int month[21]={0,31,28,31,30,31,30,31,31,30,31,30,31};//非闰年
 6 int rmonth[21]={0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年
 7 int flag=1;
 8 int tot=0;
 9 int main()
10 {
11     cin>>bgyear>>bgmonth>>bgday;
12     cin>>enyear>>enmonth>>enday;
13     for(int i=bgyear;i<=enyear+1;i++)//寻找年数上的差异
14     {
15         if((i%4==0&&i%100!=0)||(i%400==0))
16         {
17             for(int j=1;j<=12;j++)
18             {
19                 if(i==bgyear&&j<bgmonth)
20                 continue;//寻找开始月份
21                 for(int k=1;k<=rmonth[j];k++)
22                 {
23                     if(i==enyear&&j==enmonth&&k==enday)
24                     {
25                         cout<<tot;
26                         return 0;
27                     }
28                     if(k<bgday&&flag==1)
29                     continue;
30                     else
31                     {
32                         flag=0;
33                         tot++;
34                     }
35
36                 }
37
38             }
39         }//闰年
40         else
41         {
42
43             for(int j=1;j<=12;j++)
44             {
45                 if(i==bgyear&&j<bgmonth)
46                 continue;//寻找开始月份
47                 for(int k=1;k<=month[j];k++)
48                 {
49                     if(i==enyear&&j==enmonth&&k==enday)
50                     {
51                         cout<<tot;
52                         return 0;
53                     }
54                     if(k<bgday&&flag==1)
55                     continue;
56                     else
57                     {
58                         flag=0;
59                         tot++;
60                     }
61
62                 }
63
64             }
65         }//非闰年
66     }
67     cout<<tot;
68     return 0;
69 }

时间: 2024-08-25 01:04:15

25:计算两个日期之间的天数的相关文章

C++计算两个日期之间的天数

计算两个日期之间的天数的思路: 首先,判断输入的年份是不是闰年.年份是否相同?月份是否相同?日是否相同? 日月年有三种可能的情况: 同年同月.日数相减就出来了. 同年不同月.计算日期小的月份到年初的天数,计算日期大的月份到年初的天数.再把两个日期向减 不同年.先计算中间相隔几年,计算较小的日期到年底有多少天,再计算较大的日期距年初有多少天,将三个数向加. 代码如下: 1 #include<iostream> 2 #include<CString> 3 #include<cma

计算两个日期之间的天数

//计算两个日期之间的天数 - (NSInteger)calcDaysFromBegin:(NSDate *)beginDate end:(NSDate *)endDate { NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"]; //取两个日期对象的时间间隔: NSTimeInterval time= [endD

JS计算两个日期之间的天数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

java计算两个日期之间相差天数和相隔天数详解

大家看到文章标题"两个日期之间相差天数和相隔天数",是否有疑惑呢!从中文字面理解,"相差"和"相隔"是有区别的,然而就是这些区别害死很多人,却没有发现,在大量新增统计时是差之毫厘谬以千里,我能都发现是因为一个偶然的机会,一个项目运行几年却没有人发现,我在其中还不到一年,一开始写这些这代码的人根本没分清楚什么情况就写了,怪不得统计的数据总是有那么细微的差别,在于日期"相差"和"相隔"有某些特定的情况下是相等的

计算两个日期之间的天数差C++/java

1--Java 分析:调用java中Calendar类 int days(Date date1,Date date2){ Calendar cal = new Calendar.getInstance(); cal.setTime(date1); int time1 = cal.get(Calendar.DAY_OF_YEAR); cal.setTime(date2); int time2 = cal.get(Calendar.DAY_OF_YEAR); //long days = Math.a

As3 计算两个日期之间的天数差

? 1 /*日期转YYYYMMDD*/ ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

8.02 计算两个日期之间的天数

select datediff(ward_hd,allen_hd)from (select hiredate as ward_hdfrom empwhere ename='WARD') x,(select hiredate as allen_hdfrom empwhere ename='ALLEN')y;

8.2 计算两个日期之间的天数

select datediff(ward_hd,allen_hd)from (select hiredate as ward_hdfrom empwhere ename='WARD') x,(select hiredate as allen_hdfrom empwhere ename='ALLEN')y;

java计算两个日期之间相隔的天数

1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.Calendar; 4 import java.util.Date; 5 6 7 public class date { 8 9 /** 10 * @param args 11 * @throws ParseException 12 */ 13 public static void main(String[] arg