How to get date of next friday

HI i have a date let say 2020-02-14 how can i find next coming friday date which will be (2020-02-21)
can anybody help in this ?

1 Like

static string GetNextFriday(DateTime dt)
{
int diff = (int)(dt.DayOfWeek - DayOfWeek.Friday);
var str = “”;
if (diff == 0)
{
str = “Today is Friday”;
}
else if (diff < 0)
{
str = "Next friday is : " + dt.AddDays(Math.Abs(diff)).ToShortDateString();
}
else if (diff > 0)
{
str = "Next friday is : " + dt.AddDays(Math.Abs(6)).ToShortDateString();
}
return str;
}

//calling method
Console.WriteLine(GetNextFriday(DateTime.Now));
Console.WriteLine(GetNextFriday(new DateTime(2020,02,28)));
Console.WriteLine(GetNextFriday(new DateTime(2020, 03, 01)));

Hi,

Hope the following expression helps you.

dt.AddDays(if(dt.DayOfWeek<DayofWeek.Friday,DayOfWeek.Friday-dt.DayOfWeek,7-dt.DayOfWeek+DayOfWeek.Friday))

dt is datetime variable.

Regards,

2 Likes

Hi @goharmalik,

For this you can try this expression,

date1.AddDays(5-Convert.ToInt32(date1.DayOfWeek))

Example -
here date1 is DateTime variable.
Convert.ToInt32(date1.DayOfWeek) —> this will convert DayOfWeek in number(let’s take today’s date 2020/02/26, DayOfWeek is Wednesday then, it’ll be 3)
As 5th day is friday, (5-Convert.ToInt32(date1.DayOfWeek)) —> 5-3 becomes 2.
date1.AddDays(2) —> 2020/02/28 which is coming friday.

that’s it.:slight_smile:

1 Like

you can try below -

DateTime.Now.AddDays(7 - CInt(DayOfWeek.Friday))

Today - 26Feb
Output for Next Friday is 28Freb

1 Like

@samir I have a doubt. When it is saturday, (5-Convert.ToInt32(date1.DayOfWeek)) this returns -1 and wont it give previous friday rather than future date?

@samir Let suppose the date is 29/02/2020. This expression will give me the date of 28/02/2020 for Friday. The expression is expected to give next Friday but this will give past Friday.

hey @Sugumar8785, @goharmalik

try this,

If(date1.AddDays(5-CInt(Date1.DayOfWeek))<=date1,date1.AddDays(5-CInt(Date1.DayOfWeek)).AddDays(7),date1.AddDays(5-CInt(Date1.DayOfWeek)))

this will give to next coming Friday even if given date day is Friday.

1 Like

it is working thanks

it worked

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.