Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

using contains?

Back to Forum: Tech Off
  • User profile image
    venkatzeus

    Hi..

    I am comparing 2 date range values. In the application, the user enters start date and end date. After this , the user selects from 7 checkboxes(Monday - sunday) .
    I am using the "contains" to check if the week days contains the selected days checked by the user. 

    here is the function i have used:

    DateTime TheStartDate = DateTime.Parse(txtStartDate.Text);

    DateTime TheEndDate = DateTime.Parse(txtEndDate.Text);


    DateTime
    TempDate = TheStartDate;

    string WeekDay = "";

    while(DateTime.Compare(TempDate,TheEndDate)<=0)

    {

    WeekDay += TempDate.DayOfWeek.ToString() + ",";

    TimeSpan TimeWeekDayspan = new TimeSpan(1,0,0,0);

    TempDate = TempDate.Add(TimeWeekDayspan);

    }

    string CheckedDays = IsWeeklyReturnCheckBoxChecked();

    string []checks = CheckedDays.Split(',');

    string []weeks = WeekDay.Split(',');

    for(int i=0;i<checks.Length-1;i++)

    {

    if(WeekDay.Contains(checks[i]))

    {

    return false;

    }

    else

    {

    Page.ClientScript.RegisterStartupScript(this.GetType(), "__FormHasBeenModified", "alert(INVALID_CHECKBOX);", true);

    return true;

    }

    }

    IsWeeklyReturnCheckBoxChecked() returns the checked days.

    But the problem is , if the user enters start date as 24/04/2007 and end date as 27/04/2007. here we have the range as tuesday to friday. Now if the user selects tuesday,wednesday,thursday and saturday, the value is accepted by the function, but its not valid.

    How to overcome this? Please help

     

  • User profile image
    odujosh

    Way too much code ! Smiley Heres how to test each Checked field:

    public bool DayIsBetween(DateTime start, DateTime end, DayOfWeek test)

    {

    return test >= start.DayOfWeek && test <= end.DayOfWeek;

    }

    DayOfWeek is an Enum Sunday is 0 than you increment it by one to get at each day. Enums are a static dictionary type that lets you have number keys of certain types (default is int) bound to string constants.

    You can use the Checkbox list index as your test value if you start with Sunday and go to Saturday as the values will be the same.

    If you are pickey about time change it to compare start and end to a datetime test. (Worry about time zone in this case!)

  • User profile image
    odujosh

    Here is a console app that demos the code:

    using System;

     

    namespace ConsoleApplication2

    {

    class Program

    {

    static void Main(string[] args)

    {

    DayOfWeek TEST = 0;

    bool mytest = DateIsBetween(DateTime.Now, DateTime.Now.AddDays(2), TEST);

    Console.WriteLine(mytest.ToString());

    DayOfWeek TEST1 = DayOfWeek.Wednesday;

    bool mysecondtest = DateIsBetween(DateTime.Now, DateTime.Now.AddDays(2), TEST1);

    Console.WriteLine(mysecondtest.ToString());

    }

    public static bool DateIsBetween(DateTime start, DateTime end, DayOfWeek test)

    {

    return test > start.DayOfWeek && test < end.DayOfWeek;

    }

    }

    }

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.