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

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

问题描述

如何有效地获取年/月/周/日中两个日期之间的差异?

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.

差异表示两个日期之间的年、月、周和日计数.

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:

  • 2008 年 6 月 19 日至 2010 年 6 月 19 日 = 2 年,但也是 365 * 2 天

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

2006 年 6 月 19 日至 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.

不幸的是,我没有足够的时间来完整地回答这个问题.我建议您首先定义一个表示 Period 的结构体:

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!
    }
}

我建议你首先实现 + 运算符,它应该通知 Difference 方法 - 你应该确保 first + (Period.Difference(first, second)) == second 用于所有 first/second 值.

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天全站免登陆