C# 如何将天数转换为年、月和日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19632069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:28:08  来源:igfitidea点击:

How to convert number of days to years,months and days

c#asp.net.netdatedatetime

提问by Anyname Donotcare

If i have Two dates then i get the difference between them in days Like this Post.

如果我有两个日期,那么我会像这篇文章一样得到它们之间的天数差异。



How to detail that in the following view :

如何在以下视图中详细说明:

convert the days to (number of years,number of months and the rest in the number of days)

将天数转换为 ( number of years,number of months and the rest in the number of days)

采纳答案by James

There is no out-of-the-box solution for this. The problem is the data isn't "fixed" e.g. not all years are 365 days (366 on a leap year) and not every month can be assumed to be standard 30 days.

对此没有开箱即用的解决方案。问题是数据不是“固定的”,例如并非所有年份都是 365 天(闰年为 366 天),并且并非每个月都可以假设为标准的 30 天。

It's very difficult to calculate this sort of information without context. You have a duration in days, however, to accurately calculate the number of years/months you need to know exactly when these days fall i.e. on what month and of what year - this would allow you to determine the exact days in the month and/or year.

在没有上下文的情况下计算这种信息是非常困难的。但是,您有一个以天为单位的持续时间,以准确计算您需要确切知道这些日子何时落下的年数/月数,即哪一年是哪一年-这将使您能够确定该月和/中的确切天数或年。



Based on your comments and the following conditions

根据您的意见和以下条件

  • 1 year = 365 days
  • 1 month = 30 days
  • 1 年 = 365 天
  • 1 个月 = 30 天

Then the following code would do the job

然后下面的代码将完成这项工作

DateTime startDate = new DateTime(2010, 1, 1);
DateTime endDate = new DateTime(2013, 1, 10);
var totalDays = (endDate - startDate).TotalDays;
var totalYears = Math.Truncate(totalDays / 365);
var totalMonths = Math.Truncate((totalDays % 365) / 30);
var remainingDays = Math.Truncate((totalDays % 365) % 30);
Console.WriteLine("Estimated duration is {0} year(s), {1} month(s) and {2} day(s)", totalYears, totalMonths, remainingDays);

回答by Czeshirecat

You can't because it depends on the start date i.e. 30 days may be 1 month 1 day, or 1 month 2 days, or less than a month or 365 days will be less than a year if it's a leap year

你不能,因为它取决于开始日期,即 30 天可能是 1 个月 1 天,或 1 个月 2 天,或者少于一个月或 365 天将少于一年,如果是闰年

回答by tomsullivan1989

As mentioned in previous answers, it is very difficult to work this out from just a number of days. There is problems with leap years, and the number of days in months. If you start with the original two datetimes you can use code similar to the following:

正如之前的答案中提到的,仅仅几天就很难解决这个问题。闰年和月中的天数存在问题。如果您从最初的两个日期时间开始,您可以使用类似于以下内容的代码:

DateTime date1 = new DateTime(2010, 1, 18);
DateTime date2 = new DateTime(2013, 2, 22);

int oldMonth = date2.Month;
while (oldMonth == date2.Month)
{
     date1 = date1.AddDays(-1);
     date2 = date2.AddDays(-1);
}       

int years = 0, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0;

// getting number of years
while (date2.CompareTo(date1) >= 0)
{
     years++;
     date2 = date2.AddYears(-1);
}
date2 = date2.AddYears(1);
years--;


// getting number of months and days
oldMonth = date2.Month;
while (date2.CompareTo(date1) >= 0)
{
     days++;
     date2 = date2.AddDays(-1);
     if ((date2.CompareTo(date1) >= 0) && (oldMonth != date2.Month))
     {
          months++;
          days = 0;
          oldMonth = date2.Month;
     }
}
date2 = date2.AddDays(1);
days--;

TimeSpan difference = date2.Subtract(date1);

Console.WriteLine("Difference: " +
                    years.ToString() + " year(s)" +
                    ", " + months.ToString() + " month(s)" +
                    ", " + days.ToString() + " day(s)");

Output is:Difference: 3 year(s), 1 month(s), 4 day(s)

输出是:Difference: 3 year(s), 1 month(s), 4 day(s)