Picking Up Current Week And Last Week Dates In C#

In this article you will learn how to pick up the current week and the last week's dates in C#.

Introduction

This article is about picking up dates in the current week and the previous week.

Tools

Visual Studio (Any Version)

It’s been an issue for many programmers to pick up dates in the current week to get the required data from the database, or to put checks on the basis of those dates. Let’s see how we can pick up these current week and last week dates into a list using some simple steps.

Step 1

Create a table in SQL having all dates for the current year, which will be used to pick up dates and check the dates. Currently I am using a table which names ‘hrCalendarDays’. It has all dates for 2019. You can make your own table using SQL query.

Step 2

We will use a built in namespace for this purpose which is “DayOfWeek”. It includes a sequence of days in a week starting from Sunday as a ‘0’ and finishing on Saturday which is ‘6’. The picture below shows the enum DayOfWeek.

Picking Up Current Week And Last Week Dates In C# 

Let’s see how to use it. Simply type this key word ‘DayOfWeek’ and Visual Studio will automatically pick it up.

C# Code
  1. DayOfWeek currentDay = DateTime.Now.DayOfWeek;  
  2.         int daysTillCurrentDay = currentDay - DayOfWeek.Monday;  
  3. DateTime currentWeekStartDate = DateTime.Now.AddDays(-daysTillCurrentDay);  

This will give you the current week’s start date and let see how to pick up whole dates in the next step.

Step 3

Create an object of your table and select the index of your start date in that table. You have to compare the date column in the table with your current date. You can use ‘IndexOf’ for this purpose. After that you can use ‘Get Range’ specifying the range from start date to the next seven days, which will give you all 7 dates in the current week. My Current Date is 02/04/2019 dd/MM/yyyy. So, the result will be something like this,

Picking Up Current Week And Last Week Dates In C# 
 
C# Code
  1. var calendar = db.hrCalendarDays.ToList();  
  2. var indexOfCurrentWeekStartDate = calendar.FindIndex(p => p.CalendarDate == currentWeekStartDate.Date);  
  3. var currentWeekDates = calendar.GetRange(indexOfCurrentWeekStartDate, 7).Select(p => new { p.CalendarDate }).ToList();  

This will give you all 7 dates in the current week. Now let’s see how to get last week’s dates.

Step 4

The Code is almost the same for the last week’s dates. Just for the last week’s start date additionally you have to subtract 7 more days to go back to last week. In this case the output will be something like this.

Picking Up Current Week And Last Week Dates In C# 
 
C# Code
  1. DateTime lastWeekStartDate = DateTime.Now.AddDays(-daysTillCurrentDay - 7);  
  2. var indexOflastWeekStartDate = calendar.FindIndex(p => p.CalendarDate == lastWeekStartDate.Date);  
  3. var lasttWeekDates = calendar.GetRange(indexOflastWeekStartDate, 7).Select(p => new { p.CalendarDate }).ToList();   

Now you have two lists, one is for the current week’s all dates and the second is for the last week’s dates. This article tells you how to get them in simple ways. I hope now you have more knowledge of how to do this.