Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Run through loop for week and dont select weekends

Run through loop for week and dont select weekends

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasejavascripthtmlcomdesign
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xnaLearner
    wrote on last edited by
    #1

    So the user can select 2 different dates, a start date and an end date from 2 calendars, when they click 'add' the dates between&including the dates selected will be added to the DB, each date as a separate record.

    This works fine however I dont want weekends to be added to the DB.

    Iv updated the UI of datepicker http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerCustomCellRender.html but if a user selects fri-mon for example fri,sat,sun and mon will be added to the db Iv tried to only run the code if datyofweek is not saturday or sunday

    public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate)
    {
    DateTime startDates = Convert.ToDateTime(HolidayDate),
    endDates = Convert.ToDateTime(endDate);

            while (startDates <= endDates)
            {
                if (startDates.DayOfWeek != DayOfWeek.Saturday || startDates.DayOfWeek != DayOfWeek.Sunday)
                {
                    Holiday holiday1 = new Holiday();
                    holiday1.PersonId = PersonId.Value;
                    holiday1.HolidayDate = startDates;
              
                    db.Holidays.AddObject(holiday1);
                    db.SaveChanges();
    
                    //say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
                    //but doesnt chage the value of startdates = 'startdates'
                   // startDates = startDates.AddDays(1);
    
                     startDates = startDates.AddDays(1);
                }
            }
    
            return RedirectToAction("Index");
        }
    

    any help? Thanks

    P 1 Reply Last reply
    0
    • X xnaLearner

      So the user can select 2 different dates, a start date and an end date from 2 calendars, when they click 'add' the dates between&including the dates selected will be added to the DB, each date as a separate record.

      This works fine however I dont want weekends to be added to the DB.

      Iv updated the UI of datepicker http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerCustomCellRender.html but if a user selects fri-mon for example fri,sat,sun and mon will be added to the db Iv tried to only run the code if datyofweek is not saturday or sunday

      public ActionResult listHolidays(Holiday holiday, int? PersonId, string HolidayDate, string endDate)
      {
      DateTime startDates = Convert.ToDateTime(HolidayDate),
      endDates = Convert.ToDateTime(endDate);

              while (startDates <= endDates)
              {
                  if (startDates.DayOfWeek != DayOfWeek.Saturday || startDates.DayOfWeek != DayOfWeek.Sunday)
                  {
                      Holiday holiday1 = new Holiday();
                      holiday1.PersonId = PersonId.Value;
                      holiday1.HolidayDate = startDates;
                
                      db.Holidays.AddObject(holiday1);
                      db.SaveChanges();
      
                      //say start date is 10. AddDays(1) will make it 11 then return it to startDates in 'startDates' = startdates,
                      //but doesnt chage the value of startdates = 'startdates'
                     // startDates = startDates.AddDays(1);
      
                       startDates = startDates.AddDays(1);
                  }
              }
      
              return RedirectToAction("Index");
          }
      

      any help? Thanks

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      xnaLearner wrote:

      if (startDates.DayOfWeek != DayOfWeek.Saturday || startDates.DayOfWeek != DayOfWeek.Sunday)

      That's an AND condition, not an OR condition. It should read:

      if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      X 1 Reply Last reply
      0
      • P Pete OHanlon

        xnaLearner wrote:

        if (startDates.DayOfWeek != DayOfWeek.Saturday || startDates.DayOfWeek != DayOfWeek.Sunday)

        That's an AND condition, not an OR condition. It should read:

        if (startDates.DayOfWeek != DayOfWeek.Saturday && startDates.DayOfWeek != DayOfWeek.Sunday)

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        X Offline
        X Offline
        xnaLearner
        wrote on last edited by
        #3

        thanks for the help

        P 1 Reply Last reply
        0
        • X xnaLearner

          thanks for the help

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Not a problem.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups