c# date calculation bug

Posted on 2006-03-30
Last Modified: 2012-06-21
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
Question by:aaalife
    LVL 4

    Expert Comment

    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


    Author Comment

    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
    LVL 4

    Accepted Solution

    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);

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now