Get the previous month's first and last day dates in c
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Finding the first and last day of the previous month is a common reporting task in C#. The cleanest solution is to anchor on the first day of the current month, then step backward in a way that avoids hard-coded month lengths.
A reliable DateTime approach
The easiest pattern is:
- build the first day of the current month
- subtract one day to get the last day of the previous month
- build the first day of that previous month
This works for January as well. If today is in January, subtracting one day moves into December of the previous year automatically.
Wrap the logic in a reusable method
If you need the same calculation in reports, billing jobs, or API filters, move it into a method that returns both dates.
Passing a reference date instead of calling DateTime.Today inside the method makes the code easier to test and reuse.
An alternative using AddMonths
You can also start from the first day of the current month and step back exactly one month.
This version is compact and still safe because it never tries to guess how many days were in the previous month.
Include time boundaries when needed
Sometimes a report needs a full timestamp range rather than just dates. In that case, define clearly whether the end value is inclusive or exclusive.
An inclusive end:
A safer query boundary for databases is often an exclusive end:
The exclusive-end pattern avoids precision problems and usually maps better to SQL predicates such as created_at >= start AND created_at < exclusiveEnd.
Prefer DateTimeOffset when timezone matters
If the range is used in a distributed system, think about timezone semantics early. DateTime.Today depends on the local machine timezone. For local desktop software that may be fine, but services often need an explicit offset or a UTC-based strategy.
This is especially important when month boundaries drive billing or compliance logic.
Common Pitfalls
The biggest mistake is trying to compute the last day of the previous month by hard-coding month lengths or writing special cases for leap years. DateTime already handles those transitions correctly, so manual logic only creates bugs.
Another issue is mixing dates and datetimes without defining the boundary semantics. If your query says "up to the end of the previous month," decide whether that means midnight at the start of the last day, the final representable tick of that day, or an exclusive boundary at the next month.
Developers also run into timezone problems when a server in UTC and a user in a local timezone both talk about "previous month." The answer may differ near midnight on the first day of a month. Use a clear reference timezone if the result matters across systems.
Finally, avoid hiding the reference date inside the helper unless you truly want the current machine date every time. Explicit input makes tests deterministic and business rules easier to audit.
Summary
- Build from the first day of the current month, then step backward.
- '
AddDays(-1)andAddMonths(-1)handle year and month boundaries safely.' - Use a reusable helper that accepts a reference date for easier testing.
- Define whether the end of the range is inclusive or exclusive.
- Prefer explicit timezone handling when the date range is used across systems.
Related reading
- Get the sum of powers of 2 for a given number c
- Get Timezone Offset of Server in C
- Get URL parameters from a string in .NET
- GetFiles with multiple extensions
- GetHashCode Guidelines in C
- GetPathsOfAllDirectoriesAbove cannot be evaluated after updating .Net Framework version 4.6.2 to 4.7.2
- GetProperties to return all properties for an interface inheritance hierarchy
- Getter and Setter declaration in .NET

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.