Avatar of Member_2_1242703
Member_2_1242703
 asked on

jQuery/javascript/bootstrap - Working with datepicker, combining two scripts, StartDate/EndDate

I have two sets of code, both which work on their own, but I'm really struggling to combine the two.
What I'm trying to do is control start date / end date entries into respective textboxes with datepicker.
When the start date is selected, the date picker disables dates before that selected date when the other text box is clicked.
Also, only days up until the next Sunday (from the selected start date) can be selected as an end date.

So this code works to disable any previous dates for the endDate textbox.

  var dateFormat = "mm/dd/yy";
  var from = $(".start").datepicker({
    changeMonth: true,
    numberOfMonths: 1

  }).on("change", function () {
    to.datepicker("option", "minDate", getDate(this));
  });

  var to = $(".end").datepicker({
    changeMonth: true,
    numberOfMonths: 1
  }).on("change", function() {
    from.datepicker("option", "maxDate", getDate(this));
  });

  function getDate(element) {
    var date;
    try {
      date = $.datepicker.parseDate(dateFormat, element.value);
    } catch (error) {
      date = null;
    }

    return date;
  }

Open in new window


And this code works to only allow dates selected until the next Sunday:

<script>
var dateFormat = "mm/dd/yy";
$(document).ready(function(){
  var from = $("#StartDate").datepicker({
    changeMonth: true,
    dateFormat: dateFormat,
    numberOfMonths: 1,
    onSelect: function(dt, inst){
      var fromDt = $(this).datepicker("getDate");
      var nextSunday = new Date(fromDt.getTime());
      nextSunday.setDate(nextSunday.getDate() + 7 - fromDt.getDay());
());

      to.datepicker("option", "minDate", dt);
      to.datepicker("option", "maxDate", nextSunday);
    }
  });

  var to = $("#EndDate").datepicker({
    changeMonth: true,
    dateFormat: dateFormat,
    numberOfMonths: 1,
    onSelect: function(dt, inst){
      from.datepicker("option", "maxDate", dt);
    }
  });
});
</script>

Open in new window


I need to combine the two, or have a better way to do it. I just can't seem to get them to play together.
JavaScriptjQuery.NET ProgrammingWeb Development

Avatar of undefined
Last Comment
Member_2_1242703

8/22/2022 - Mon
Leonidas Dosas

Can you post and the HTML code?
Member_2_1242703

ASKER
It's CHTML. What are you looking for specifically?
ASKER CERTIFIED SOLUTION
Member_2_1242703

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Member_2_1242703

ASKER
Mine was the only one provided
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck