C# - Get Current and Last Month Start and End Date

visibility 610 event 2022-01-15 access_time 9 months ago language English
more_vert

This code snippet provides example about calculating current or last month's start and end date using C# in .NET. In time related calculations or data analytics, it is often required. 

Code snippet

var today = DateTime.Today;
var monthStart = new DateTime(today.Year, today.Month, 1);
var monthEnd = monthStart.AddMonths(1).AddDays(-1);
var lastMonthStart = monthStart.AddMonths(-1);
var lastMonthEnd = monthStart.AddDays(-1); Console.WriteLine("Current month start date is: {0}, end date is: {1}", monthStart, monthEnd); Console.WriteLine("Last month start date is: {0}, end date is: {1}", lastMonthStart, lastMonthEnd);

You can apply similar approaches to other programming languages. 

copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts