get nth weekday of month in C# [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I determine if a given date is Nth weekday of the month?
How do i get the nth weekday of the month?
For ex.:
2nd Monday of "July 2010" = 07/12/2010.
Looking for a function like:
public DateTime GetNthWeekofMonth(DateTime date, int nthWeek, DayOfWeek dayofWeek)
{
//return the date of nth week of month
}
from the above, the parameters of the function will be ("Any Date in July 2010", 2, Monday).

Use the following extension method:
public static class DateTimeExtensions
{
///<summary>Gets the first week day following a date.</summary>
///<param name="date">The date.</param>
///<param name="dayOfWeek">The day of week to return.</param>
///<returns>The first dayOfWeek day following date, or date if it is on dayOfWeek.</returns>
public static DateTime Next(this DateTime date, DayOfWeek dayOfWeek) {
return date.AddDays((dayOfWeek < date.DayOfWeek ? 7 : 0) + dayOfWeek - date.DayOfWeek);
}
}
You can then write
new DateTime(2010, 07, 01).Next(DayOfWeek.Monday).AddDays((2 - 1) * 7);
Or, as a function:
public DateTime GetNthWeekofMonth(DateTime date, int nthWeek, DayOfWeek dayOfWeek) {
return date.Next(dayOfWeek).AddDays((nthWeek - 1) * 7);
}
(I need to subtract one because date.Next(dayOfWeek) is already the first occurrence of that day)

One possible algorithm:
Start from the 1st of the month.
Move forward one day at a time until
you get the Day of Week you're
looking for.
Add (7 * N) to the date you're on to
get the date you want.

Duplicate can be found here: How do I determine if a given date is the Nth weekday of the month?
int d = date.Day;
return date.DayOfWeek == dow && (d-1)/7 == (n-1);

IEnumerable<DateTime> WeekdaysFrom( DateTime start )
{
DateTime weekday = start.Add( TimeSpan.FromDays(1) );
while( weekday < DateTime.MaxValue.Subtract( TimeSpan.FromDays(1) ) )
{
while( weekday.DayOfWeek == DayOfWeek.Saturday || weekday.DayOfWeek == DayOfWeek.Sunday )
{
weekday.Add( TimeSpan.FromDays(1) );
}
yield return weekday;
}
}
DateTime NthWeekday( DateTime month, int n )
{
return WeekdaysFrom( new DateTime( month.year, month.month, 1 ) ).Skip(n-1).First();
}

Related

how to get the get first Sunday of the month using date format?

Hi I need to check a condition for the first Sunday of the month for a date formatted as YYYYMMDD
var calDate = data.value; // example 20210502 is sunday
if (first Sunday of the month)
{
do this
}
else
{
do that
}
I need to check the above condition for the first Sunday of the month
Split your problem in two:
parse the string to a DateTime object
var date = DateTime.ParseExact(calDate, "yyyyMMdd", null);
Check if the DateTime object refers to the first sunday in a month. For this, it must obviously be a Sunday and the day part must be in the range 1 to 7:
var isFirstSunday = date.DayOfWeek == DayOfWeek.Sunday && date.Day <= 7;
Thsi function can return true or false if the date is first Sunday of the month or not
private static bool IsFirstSunday(DateTime date)
{
int i = 1;
while (i!=7)
{
if (date.Day==i && date.DayOfWeek == DayOfWeek.Sunday)
{
return true;
}
i++;
}
return false;
}
And use the result in your if condition
//First sunday
var date = DateTime.Parse("2021-05-02 11:27 AM");
var result = IsFirstSunday(date);
//Second sunday
date = DateTime.Parse("2021-05-09 11:27 AM");
result = IsFirstSunday(date);
//Non sunday
date = DateTime.Parse("2021-05-04 11:27 AM");
result = IsFirstSunday(date);
public static DateTime GetFirstSundayOfMonth(DateTime givenDate)
{
DateTime firstDayNextMonth = givenDate.AddDays(-givenDate.Day + 1).AddMonths(1);
int diff = 7 - (int)firstDayNextMonth.DayOfWeek;
return firstDayNextMonth.AddDays(diff);
}

If birth date is in current week return true

