c# date calculation bug

hi all, have a bug in my code regarding calculating current end of week.  problem is when the month changes, my end week date gets corrupted.

private DateTime weekstartdate = DateTime.Today.AddDays(-(int)(DateTime.Today.DayOfWeek));
private DateTime weekenddate = DateTime.Today.AddDays(-(int)(DateTime.Today.DayOfWeek) + 6);

*** weekenddate ok here, shows 4/1/06

    later in code, to get last day of week:

int day = DateTime.Today.Day;
        int month = DateTime.Today.Month;
        int year = DateTime.Today.Year;

switch (TimeFrame.SelectedValue)
            case "Week":
                startdate = new DateTime(year, month, weekstartdate.Day, 0, 0, 0);
**                enddate = new DateTime(year, month, weekenddate.Day, 23, 59, 59, 997);  **

**** but here enddate year is showing 2006, month 3, weekenddate=4/1/06 (correct) .Day = 1 (correct), 23, 59, 59, 997)

how can i correctly calculate weekend date when month changes, as it has with this week.  my week is considered sunday midnight to saturday 11:59:59:997.

thanks in advanced
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I tried this and it worked. Is this what you're looking for?

DateTime today = DateTime.Today;
DateTime weekStart = today.AddDays( - ((int)today.DayOfWeek) );
DateTime weekEnd = weekStart.AddDays(7).AddMilliseconds(-3);
Console.WriteLine("Start: {0:R}", weekStart);
Console.WriteLine("End:   {0:R}", weekEnd);



Start: Sun, 26 Mar 2006 00:00:00 GMT
End:   Sat, 01 Apr 2006 23:59:59 GMT

aaalifeAuthor Commented:
no, the weekend datetime is fine when declared, it's when i'm in the switch statement determining the actual date for weekend date that the month change corrupts the field.

enddate = new DateTime(year, month, weekenddate.Day, 23, 59, 59, 997);  **

year = 2006, month = 3 -- wrong, needs to be 4 since the month changed at the end of the week
Well, Month is wrong because, above, you're declaring 'month' as DateTime.Today.Month. I'm not sure why you're doing all that work again in the switch, since you had it, essentially, write up at the top.

Try this instead:

========= C# EXAMPLE =========

DateTime today = DateTime.Today;

switch (TimeFrame.SelectedValue)
    case "Week":

        startdate = today.AddDays( - ((int)today.DayOfWeek) );
        endDate = startdate.AddDays(7).AddMilliseconds(-3);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.