calculate week ending

metropia
metropia used Ask the Experts™
on
hi,

I need to calculate the week ending based on a date provided by the user in a asp.net form.

I am using C#. The calculation of the week ending would be placed here:

            if (dt.HasValue)
            {
                Session["Date"] = dt;
                In here <<
            }

Open in new window


Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can use the DateTime.DayOfWeek property to determine the day of the week.

http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek

Once you know what day of the week it is, it should become clear how many days it is until the end of the week depending on what you define as the end of the week. For example, if "Sunday" is the end of the week, and it currently is "Thursday", then you know that the end of the week is 3 days in the future.

You can advance your date by that many days using the DateTime.AddDays() function, and you should have a datetime set to the end of the week.

http://msdn.microsoft.com/en-us/library/system.datetime.adddays
Something like this, roughly. I haven't tested this code but this is the general idea:

DayOfWeek dow = dt.Date.DayOfWeek;
int num;
if( dow == DayOfWeek.Monday ) {
    num = 6;
}
if( dow == DayOfWeek.Tuesday) {
    num = 5;
}
if( dow == DayOfWeek.Wednesday ) {
    num = 4;
}
if( dow == DayOfWeek.Thursday ) {
    num = 3;
}
if( dow == DayOfWeek.Friday ) {
    num = 2;
}
if( dow == DayOfWeek.Saturday ) {
    num = 1;
}
if( dow == DayOfWeek.Sunday ) {
    num = 0;
}

DateTime dtEndOfWeek;
dtEndOfWeek = dt.Date.AddDays(num);

Open in new window

I would look at the date then work out what day of the week the date is and then add or remove days accordingly.

So for your example, you need to put the date into dateTime datatype.

then use :

dt.DayOfWeek;

to get the days value and then use :

dt.AddDays(xxx);

to add days to make it a week ending.

I hope this helps
Anuradha GoliSystems Development / Support Specialist
Commented:
DateTime dt = Convert.ToDateTime(TextBox1.Text);
        int i = 0;
        switch (dt.DayOfWeek)
        {

            case DayOfWeek.Monday: i = 6;
                break;
            case DayOfWeek.Tuesday: i = 6;
                break;
            case DayOfWeek.Wednesday: i = 4;
                break;
            case DayOfWeek.Thursday: i = 3;
                break;
            case DayOfWeek.Friday: i = 2;
                break;
            case DayOfWeek.Saturday: i = 1;
                break;
            case DayOfWeek.Sunday: i = 0;
                break;
            default: i = 0;
                break;
        }
        //weekend to be sunday
        string weekend = dt.AddDays(i).Date.ToString();
        Label1.Text = weekend;

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial