0

I only want the date range to be selectable for future days and the first day should be before or the same day as the 2nd day to create a date range. Input is first date: 01/23/2020
second date: 03/19/2020 currdate: 12/11/2019

var curdate = month + "/" + day + "/" + year;
if(smsBlackoutFirstDateSelect.value > smsBlackoutSecondDateSelect.value){
      alert("second date is before first date");
} else if(smsBlackoutFirstDateSelect.value <= curdate){
      alert("first date is on or before today" + " " + smsBlackoutFirstDateSelect.value + " " + 
             smsBlackoutSecondDateSelect.value + " " + curdate);
} else { 
     some success function;
}

Output is : first date is on or before today 01/23/2020 03/19/2020 12/11/2019

any idea why it isn't comparing years?

Tim Willis
  • 140
  • 10
  • 4
    You are formatting dates as strings in mm/dd/yyyy format and then doing a string comparison. `01` comes before `03`. Either make date objects and compare those, or format it so the most significant part of the date comes first, yyyy/mm/dd. –  Dec 11 '19 at 18:22
  • I think you are correct with your analysis, I went with Nasim's solution though – Tim Willis Dec 11 '19 at 19:28
  • I covered the solution proposed by Nasim as `make date objects and compare those,...` –  Dec 11 '19 at 19:29

2 Answers2

1

if you are referring to the comparison of curdate with smsBlackoutFirstDateSelect.value, curdate is not a date field.

if you want date comparison to convert it to date by

var d1 = new Date(curdate); and use it you can refer below question Compare two dates with JavaScript

Nasim
  • 325
  • 2
  • 14
1

I see that you're using a string to represent the date. This won't work well. Ideally, we should use javascript Date variables to handle the sorting as needed.
Try something like this and see how it compares.

Example: 3 numbers specify year, month, and day:

var curdate = new Date(2018, 11, 24);

I can't see how smsBlackoutFirstDateSelect is defined. It may also need to be casted to Dates() separately. See the other methods to create a Date() referenced below.

Reference: https://www.w3schools.com/js/js_dates.asp

CloudEmber
  • 99
  • 4