如何获得两个日期之间的差异在年/月/周/日? [英] How to get difference between two dates in Year/Month/Week/Day?

查看:220
本文介绍了如何获得两个日期之间的差异在年/月/周/日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得两个日期之间的差异在年/月/周/日以有效的方式?

How to get difference between two dates in Year/Month/Week/Day in an efficient way?

如。两个日期之间的差为1年,2月,3周,4天。

eg. difference between two dates is 1 Year, 2 Months, 3 Weeks, 4 Days.

今年(S)的差异再presents算,两个日期之间一个月(县),周(S)和天(S)。

Difference represents count of year(s), month(s), week(s) and day(s) between two dates.

推荐答案

这其实是相当棘手。天的不同总数会导致同样的结果。例如:

This is actually quite tricky. A different total number of days can result in the same result. For example:

  • 19日2008年6月至2010年6月19日= 2年,而且365 * 2天

  • 19th June 2008 to 19th June 2010 = 2 years, but also 365 * 2 days

19日2006年6月至2008年6月19日= 2年,但是由于闰年也是365 +366天

19th June 2006 to 19th June 2008 = 2 years, but also 365 + 366 days due to leap years

您可能想减去年,直到你得到的地方,你有两个日期这是不到一年的时间间隔点。然后减去个月,直到你得到的地方,你有两个日期这是不到一个月的时间间隔点。

You may well want to subtract years until you get to the point where you've got two dates which are less than a year apart. Then subtract months until you get to the point where you've got two dates which are less than a month apart.

进一步的混乱:减去(或增加)个月是棘手的,当你不妨先从3月30日的日期 - 什么是一个月前比

Further confusion: subtracting (or adding) months is tricky when you might start with a date of "30th March" - what's a month earlier than that?

甚至进一步的混乱(可能的是不相关的):甚至一天不总是24小时。夏令人?

Even further confusion (may not be relevant): even a day isn't always 24 hours. Daylight saving anyone?

甚至进一步的混乱(几乎可以肯定的没有的相关):即使一分钟并不总是60秒。闰秒是非常混乱...

Even further confusion (almost certainly not relevant): even a minute isn't always 60 seconds. Leap seconds are highly confusing...

我没有工作了,现在这样做的权利的确切正确的方式的时候 - 这个答案多半是提高的事实,它并不像它听起来几乎一样简单

I don't have the time to work out the exact right way of doing this right now - this answer is mostly to raise the fact that it's not nearly as simple as it might sound.

编辑:不幸的是,我不会有足够的时间来充分回答这个问题。我建议你​​开始定义一个结构重presenting一个周期

Unfortunately I'm not going to have enough time to answer this fully. I would suggest you start off by defining a struct representing a Period:

public struct Period
{
    private readonly int days;
    public int Days { get { return days; } }
    private readonly int months;
    public int Months { get { return months; } }
    private readonly int years;
    public int Years { get { return years; } }

    public Period(int years, int months, int days)
    {
        this.years = years;
        this.months = months;
        this.days = days;
    }

    public Period WithDays(int newDays)
    {
        return new Period(years, months, newDays);
    }

    public Period WithMonths(int newMonths)
    {
        return new Period(years, newMonths, days);
    }

    public Period WithYears(int newYears)
    {
        return new Period(newYears, months, days);
    }

    public static DateTime operator +(DateTime date, Period period)
    {
        // TODO: Implement this!
    }

    public static Period Difference(DateTime first, DateTime second)
    {
        // TODO: Implement this!
    }
}

我建议你先实施+运营商,这应告知区别方法 - 你应该确保第一+(Period.Difference(第一,第二))==第二个所有第一 / 第二值。

I suggest you implement the + operator first, which should inform the Difference method - you should make sure that first + (Period.Difference(first, second)) == second for all first/second values.

开始用编写单元测试整体转换 - 开始轻松的情况下,然后转移到棘手的那些涉及闰年。我知道正常的方法是在同一时间编写一个测试,但我个人头脑风暴一群人在开始执行任何工作之前。

Start with writing a whole slew of unit tests - initially "easy" cases, then move on to tricky ones involving leap years. I know the normal approach is to write one test at a time, but I'd personally brainstorm a bunch of them before you start any implementation work.

让自己每天要正确地实现这一点。这是棘手的东西。

Allow yourself a day to implement this properly. It's tricky stuff.

请注意,我在这里省略了周 - 该值至少是很容易的,因为它总是7天。因此,给予(阳性)期间,你必须:

Note that I've omitted weeks here - that value at least is easy, because it's always 7 days. So given a (positive) period, you'd have:

int years = period.Years;
int months = period.Months;
int weeks = period.Days / 7;
int daysWithinWeek = period.Days % 7;

(我建议你避免甚至想着消极的时期 - 确保一切都是积极的,所有的时间。)

(I suggest you avoid even thinking about negative periods - make sure everything is positive, all the time.)

这篇关于如何获得两个日期之间的差异在年/月/周/日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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