Raymond Raymond

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

event 2022-01-15 visibility 10,486 comment 3 insights toc
more_vert
insights Stats
toc Table of contents

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. 

More from Kontext
comment Comments
Raymond Raymond #1789 access_time 2 years ago more_vert

Thanks for sharing AJ and welcome to Kontext. If you want to be even more accurate, you can also consider milliseconds as there might be possibilities that some purchases will land on the last second.

format_quote

person AJ access_time 2 years ago

Thank you Raymond, it was very helpful for me! To anyone that needs the very last instance of the end date, just edit monthEnd and lastMonthEnd to:
var monthEnd = monthStart.AddMonths(1).AddSeconds(-1);
var lastMonthEnd = monthStart.AddSeconds(-1);

This will output for example (it's January 2023)
monthEnd = 1/31/2023 11:59:59 PM
lastMonthEnd = 12/31/2022 11:59:59 PM

This works in my shop in the case let's say I registered a purchase on 12/31/2022 10:05 AM, the LastMonthEnd will originally output the last day of the month but just at 12/31/2022 12:00:00 AM, so when you look up for records of December 2022 that purchase won't count.

If not expert with queries (like me) we would normally search between two dates with '<=' and '>=' operators and this won't count that purchase unless using the edit above, for example I used LINQ to count my purchases of the current month in a filled DataTable:

int currMonthOrders = dataSet1.myOrders.AsEnumerable().Where(row => row.Field<DateTime>("yourDateColumn") >= monthStart && row.Field<DateTime>("yourDateColumn") <= monthEnd).Count();

label1.Text = "You have done " +currMonthOrders.ToString("N0")+ " purchases this month";
A AJ #1788 access_time 2 years ago more_vert

the best way to avoid this if by using the Month function of sql (you can query using SqlDataAdapter or SqlCommand at your like) 

SELECT  *  FROM  myOrders WHERE MONTH(date_column) = 12 AND YEAR(date_column) = 2022; 

This will be more accurate and give you all records of December 2022 no matter the time ;)

A AJ #1787 access_time 2 years ago more_vert
Thank you Raymond, it was very helpful for me! To anyone that needs the very last instance of the end date, just edit monthEnd and lastMonthEnd to:
var monthEnd = monthStart.AddMonths(1).AddSeconds(-1);
var lastMonthEnd = monthStart.AddSeconds(-1);

This will output for example (it's January 2023)
monthEnd = 1/31/2023 11:59:59 PM
lastMonthEnd = 12/31/2022 11:59:59 PM

This works in my shop in the case let's say I registered a purchase on 12/31/2022 10:05 AM, the LastMonthEnd will originally output the last day of the month but just at 12/31/2022 12:00:00 AM, so when you look up for records of December 2022 that purchase won't count.

If not expert with queries (like me) we would normally search between two dates with '<=' and '>=' operators and this won't count that purchase unless using the edit above, for example I used LINQ to count my purchases of the current month in a filled DataTable:

int currMonthOrders = dataSet1.myOrders.AsEnumerable().Where(row => row.Field<DateTime>("yourDateColumn") >= monthStart && row.Field<DateTime>("yourDateColumn") <= monthEnd).Count();

label1.Text = "You have done " +currMonthOrders.ToString("N0")+ " purchases this month";

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts