I got two dates, how do I check if both dates does not exceed over one year?
我有两个约会,我如何检查这两个日期是否超过一年?
Short question! :-)
简短的问题! :-)
8 个解决方案
#1
if (Math.Abs((d1 - d2).TotalDays) < 365)
Edit: This should account for leap years better.
编辑:这应该更好地解释闰年。
if ( d1 <= d2 && d1.AddYears(1) >= d2 || d2 < d1 && d2.AddYears(1) > d1)
Update:
I like @JDunkerley's solution better:
更新:我更喜欢@JDunkerley的解决方案:
if (d1 < d2 ? d2 < d1.AddYears(1) : d1 < d2.AddYears(1))
#2
I give you a little example:
我举个例子:
DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddSeconds( 75 );
TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );
#3
TimeSpan ts = Date1.Subtract(Date2);
if(ts.Days > 365)
{
// Over a year.
}
#4
That depends on the date format.
这取决于日期格式。
- If you have two timestamps, you may calculate the difference between them.
- If you have two specific dates in a known format, you simply compare the year attributes as strings.
如果您有两个时间戳,则可以计算它们之间的差异。
如果您有一个已知格式的两个特定日期,则只需将年份属性作为字符串进行比较。
#5
If they're both in DateTime structures, then you can just subtract the two to get a Timespan structure. The Timespan structure has a Days property which you can look at.
如果它们都在DateTime结构中,那么您可以减去这两个以获得Timespan结构。 Timespan结构有一个Days属性,您可以查看。
So you'll have something like:
所以你会有类似的东西:
if(Math.Abs((date1 - date2).Days) <= 365) ...
#6
If necessary, swap t1 and t2 so that t1 <= t2
如有必要,交换t1和t2,使t1 <= t2
if(t1.AddYears(1) >= t2) {
// t1 is within a year of t2
return true;
} else {
// t1 is not within a year of t2
return false;
}
#7
This is the same question as "how do I calculate someone's age".
这与“如何计算某人的年龄”相同。
Blatantly stealing the answer from there and modifying it for use:
公然从那里窃取答案并修改它以供使用:
public static bool DatesAreWithinOneYear(DateTime date1, DateTime date2)
{
DateTime startDate = date2 > date1 ? date1 : date2;
DateTime endDate = date2 > date1 ? date2 : date1;
int years = endDate.Year - startDate.Year;
if (endDate < startDate.AddYears(years))
{
years--;
}
return years < 1;
}
#8
if (year(date1) == year(date2))
{
//true
}
else
{
//false
}
#1
if (Math.Abs((d1 - d2).TotalDays) < 365)
Edit: This should account for leap years better.
编辑:这应该更好地解释闰年。
if ( d1 <= d2 && d1.AddYears(1) >= d2 || d2 < d1 && d2.AddYears(1) > d1)
Update:
I like @JDunkerley's solution better:
更新:我更喜欢@JDunkerley的解决方案:
if (d1 < d2 ? d2 < d1.AddYears(1) : d1 < d2.AddYears(1))
#2
I give you a little example:
我举个例子:
DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddSeconds( 75 );
TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );
#3
TimeSpan ts = Date1.Subtract(Date2);
if(ts.Days > 365)
{
// Over a year.
}
#4
That depends on the date format.
这取决于日期格式。
- If you have two timestamps, you may calculate the difference between them.
- If you have two specific dates in a known format, you simply compare the year attributes as strings.
如果您有两个时间戳,则可以计算它们之间的差异。
如果您有一个已知格式的两个特定日期,则只需将年份属性作为字符串进行比较。
#5
If they're both in DateTime structures, then you can just subtract the two to get a Timespan structure. The Timespan structure has a Days property which you can look at.
如果它们都在DateTime结构中,那么您可以减去这两个以获得Timespan结构。 Timespan结构有一个Days属性,您可以查看。
So you'll have something like:
所以你会有类似的东西:
if(Math.Abs((date1 - date2).Days) <= 365) ...
#6
If necessary, swap t1 and t2 so that t1 <= t2
如有必要,交换t1和t2,使t1 <= t2
if(t1.AddYears(1) >= t2) {
// t1 is within a year of t2
return true;
} else {
// t1 is not within a year of t2
return false;
}
#7
This is the same question as "how do I calculate someone's age".
这与“如何计算某人的年龄”相同。
Blatantly stealing the answer from there and modifying it for use:
公然从那里窃取答案并修改它以供使用:
public static bool DatesAreWithinOneYear(DateTime date1, DateTime date2)
{
DateTime startDate = date2 > date1 ? date1 : date2;
DateTime endDate = date2 > date1 ? date2 : date1;
int years = endDate.Year - startDate.Year;
if (endDate < startDate.AddYears(years))
{
years--;
}
return years < 1;
}
#8
if (year(date1) == year(date2))
{
//true
}
else
{
//false
}