473,503 Members | 3,045 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11978
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
22425
by: Solomon Grundy | last post by:
Select trunc(sysdate,'MM') from dual of course gives me the first day of the month, may I ask what the query is to find the first saturday of the month?
7
6018
by: MLH | last post by:
Public Function GetLastDayOfMonth(ByVal dtDay As Date) As Date '************************************************************************** ' Accepts a date. Determines month & year of the date....
5
6846
by: hunt4grouse | last post by:
I'm looking for C code that will give me the last day of the month. Input - any day that I pass in as a variable.... input = 20050201 Output - calculate and format with slashes.... output =...
4
48198
by: laredotornado | last post by:
Hi, Using PHP 4, if I have a date, what is a function I could use to give me a date that represents the first day of that month? For example, if my date were "3/19/2006 8:00", I would want my...
44
10127
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
7
2350
by: dunkleypilot | last post by:
Hello, I have created a database to track the flights on three aircraft owned by the flight school I manage. The relevant fields in the FlightRecords table are Date, AircraftID, HourMeterOut, and...
3
6099
by: ats | last post by:
Does anybody have any sample code for calculating the date for teh last Friday in each month. TIA -- ats@jbex When an old lady got hit by a truck I saw the wicked gleam in your eyes
6
24608
by: creative1 | last post by:
hello I am using following code to access first and last date of current month StartingDate_msk.Text = Format$(Date, "dd/mm/yyyy") EndingDate_msk.Text = Format$(MonthLastDay(Date),...
11
2435
by: Harlin Seritt | last post by:
Is there a module that can pull str values for say the last 3 months? Something like: print lastMonths(3) Thanks
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.