Last active
January 18, 2017 08:45
-
-
Save smdpro/6a5e9452f6cec670edbf4a7a1c94fcf4 to your computer and use it in GitHub Desktop.
Get start and end dates of a week in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static DateTime[] GetStartAndEndDatesOfCurrentWeek(int weekNum = 0) | |
{ | |
var currentDate = DateTime.Now.Date.AddDays(weekNum * 7); | |
var dayOfWeekForcurrentDate = (int)currentDate.DayOfWeek; | |
var startDateOfWeek = currentDate.Date.AddDays(dayOfWeekForcurrentDate - 6); | |
var endDateOfWeek = currentDate.Date.AddDays(6 - dayOfWeekForcurrentDate); | |
return new[] { startDateOfWeek.Date , endDateOfWeek.Date }; | |
} | |
} | |
// how to call method | |
// for current week | |
var daysArray = GetStartAndEndDatesOfCurrentWeek(); | |
//for next week | |
var daysArray = GetStartAndEndDatesOfCurrentWeek(1); | |
//for last week | |
var daysArray = GetStartAndEndDatesOfCurrentWeek(-1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment