0
我有形式的两个日期:日期年月计算
Start Date: 2007-03-24
End Date: 2009-06-26
现在我需要找到以下形式这两者之间的区别:
2 years, 3 months and 2 days
我该怎么做在C#窗体的形式?
我有形式的两个日期:日期年月计算
Start Date: 2007-03-24
End Date: 2009-06-26
现在我需要找到以下形式这两者之间的区别:
2 years, 3 months and 2 days
我该怎么做在C#窗体的形式?
您需要使用TimeSpan
获得差异..
class Program
{
static void Main(string[] args)
{
string StartDate = "2007-03-24";
string EndDate = "2009-06-26";
System.DateTime firstDate = DateTime.ParseExact(StartDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
System.DateTime secondDate = DateTime.ParseExact(EndDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
System.TimeSpan diff = secondDate.Subtract(firstDate);
var totalDays = (diff).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);
Console.ReadLine();
}
}
.NET已经提供了时间跨度类显示时间跨度类节目年,月,日的时间间隔seperately
的两个日期时间value.some性能之间的差异