DateTime.Compare how to check if a date is less than 30 days old?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, checking if a date is less than 30 days old is simplest with DateTime.Now - date < TimeSpan.FromDays(30) or (DateTime.Now - date).TotalDays < 30. While DateTime.Compare exists for comparing two dates, it only tells you which is earlier — it does not compute a time span. For "less than 30 days old" checks, subtracting dates to get a TimeSpan is the direct and idiomatic approach.
Simple Approach: Subtraction
Using DateTime.Compare
DateTime.Compare returns -1, 0, or 1 — not a time span. To check "within 30 days", compute the threshold date first:
Using Comparison Operators (Preferred)
C# DateTime supports <, >, <=, >=, and == directly — no need for DateTime.Compare:
Extension Method for Reuse
Working with DateTimeOffset
For timezone-aware comparisons, use DateTimeOffset:
Common Patterns
LINQ with Date Filtering
Common Pitfalls
- Using DateTime.Now instead of DateTime.UtcNow:
DateTime.Nowreturns local time, which changes with DST. A date that is "29 days old" in local time might be "30 days old" in UTC (or vice versa) during DST transitions. UseDateTime.UtcNowfor consistent server-side comparisons. - Using DateTime.Compare when subtraction is simpler:
DateTime.Compare(a, b)only returns -1, 0, or 1 — it does not tell you how far apart the dates are. For "less than 30 days" checks,(now - date).TotalDays < 30is more readable and directly answers the question. - Using .Days instead of .TotalDays on TimeSpan:
TimeSpan.Daysis the integer day component only (ignoring hours/minutes).TimeSpan.TotalDaysincludes the fractional part. A span of 29 days and 23 hours has.Days == 29but.TotalDays == 29.96. Use.TotalDaysfor accurate "less than 30 days" checks. - Comparing DateTime and DateTimeOffset: Subtracting
DateTimefromDateTimeOffset(or vice versa) can silently produce wrong results because theDateTimeis assumed to be local or UTC depending on itsKind. Use the same type consistently, or convert explicitly. - Not handling future dates: If
postDateis in the future,DateTime.Now - postDateproduces a negativeTimeSpan. The checktotalDays < 30returns true for future dates, which may not be intended. AddtotalDays >= 0to the condition if only past dates should qualify.
Summary
- Use
(DateTime.UtcNow - date).TotalDays < 30to check if a date is less than 30 days old DateTime.Compareonly returns -1/0/1 — use it when you need ordering, not duration- Prefer comparison operators (
>,<) overDateTime.Comparefor readability - Use
DateTime.UtcNowinstead ofDateTime.Nowto avoid DST-related bugs - Use
.TotalDays(not.Days) onTimeSpanfor accurate fractional-day comparisons - Create extension methods for commonly used date checks to improve code readability
Related reading
- DateTime.Now vs. DateTime.UtcNow
- DateTime.Now.ToStringyyyy-MM-dd hhmmss is returning AM time instead of PM time?
- DateTime.Parse2012-09-30T230000.0000000Z always converts to DateTimeKind.Local
- DateTime.TryParseExact rejecting valid formats
- DbContext discard changes without disposing
- Deadly exception, how to catch? System.Net.InternalException System error
- Debug async method in VS 2013
- Debug Console window cannot accept Console.ReadLine input during debugging

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.