计算年、月、周和日
在我的应用程序中,用户输入两个日期。预定的开始日期和预定的结束日期。我们必须获取这些日期,并根据差异填充 4 个字段。
因此,假设他选择 2010 年 1 月 1 日作为开始,2011 年 3 月 2 日作为结束,我们需要最终得到:
年:1 月数:2 周数:0 第 1 天
表示总持续时间为 1 年 2 个月 1 天。
有这样做的标准方法吗?或者我是否需要编写一个具有很多非常棘手的逻辑的方法来解决这个问题?我希望我很幸运,并且会有一个日期差异类型的 .Net 类可用。
In my application, a user enters two dates. A scheduled start date, and a scheduled end date. We have to take those dates, and populate 4 fields, based on the difference.
So, lets say he selects 1st Jan, 2010 as a start, and 2nd of March, 2011 as the end, we need to end up with:
Years: 1
Months: 2
Weeks: 0
Days 1
Meaning the total duration is 1 year, 2 months and 1 day.
Is there a standard way of doing this? Or would I need to write a method that has a lot of pretty tricky logic to work it out? I was hoping I'd be lucky, and there would be a date-diff type .Net class available.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是一个完整的方法,不包括周数,但可以相对简单地添加。这是一个有点复杂的问题(在 stackoverflow 上以多种方式提出,并且以多种方式回答得很差),但仍然可以回答。 TimeSpan 对象为我们提供了部分所需内容,但只能在几天内发挥作用。我已经针对此方法编写了大量测试,如果您发现漏洞,请发表评论。
其作用是比较 2 个日期,获取年、月、日、小时和分钟。 (例如,某事件发生在 1 年、6 个月、3 天、4 小时 7 分钟前)
因为这个问题已被提出并试图回答很多次,所以我不确定这是否会被注意到,但如果所以它应该提供价值。
Heres a complete method, weeks are not included, but could be added relatively simply. It's a somewhat complex question (asked in a multitude of ways on stackoverflow and answered poorly in a multitude of ways), but none the less can be answered. The TimeSpan object gives us part of what we need, but only works up through days. I've written a significant number of tests against this method, if you find a hole, please post a comment.
What this will do is compare 2 dates, getting the years, months, days, hours, and minutes. (e.g. some event happened 1 year, 6 months, 3 days, 4 hours and 7 minutes ago)
Because this question has been asked and attempted to be answered so many times, I'm not sure this will ever even get noticed, but if so it should provide value.
您可以使用此免费库的DateDiff类:
You can use the DateDiff class of this free library:
我也需要这个,但就我而言,没有周部分(所以只有年、月和日)。鉴于此,这就是我所做的:
如果您想更动态地显示这一点,您还可以使用以下代码:
I also needed this but, in my case, without the weeks part (so only years, months and days). Given that, here's what I made:
If you want to show this more dynamically, you can also use this code:
这应该可以做到。关键是如果给定的 2 个日期之间有奇数个闰日,则将天数减少 1。
已运行这些测试:
这些是打印输出:
日期 1/1/2010 12:00:00 AM 和日期 4/9/2012 12:00:00 AM 之间的差异是:
2 年 3 个月 9 天。
日期 1/1/2010 12:00:00 AM 和日期 4/9/2012 12:00:00 AM 之间的差异是:
2 年、3 个月、1 周和 2 天。
日期 1/1/2010 12:00:00 AM 和日期 2/9/2020 12:00:00 AM 之间的差异是:
10 年 1 个月 9 天。
日期 1/1/2010 12:00:00 AM 和日期 2/9/2020 12:00:00 AM 之间的差异是:
10 年、1 个月、1 周和 2 天。
日期 1/1/2010 12:00:00 AM 和日期 4/9/2020 12:00:00 AM 之间的差异是:
10 年 3 个月 9 天。
日期 1/1/2010 12:00:00 AM 和日期 4/9/2020 12:00:00 AM 之间的差异是:
10 年、3 个月、1 周和 2 天。
日期 2/29/2020 12:00:00 AM 和日期 2/28/2021 12:00:00 AM 之间的差异是:
1 年、0 个月、0 天。
日期 2/29/2020 12:00:00 AM 和日期 2/28/2021 12:00:00 AM 之间的差异是:
1 年、0 个月、0 周和 0 天。
日期 2/28/2019 12:00:00 AM 和日期 2/28/2021 12:00:00 AM 之间的差异是:
2 年 0 个月 1 天。
日期 2/28/2019 12:00:00 AM 和日期 2/28/2021 12:00:00 AM 之间的差异是:
2 年、0 个月、0 周和 1 天。
This should do it. The key is to reduce days by 1 if there are odd number of leap days in between the given 2 dates.
Had run these tests:
These are the printouts:
The difference between date 1/1/2010 12:00:00 AM and date 4/9/2012 12:00:00 AM is:
2 year(s), 3 month(s), and 9 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 4/9/2012 12:00:00 AM is:
2 year(s), 3 month(s), 1 week(s) and 2 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 2/9/2020 12:00:00 AM is:
10 year(s), 1 month(s), and 9 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 2/9/2020 12:00:00 AM is:
10 year(s), 1 month(s), 1 week(s) and 2 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 4/9/2020 12:00:00 AM is:
10 year(s), 3 month(s), and 9 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 4/9/2020 12:00:00 AM is:
10 year(s), 3 month(s), 1 week(s) and 2 day(s).
The difference between date 2/29/2020 12:00:00 AM and date 2/28/2021 12:00:00 AM is:
1 year(s), 0 month(s), and 0 day(s).
The difference between date 2/29/2020 12:00:00 AM and date 2/28/2021 12:00:00 AM is:
1 year(s), 0 month(s), 0 week(s) and 0 day(s).
The difference between date 2/28/2019 12:00:00 AM and date 2/28/2021 12:00:00 AM is:
2 year(s), 0 month(s), and 1 day(s).
The difference between date 2/28/2019 12:00:00 AM and date 2/28/2021 12:00:00 AM is:
2 year(s), 0 month(s), 0 week(s) and 1 day(s).
我认为 TimeSpan 是您正在寻找的,但它不做几年或几个月,因为它们的长度不同。
下面的例子来自上面的链接;
I think TimeSpan is what you are looking for, but it does not do years or months because those vary in length.
The below example is from the above link;
我创建这个是为了返回两个日期之间的年、月和日差异。
I had created this one for returning difference in years,month and days between 2 dates.
研究 C# TimeSpan 结构。它不会持续几个月或几年,但确实会持续几天。这让事情变得更容易。
Investigate the C# TimeSpan structure. It doesn't do months or years, but it does do days. That makes things easier.
如果您首先计算总月数和总天数,则逻辑并不太复杂。然后很容易用除法和取模进行一些整数数学运算。
这将按预期给出
Years: 1 Months: 2 Weeks: 0 Days: 1
。The logic isn't overly complicated if you first compute the total months and the total days. Then it's easy to do a bit of integer maths with divide and mod.
That gives
Years: 1 Months: 2 Weeks: 0 Days: 1
as expected.这是我刚刚构建的转换器。我没有尝试上面所有的解决方案,但我尝试过的所有解决方案在某种程度上仍然略有偏差。这会考虑年、闰年、月、周、日等,然后获取去年 YM 或 MW 或 WD 或 DH 的两个最有用的值。这很简单,但我的项目需要它,也许对以后的人有帮助。当然,如果由于某种原因这不好,我相信你们都会让我知道。
Heres a converter I just built. I didn't try all of the solutions above but all the ones I did try were still slightly off in one way or another. This takes into account Years, Leap Years, Months, Weeks, Days etc and then grabs the two most useful values last YM or MW or WD or DH. It's simple but I needed it for my project maybe could be of help to someone down the road. And then of course if this is not good for one reason or another I'm sure you all will let me know.
这是一个相当老的问题,但仍然是时候尝试一下。
重要的是要考虑到月份有不同的天数。
It's quite an old question, but still time to have a go.
Important to consider that months have differing number of days.