hi,
how to read all the sundays of a month from datetimepicker in c#
i have a datetimepicker on my windows from with a button click i want to store all the sundays in to my database
so how to capture the all the dates which comes under sunday from datetimepicker
Loading

Augustin Jianu10 reputationPosted Apr 21, 2010, 2:33 AM EDT
What you should do is establish a time interval (StartDate <-> EndDate), and store all the Sundays between those two dates.
This code should do the trick:
DateTime CurrentDate = StartDate;
//I try to find the very first Sunday after the StartDate
while (CurrentDate.DayOfWeek != DayOfWeek.Sunday)
CurrentDate = CurrentDate.AddDays(1);
//after I found the first Sunday I always skip 7 days (a week)
//this ensures me that I go from Sunday to Sunday
for (; CurrentDate <= EndDate; CurrentDate = CurrentDate.AddDays(7))
{
//to do: save CurrentDate to the database because it's certainly a Sunday
}
Hirendra SisodiyaRank #258 · 9.8K reputationPosted Apr 21, 2010, 4:44 AM EDT
hello pramod
try this code:
DateTime dt = dateTimePicker1.Value;
DateTime firstDayOfThisMonth = dt.Subtract(TimeSpan.FromDays(dt.Day - 1));
//DateTime lastDayOfThisMonth = firstDayOfThisMonth.Subtract(TimeSpan.FromDays(1));
DateTime lastDayOfThisMonth = dt;
lastDayOfThisMonth = lastDayOfThisMonth.AddMonths(1);
lastDayOfThisMonth = lastDayOfThisMonth.AddDays(-(lastDayOfThisMonth.Day));
DateTime tempdate;
for (tempdate = firstDayOfThisMonth; tempdate <= lastDayOfThisMonth; tempdate = tempdate.AddDays(1))
{
if (tempdate.DayOfWeek.ToString() == "Sunday")
{
//current day is sunday
//MessageBox.Show(tempdate.ToString());
}
}
thanks
Please mark as answere if it helps