确定日期之间的差异 [英] Determining the difference between dates

查看:127
本文介绍了确定日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一个方法让我的程序取一个日期(如2003年2月2日),并显示两者之间的差异与另一个日期(如2012年4月2日,不包括闰年)。到目前为止,我只能确定如果日期在同一个月,只是通过减去日。在这个程序中,我使用2套月,日和年整数。我从这里走到哪里都很迷茫。这是我任务的一个完全可选的部分,但我想知道如何让它工作。这似乎对我很麻烦,但也许有一个简单的数学公式我不在想。



对不起,我没有任何预先存在的代码

解决方案

这部分是因为其余的任务只是让用户输入日期,然后添加和减去一天。因为你正在寻找数学公式,它将帮助你找到一个解决你的问题的解决方案。让Y为年,M为月,D为天。对这两个日期进行此计算。



总计= Y * 365 + M * 30 + D,然后找出相应日期的2个总计之间的差额。 >

在将30乘以M值时,必须给出该月的天数。你可以使用#define value或if循环。同样,你也可以通过将366乘以Y来做闰年。



希望这将有助于....


I'm trying to figure out a way for my program to take a date (like February 2nd, 2003) and show the difference between the two with another date (like April 2nd, 2012), excluding leap years. So far I've only been able to figure it out if the dates are in the same month, just by subtracting the "day". In this program I use 2 sets of "month", "day" and "year" integers. I'm pretty much at a loss from where to go from here. This is a completely optional part of my assignment but I'd like to get an idea on how to get it to work. It seems like a hassle to me, but maybe there's a simple math formula I'm not thinking about?

Sorry, I don't have any pre-existing code for this part because the rest of the assignment just deals with having the user enter dates and then adding and subtracting a single day.

解决方案

Since you are looking for mathematical formula , it will help you to find a solution to your problem. Let Y be the year,M be the month and D be the day. Do this calculation for both the dates.

Total = Y* 365 + M*30 + D ,then find the difference between 2 totals of the corresponding dates.

While multiplying 30 with M value ,you have to give the number of days in that month. You can do it with #define value or if loop. Similarly you can do for leap year too by multiplying 366 with Y .

Hope this will help u....

这篇关于确定日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