计算年,月,周,日 [英] Calculate Years, Months, weeks and Days

查看:148
本文介绍了计算年,月,周,日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,用户输入两个日期。一个计划的开始日期和计划的结束日期。我们必须采取这些日期,并填充4个字段,基于所述差

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.

因此​​,可以说,他选择2010年1月1日作为一个开始,2011年3月第2个作为结束,我们要结束了:

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:

十年:1 月:2 周:0 天1

Years: 1 Months: 2 Weeks: 0 Days 1

含义总工期为1年,2个月零1天。

Meaning the total duration is 1 year, 2 months and 1 day.

是否有这样做的标准方式?或者,我需要写,有很多pretty的棘手的逻辑来解决它的方法?我希望我是幸运的,而且会有可用日期差异型.NET类。

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.

推荐答案

我觉得时间跨度是你在找什么,但它不会做几年或几个月,因为这些长短不一。

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;

// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);
// Calculate the interval between the two dates.
TimeSpan interval = date2 - date1;
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString());
// Display individual properties of the resulting TimeSpan object.
Console.WriteLine("   {0,-35} {1,20}", "Value of Days Component:", interval.Days);
Console.WriteLine("   {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays);
Console.WriteLine("   {0,-35} {1,20}", "Value of Hours Component:", interval.Hours);
Console.WriteLine("   {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours);
Console.WriteLine("   {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes);
Console.WriteLine("   {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes);
Console.WriteLine("   {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds);
Console.WriteLine("   {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds);
Console.WriteLine("   {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds);
Console.WriteLine("   {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds);
Console.WriteLine("   {0,-35} {1,20:N0}", "Ticks:", interval.Ticks);
// the example displays the following output:
//       8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
//          Value of Days Component:                             229
//          Total Number of Days:                   229.229340277778
//          Value of Hours Component:                              5
//          Total Number of Hours:                  5501.50416666667
//          Value of Minutes Component:                           30
//          Total Number of Minutes:                       330090.25
//          Value of Seconds Component:                           15
//          Total Number of Seconds:                      19,805,415
//          Value of Milliseconds Component:                       0
//          Total Number of Milliseconds:             19,805,415,000
//          Ticks:                               198,054,150,000,000

这篇关于计算年,月,周,日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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