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

visibility 76 access_time 5 months ago languageEnglish timeline Stats

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.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

More from Kontext
Apache Ignite Installation on Windows
visibility 177
thumb_up 0
access_time 5 months ago
Draw Images in ASP.NET Core 5
visibility 2,123
thumb_up 0
access_time 2 years ago
Draw Images in ASP.NET Core 5
Get IP Address in ASP.NET Core 3.x
visibility 20,327
thumb_up 3
access_time 2 years ago