469,278 Members | 2,391 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,278 developers. It's quick & easy.

How to get first and last fridays in a month?

Hello All,

Is there a way I can get dates for first and last fridays of a month? Any
suggestion would be helpful.

Thanks.
Nov 17 '05 #1
2 11220
There's no direct way that I'm aware of, but you could write a simple piece
of code like this:

DateTime dtFirstFriday = DateTime.MinValue;
DateTime dtLastFriday = DateTime.MaxValue;

DateTime dtFront = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime dtBack = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));

while (dtFirstFriday == DateTime.MinValue || dtLastFriday ==
DateTime.MaxValue)
{
if (dtFirstFriday == DateTime.MinValue && dtFront.DayOfWeek ==
DayOfWeek.Friday)
dtFirstFriday = dtFront;
else
dtFront = dtFront.AddDays(1.0);

if (dtLastFriday == DateTime.MaxValue && dtBack.DayOfWeek ==
DayOfWeek.Friday)
dtLastFriday = dtBack;
else
dtBack= dtBack.AddDays(-1.0);
}

At the end of the loop dtFirstFriday and dtLastFriday will hold the
appropriate values.

--
Kai Brinkmann [Microsoft]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.

"Sasidhar Parvatham" <sa****************@gmail.com> wrote in message
news:uV**************@TK2MSFTNGP10.phx.gbl...
Hello All,

Is there a way I can get dates for first and last fridays of a month? Any
suggestion would be helpful.

Thanks.

Nov 17 '05 #2
Ron
Try this:

private void FirstAndLastFridays(int month, int year, out int first, out int
last)
{
//get last friday
int lastFriday = DateTime.DaysInMonth(year, month);
DateTime day = new DateTime(year, month, lastFriday);
do
{
day = new DateTime(year, month, lastFriday);
if (day.DayOfWeek != DayOfWeek.Friday)
lastFriday--;
}
while (day.DayOfWeek != DayOfWeek.Friday);
//get first friday
int firstFriday = 1;
do
{
day = new DateTime(year, month, firstFriday);
if (day.DayOfWeek != DayOfWeek.Friday)
firstFriday++;
}
while (day.DayOfWeek != DayOfWeek.Friday);
first = firstFriday;
last = lastFriday;
}

"Sasidhar Parvatham" wrote:
Hello All,

Is there a way I can get dates for first and last fridays of a month? Any
suggestion would be helpful.

Thanks.

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Solomon Grundy | last post: by
5 posts views Thread by hunt4grouse | last post: by
4 posts views Thread by laredotornado | last post: by
3 posts views Thread by ats | last post: by
11 posts views Thread by Harlin Seritt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.