Click here to Skip to main content
15,883,758 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how I found first and last saturday of current months .pls help me
Posted
Comments
What have you tried?

sample code :
C#
void Main()
{
    var dates = GetDates(DateTime.Now.Year, DateTime.Now.Month);
    var fistSat = dates.FirstOrDefault(d=>d.DayOfWeek==DayOfWeek.Saturday );
    var lastSat = dates.LastOrDefault(d=>d.DayOfWeek==DayOfWeek.Saturday );
}

public static List<DateTime> GetDates(int year, int month)
{
   return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                    .Select(day => new DateTime(year, month, day))
                    .ToList();
}
 
Share this answer
 
Comments
Animesh Datta 6-Jun-14 1:00am    
my 5!
DamithSL 6-Jun-14 1:11am    
Thank You Animesh :-)
Debabrata_Das 6-Jun-14 1:51am    
my 5 too! :)
DamithSL 6-Jun-14 1:55am    
Thanks Debabrata_Das
Linq way is practical and easy, this is the classical method if anyone needs a non linq method;

C#
public static Tuple<DateTime,DateTime> GetFirstAndLastSaturday()
{
    var curDate = DateTime.Now;
    var firstSaturday = new DateTime(curDate.Year,curDate.Month,1);
    while (firstSaturday.DayOfWeek != DayOfWeek.Saturday)
    {
       firstSaturday = firstSaturday.AddDays(1);
    }
    var lastSaturday = firstSaturday;
    while (lastSaturday.AddDays(7).Month == curDate.Month)
    {
        lastSaturday = lastSaturday.AddDays(7);
    }

    return new Tuple<DateTime, DateTime>(firstSaturday,lastSaturday);
}

var result = GetFirstAndLastSaturday();
 
Share this answer
 
v2
It's easiest if you let the DateTime class do the work.

Use the 1st day of the month and calculate to next Saturday. Use the 1st day of the next month and calculate to previous Saturday.

private void onDtPickerValueChanged(object sender, EventArgs e)
{
	DateTimePicker dtPicker = (DateTimePicker)sender;
	int firstSaturday = 0;
	int lastSaturday = 0;

	getFirstLastSatuday(dtPicker.Value, out firstSaturday, out lastSaturday);
	System.Diagnostics.Debug.WriteLine("First Saturday = {0}, Last Saturday = {1}",
		firstSaturday, lastSaturday);
}

private static void getFirstLastSatuday(DateTime date, out int firstDay, out int lastDay)
{
	DateTime dateTime = new DateTime(date.Year, date.Month, 1);
	int dayofWeek = (int)dateTime.DayOfWeek; // Sunday = 0, Saturday = 6
	firstDay = 1 + (6 - dayofWeek);

	dateTime = dateTime.AddMonths(1);
	dayofWeek = (int)dateTime.DayOfWeek;
	lastDay = dateTime.AddDays((dayofWeek + 1) * -1).Day;
}
 
Share this answer
 
This method computes the first and last Saturday of the current month.

C#
DateTime firstDate = DateTime.Parse(DateTime.Now.Year.ToString () + "/" + DateTime.Now.Month.ToString () + "/01");
DateTime lastDate; // Last date of the Current Month
int intDiff;
DateTime firstSaturday; // First Saturday of the Current Month
DateTime lastSaturday;   // Last Saturday of the Current Month
//
// Compute First Saturday
intDiff= 6 - (int)firstDate.DayOfWeek;
firstSaturday = firstDate.AddDays(intDiff); 
//
// Compute Last Date of the Current Month 
lastDate = firstDate.AddMonths(1).AddDays(-1);  
//
// Compute Last Saturday of the Current Month
intDiff = 6 - (int)lastDate.DayOfWeek;
if (intDiff == 0)
{
    lastSaturday = lastDate;
}
else
{
    lastSaturday = lastDate.AddDays(-(7 - intDiff));
}
//
//
Debug.WriteLine("First Saturday={0}", firstSaturday);
Debug.WriteLine("Last Saturday={0}", lastSaturday);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900