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

visibility 76 access_time 5 months ago languageEnglish

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

timeline Stats
Page index 0.60
More from Kontext
Send Emails using Microsoft 365 in ASP.NET Core
visibility 9,928
thumb_up 0
access_time 2 years ago
C# 10.0 New Features
visibility 189
thumb_up 0
access_time 10 months ago
C# 10.0 New Features
.NET Standard for C# 9.0
visibility 2,939
thumb_up 0
access_time 2 years ago