I am doing my lab hw. There is a part that I couldn't handle it: "The company wants to give bonus 200 TL to the employee if the current week is the one in which the employee’s birthday occurs." I am trying to write a method that returns boolean, if the birth date in the current week. Then, I realised that the method I have done is just checking if the current day is equalt to birth date. What I want is if the current payment week if inculdes someone's birthdate. I think I need to use a calendar or something like that. Since the program is temp, each day will be payment day. I mean if the birthday is in the same week with DateTime.Today then return true. And add 200 TL more to the salary.
protected bool isBirthDay()
{
try
{
string today = DateTime.Today.
string birth_date = birthDate.ToString("d");
if (today[0]== birth_date[0] && today[1]== birth_date[1] && today[3]==birth_date[3] && today[4]==birth_date[4])
return true;
return false;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
This is more complex than I initially thought. Fortunately the DateTime structure has a method called DayOfYear that could simplify our calculation because we can remove the Year problem and the leap year checks
bool isBirthWeek(DateTime birthDate)
{
DateTime dtStart = DateTime.Today;
// Find the previous monday
while (dtStart.DayOfWeek != DayOfWeek.Monday)
{
dtStart = dtStart.AddDays(-1);
}
DateTime dtEnd = dtStart.AddDays(7);
int dayStart = dtStart.DayOfYear;
int dayEnd = dtEnd.DayOfYear;
int birthDay = birthDate.DayOfYear;
return (birthDay >= dayStart && birthDay < dayEnd);
}
A more generic version could replace the DateTime.Today with a custom date passed from the caller
bool isBirthWeek(DateTime dtStart, DateTime birthDate)
{
// Find the previous monday
while (dtStart.DayOfWeek != DayOfWeek.Monday)
{
dtStart = dtStart.AddDays(-1);
}
DateTime dtEnd = dtStart.AddDays(7);
int dayStart = dtStart.DayOfYear;
int dayEnd = dtEnd.DayOfYear;
int birthDay = birthDate.DayOfYear;
return (birthDay >= dayStart && birthDay < dayEnd);
}
but if you just want to know if the birthdate is inside the range of 6 day before the payment date you could write
bool isBirthWeek(DateTime payDate, DateTime birthDate)
{
DateTime dtStart = payDate.AddDays(-6);
DateTime dtEnd = payDate;
int dayStart = dtStart.DayOfYear;
int dayEnd = dtEnd.DayOfYear;
int birthDay = birthDate.DayOfYear;
return (birthDay >= dayStart && birthDay < dayEnd);
}
I think people are making this too difficult.
/// <summary>
/// Returns True birthdate is in the same week as the compareDate, with Sunday being the start of the week
/// </summary>
/// <param name="compareDate"></param>
/// <param name="birthdate"></param>
/// <returns></returns>
public static bool IsInSameWeek(DateTime compareDate, DateTime birthdate)
{
var startDate = compareDate.AddDays(-(int)compareDate.DayOfWeek); //get the Sunday of the week
if (!DateTime.IsLeapYear(startDate.Year) && birthdate.Month == 2 && birthdate.Day == 29)
birthdate = birthdate.AddDays(-1);
var compareBd = new DateTime(startDate.Year, birthdate.Month, birthdate.Day); //get the birth date of the compare year
var totalDays = (compareBd - startDate).TotalDays; //number of days between the dates.
return (totalDays >= 0 && totalDays <= 6); //Are we within 6 days of each other
}
As the comments point out, we get the start of the week of "compareDate", with Sunday being defined as the start of the week.
We then create a new DateTime with the birth month and day of the year of "compareDate".
Then we make sure the new birthday is equal to the compare date, but no more than 6 days older.

How can I get total number of days in selected month with/without the weekends in WPF?

I am hoping to find a way to get the total number of days in a month with and without the weekends using LINQ in my ViewModel. For example, September would count for 20 days without weekends and 30 days with the weekends.
I also have a datepicker binding to MDate and I have no idea were to start.
Can someone please suggest an easy method? I am just an amateur.
private DateTime _mDate = DateTime.Now;
public DateTime MDate
{
get { return _mDate; }
set
{
if (value == _mDate)
{
return;
}
else
{
_mDate = value;
OnPropertyChanged("MDate");
SetDaysInMonth();
}
}
}
private void SetDaysInMonth()
{
???
}
Thank you.
DateTime.DayOfWeek method could come handy, and this answer by Ani shows how to get all days in a given month.
borrowing from the answer mentioned above:
public static List<DateTime> GetDates(int year, int month)
{
var dates = new List<DateTime>();
// Loop from the first day of the month until we hit the next month, moving forward a day at a time
for (var date = new DateTime(year, month, 1); date.Month == month; date = date.AddDays(1))
{
if(date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday)
{
dates.Add(date);
}
}
return dates;
}
int count = Enumerable.Range(1, DateTime.DaysInMonth(2017, 10)) // year and month
.Select(day => new DateTime(2017, 10, day)) // year and month
.Where(d => d.DayOfWeek != DayOfWeek.Sunday && d.DayOfWeek != DayOfWeek.Saturday) // check each day for weekend
.ToList().Count;
Hope you need only the no of working days in a month
Find the number of days in month then enumerate and excluded Saturday and Sunday.
private static void SetDaysInMonth(DateTime mDate)
{
int numberOfBusinessDays = Enumerable.Range(1, DateTime.DaysInMonth(mDate.Year, mDate.Month))
.Select(day => new DateTime(2017, mDate.Month, day))
.Count(d => d.DayOfWeek != DayOfWeek.Sunday && d.DayOfWeek != DayOfWeek.Saturday);
}

Get all the dates of current week

Say I consider Sunday - Saturday as a week, how do I get all the dates of the current week in c#?
For example, current date is 30th March 2017, the output I need is,
26-March-2017,
27-March-2017,
28-March-2017,
29-March-2017,
30-March-2017,
31-March-2017,
01-April-2017
You can try DateTimeFormat to find out current week's starting date and Linq to generate the string:
DateTime startOfWeek = DateTime.Today.AddDays(
(int) CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek -
(int) DateTime.Today.DayOfWeek);
string result = string.Join("," + Environment.NewLine, Enumerable
.Range(0, 7)
.Select(i => startOfWeek
.AddDays(i)
.ToString("dd-MMMM-yyyy")));
In case of en-US culture you'll get (week starts from Sunday)
26-March-2017, // <- starts from Sunday
27-March-2017,
28-March-2017,
29-March-2017,
30-March-2017,
31-March-2017,
01-April-2017
In case of, say, ru-RU culture you'll get (week starts from Monday)
27-марта-2017, // <- Starts from Monday
28-марта-2017,
29-марта-2017,
30-марта-2017,
31-марта-2017,
01-апреля-2017,
02-апреля-2017
Assuming that Sunday will be the start day of the week, as it is mentioned in the question I suggest following solution.
var today = DateTime.Now.Date; // This can be any date.
Console.WriteLine(today.DayOfWeek);
var day = (int)today.DayOfWeek; //Number of the day in week. (0 - Sunday, 1 - Monday... and so On)
Console.WriteLine(day);
const int totalDaysOfWeek = 7; // Number of days in a week stays constant.
for (var i = -day; i < -day + totalDaysOfWeek; i++)
{
Console.WriteLine(today.AddDays(i).Date);
}
I found this here
DayOfWeek Day = DateTime.Now.DayOfWeek;
int Days = Day - DayOfWeek.Monday; //here you can set your Week Start Day
DateTime WeekStartDate = DateTime.Now.AddDays(-Days);
DateTime WeekEndDate1 = WeekStartDate.AddDays(1);
DateTime WeekEndDate2 = WeekStartDate.AddDays(2);
DateTime WeekEndDate3 = WeekStartDate.AddDays(3);
DateTime WeekEndDate4 = WeekStartDate.AddDays(4);
DateTime WeekEndDate5 = WeekStartDate.AddDays(5);
DateTime WeekEndDate6 = WeekStartDate.AddDays(6);
In my opinion, an extension method is the most useful approach:
public static IEnumerable<DateTime> GetDatesOfWeek(this DateTime date, CultureInfo ci) {
Int32 firstDayOfWeek = (Int32) ci.DateTimeFormat.FirstDayOfWeek;
Int32 dayOfWeek = (Int32) date.DayOfWeek;
DateTime startOfWeek = date.AddDays(firstDayOfWeek - dayOfWeek);
var valuesDaysOfWeek = Enum.GetValues(typeof(DayOfWeek)).Cast<Int32>();
return valuesDaysOfWeek.Select(v => startOfWeek.AddDays(v));
}
Use as follows:
DateTime myDate = DateTime.Today;
IEnumerable<DateTime> result = myDate.GetDatesOfWeek(CultureInfo.CurrentCulture);
foreach ( DateTime d in result ) {
Console.WriteLine(d);
}

How do I determine if a date lies between current week dates?

In C#,
How we are check certain date with in week dates?
Eg: 6/02/2014
Current Weeks: 02/02/2014 - 08/02/2014
so this dates are with in above week....
Use this for check (last parameter is optional if you want always 1 week from fromDate, you don't need use last parameter):
public static bool DateInside(DateTime checkDate,
DateTime fromDate, DateTime? lastDate = null)
{
DateTime toDate = lastDate != null ? lastDate.Value : fromDate.AddDays(6d);
return checkDate >= fromDate && checkDate <= toDate;
}
To call use:
bool isDateInside = DateInside(new DateTime(2014, 02, 06),
new DateTime(2014, 02, 02)); // return true
And search first :) Answer is also here: How to check whether C# DateTime is within a range
If you want to check if the dates are inside the same week, then you can use this:
public static bool DateInsideOneWeek(DateTime checkDate, DateTime referenceDate)
{
// get first day of week from your actual culture info,
DayOfWeek firstWeekDay = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
// or you can set exactly what you want: firstWeekDay = DayOfWeek.Monday;
// calculate first day of week from your reference date
DateTime startDateOfWeek = referenceDate;
while(startDateOfWeek.DayOfWeek != firstWeekDay)
{ startDateOfWeek = startDateOfWeek.AddDays(-1d); }
// fist day of week is find, then find last day of reference week
DateTime endDateOfWeek = startDateOfWeek.AddDays(6d);
// and check if checkDate is inside this period
return checkDate >= startDateOfWeek && checkDate <= endDateOfWeek;
}
Actual week in my culture info start with monday, February 3th 2014 (so for me is week between February 3th and February 9th). If I check any date with reference date (second parameter) as today (2014-Feb-06) I get this results:
For 2014-Feb-02 (Sunday before this week): false
For 2014-Feb-03 (Monday inside this week): true
For 2014-Feb-06 (Today inside this week): true
For 2014-Feb-09 (Sunday inside this week): true
For 2014-Feb-10 (Monday next week): false
You can call this method to check if one date is inside the same week as referentional like this:
DateInsideOneWeek(new DateTime(2014, 02, 02), new DateTime(2014, 02, 06));
You can find current week start and end dates with this code:
DateTime startDateOfWeek = DateTime.Now.Date; // start with actual date
while(startDateOfWeek.DayOfWeek != DayOfWeek.Monday) // set first day of week in your country
{ startDateOfWeek = startDateOfWeek.AddDays(-1d); } // after this while loop you get first day of actual week
DateTime endDateOfWeek = startDateOfWeek.AddDays(6d); // you just find last week day
Is this you wanted?
hmm
public bool isBetween(DateTime input, DateTime date1, DateTime date2)
{
if (input > date1 && input < date2)
return true;
else
return false;
}
?
input= your date
date1 & date2 = start and end of a week
How about:
bool inRange = (date >= lowerDate && date <= upperDate);
Here's another solution :)
public static class DateExtensions
{
private static void Swap<T>(ref T one, ref T two)
{
var temp = one;
one = two;
two = temp;
}
public static bool IsFromSameWeek(this DateTime first, DateTime second, DayOfWeek firstDayOfWeek = DayOfWeek.Monday)
{
// sort dates
if (first > second)
{
Swap(ref first, ref second);
}
var daysDiff = (second - first).TotalDays;
if (daysDiff >= 7)
{
return false;
}
const int TotalDaysInWeek = 7;
var adjustedDayOfWeekFirst = (int)first.DayOfWeek + (first.DayOfWeek < firstDayOfWeek ? TotalDaysInWeek : 0);
var adjustedDayOfWeekSecond = (int)second.DayOfWeek + (second.DayOfWeek < firstDayOfWeek ? TotalDaysInWeek : 0);
return adjustedDayOfWeekSecond >= adjustedDayOfWeekFirst;
}
}
Upd: it appears to have at least twice better performance than #Atiris solution :)

Categories

Resources