如何在c#中计算两个日期之间的月差?
c#中是否有相当于VB的DateDiff()方法。我需要找出相隔数年的两个日期之间的月差。文档说我可以像这样使用TimeSpan:
TimeSpan ts = date1 - date2;
但这里的数据是以天为单位的。我不想把这个数字除以30,因为不是每个月都是30天,而且两个操作数的值相差很大,所以我担心除以30可能会得到错误的值。
有什么建议吗?
如何在c#中计算两个日期之间的月差?
c#中是否有相当于VB的DateDiff()方法。我需要找出相隔数年的两个日期之间的月差。文档说我可以像这样使用TimeSpan:
TimeSpan ts = date1 - date2;
但这里的数据是以天为单位的。我不想把这个数字除以30,因为不是每个月都是30天,而且两个操作数的值相差很大,所以我担心除以30可能会得到错误的值。
有什么建议吗?
当前回答
下面是一个使用VB的更简洁的解决方案。只适用于年、月、日的净日期。你也可以在c#中加载DateDiff库。
Date1必须<= date2
VB。网
Dim date1 = Now.AddDays(-2000)
Dim date2 = Now
Dim diffYears = DateDiff(DateInterval.Year, date1, date2) - If(date1.DayOfYear > date2.DayOfYear, 1, 0)
Dim diffMonths = DateDiff(DateInterval.Month, date1, date2) - diffYears * 12 - If(date1.Day > date2.Day, 1, 0)
Dim diffDays = If(date2.Day >= date1.Day, date2.Day - date1.Day, date2.Day + (Date.DaysInMonth(date1.Year, date1.Month) - date1.Day))
C#
DateTime date1 = Now.AddDays(-2000);
DateTime date2 = Now;
int diffYears = DateDiff(DateInterval.Year, date1, date2) - date1.DayOfYear > date2.DayOfYear ? 1 : 0;
int diffMonths = DateDiff(DateInterval.Month, date1, date2) - diffYears * 12 - date1.Day > date2.Day ? 1 : 0;
int diffDays = date2.Day >= date1.Day ? date2.Day - date1.Day : date2.Day + (System.DateTime.DaysInMonth(date1.Year, date1.Month) - date1.Day);
其他回答
使用野田时间:
LocalDate start = new LocalDate(2013, 1, 5);
LocalDate end = new LocalDate(2014, 6, 1);
Period period = Period.Between(start, end, PeriodUnits.Months);
Console.WriteLine(period.Months); // 16
(例子)
简单的修复。工作的100%
var exactmonth = (date1.Year - date2.Year) * 12 + date1.Month -
date2.Month + (date1.Day >= date2.Day ? 0 : -1);
Console.WriteLine(exactmonth);
我的问题用这个方法解决了:
static void Main(string[] args)
{
var date1 = new DateTime(2018, 12, 05);
var date2 = new DateTime(2019, 03, 01);
int CountNumberOfMonths() => (date2.Month - date1.Month) + 12 * (date2.Year - date1.Year);
var numberOfMonths = CountNumberOfMonths();
Console.WriteLine("Number of months between {0} and {1}: {2} months.", date1.ToString(), date2.ToString(), numberOfMonths.ToString());
Console.ReadKey();
//
// *** Console Output:
// Number of months between 05/12/2018 00:00:00 and 01/03/2019 00:00:00: 3 months.
//
}
我只是需要一些简单的东西来满足例如,只输入月份/年的就业日期,所以希望工作的年份和月份不同。这就是我所使用的,只是为了实用
public static YearsMonths YearMonthDiff(DateTime startDate, DateTime endDate) {
int monthDiff = ((endDate.Year * 12) + endDate.Month) - ((startDate.Year * 12) + startDate.Month) + 1;
int years = (int)Math.Floor((decimal) (monthDiff / 12));
int months = monthDiff % 12;
return new YearsMonths {
TotalMonths = monthDiff,
Years = years,
Months = months
};
}
net小提琴
似乎DateTimeSpan解决方案使许多人满意。我不知道。让我们考虑一下:
BeginDate = 1972/2/29销售= 1972/4/28。
基于DateTimeSpan的答案是:
1年(s), 2个月(s)和0天(s)
我实现了一个方法,在此基础上,答案是:
1年、1个月及28天
显然没有两个月的时间。我想说的是,因为我们在开始日期的月末,剩下的实际上是整个3月加上结束日期(4月)的月份所经过的天数,所以1个月零28天。
如果你读到这里,你有兴趣,我把方法贴在下面。我在评论中解释了我所做的假设,因为有多少个月,月份的概念是一个不断变化的目标。多次测试,看看答案是否有意义。我通常选择相邻年份的考试日期,一旦我确认了答案,我就会前后移动一两天。到目前为止,它看起来不错,我相信你会发现一些bug:D。代码可能看起来有点粗糙,但我希望它足够清楚:
static void Main(string[] args) {
DateTime EndDate = new DateTime(1973, 4, 28);
DateTime BeginDate = new DateTime(1972, 2, 29);
int years, months, days;
GetYearsMonthsDays(EndDate, BeginDate, out years, out months, out days);
Console.WriteLine($"{years} year(s), {months} month(s) and {days} day(s)");
}
/// <summary>
/// Calculates how many years, months and days are between two dates.
/// </summary>
/// <remarks>
/// The fundamental idea here is that most of the time all of us agree
/// that a month has passed today since the same day of the previous month.
/// A particular case is when both days are the last days of their respective months
/// when again we can say one month has passed.
/// In the following cases the idea of a month is a moving target.
/// - When only the beginning date is the last day of the month then we're left just with
/// a number of days from the next month equal to the day of the month that end date represent
/// - When only the end date is the last day of its respective month we clearly have a
/// whole month plus a few days after the the day of the beginning date until the end of its
/// respective months
/// In all the other cases we'll check
/// - beginingDay > endDay -> less then a month just daysToEndofBeginingMonth + dayofTheEndMonth
/// - beginingDay < endDay -> full month + (endDay - beginingDay)
/// - beginingDay == endDay -> one full month 0 days
///
/// </remarks>
///
private static void GetYearsMonthsDays(DateTime EndDate, DateTime BeginDate, out int years, out int months, out int days ) {
var beginMonthDays = DateTime.DaysInMonth(BeginDate.Year, BeginDate.Month);
var endMonthDays = DateTime.DaysInMonth(EndDate.Year, EndDate.Month);
// get the full years
years = EndDate.Year - BeginDate.Year - 1;
// how many full months in the first year
var firstYearMonths = 12 - BeginDate.Month;
// how many full months in the last year
var endYearMonths = EndDate.Month - 1;
// full months
months = firstYearMonths + endYearMonths;
days = 0;
// Particular end of month cases
if(beginMonthDays == BeginDate.Day && endMonthDays == EndDate.Day) {
months++;
}
else if(beginMonthDays == BeginDate.Day) {
days += EndDate.Day;
}
else if(endMonthDays == EndDate.Day) {
days += beginMonthDays - BeginDate.Day;
}
// For all the other cases
else if(EndDate.Day > BeginDate.Day) {
months++;
days += EndDate.Day - BeginDate.Day;
}
else if(EndDate.Day < BeginDate.Day) {
days += beginMonthDays - BeginDate.Day;
days += EndDate.Day;
}
else {
months++;
}
if(months >= 12) {
years++;
months = months - 12;
}
}