JavaScript how can i compare date strings? compare date strings javascript,compare date strings python,compare date

i need to check whether the date is between minDate and maxDate. but when i try to compare with minDate, i should get valid as false but am getting true..

            let minDate = "27-05-2019";
        let maxDate = "27-05-2019";
        let date = "13-02-2018";
        var valid;

        if(new Date(date.replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3")).toDateString() >= new Date(minDate.replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3")).toDateString()){
            valid = true;
            } else {
            valid = false;
            }
            
            console.log(valid);
Answer:1

Plain JS solution with a couple of helper functions:

const splitMyDateString = str => {
  const [day, mon, yr] = str.split('-').map(Number);
  return [yr, mon, day];
};

const makeDate = ([yr, mon, day]) => {
  return new Date(yr, mon - 1, day);
};

let minDate = makeDate(splitMyDateString("27-05-2019"));
let maxDate = makeDate(splitMyDateString("27-05-2019"));
let date = makeDate(splitMyDateString("13-02-2018"));
let valid = (date >= minDate) && (date <= maxDate);

Note that the Date constructor is not guaranteed to accept strings accept in the ISO 8601 format. So here we're passing integers instead. Note also we have to subtract 1 from the month because JS months are 0-11, not 1-12.

Answer:2

The following snippet is from "Example 5" from this thread on JS and closure. Alternatively, this other thread sort of gets at what I'm curious about. function buildList(list) { var result = []; ...

The following snippet is from "Example 5" from this thread on JS and closure. Alternatively, this other thread sort of gets at what I'm curious about. function buildList(list) { var result = []; ...

  1. javascript closure function parameters
  2. function closure argument

I want to make my component more reusable. In the component I'm binding two values with ngModel: elem.key and elem.value. The problem is that wherever I want to use this component, the element has to ...

I want to make my component more reusable. In the component I'm binding two values with ngModel: elem.key and elem.value. The problem is that wherever I want to use this component, the element has to ...

  1. angular make component fill parent
  2. angular make component injectable
  3. angular make component focusable
  4. angular make component global
  5. angular make component fullscreen
  6. angular make component singleton
  7. angular make component visible
  8. angular make component invisible
  9. angular make component read only
  10. angular make component draggable
  11. angular make component scrollable
  12. angular make component
  13. angular make component clickable
  14. angular make component refresh
  15. angular make component full page
  16. angular make component input required
  17. angular make component hidden
  18. angular make component sticky
  19. angular make component height 100
  20. angular make component observable

So im trying to get a div with an iframe in it to reload every 20 sec, i have searched a bit on it, got a code to work on local, but when i upload it to the internet, it wont work, WHY ?. Are there ...

So im trying to get a div with an iframe in it to reload every 20 sec, i have searched a bit on it, got a code to work on local, but when i upload it to the internet, it wont work, WHY ?. Are there ...

I have an array of prime numbers: const primes = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] I want to find the first number in this list that is <= the number given. ...

I have an array of prime numbers: const primes = [3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] I want to find the first number in this list that is <= the number given. ...

  1. javascript find first in array
  2. javascript find first item in array
  3. javascript find first character in string
  4. javascript find first number in string
  5. javascript find first element in array
  6. javascript find first letter in string
  7. javascript find first index
  8. javascript find first instance of character in string
  9. javascript find first child
  10. javascript find first space in string
  11. javascript find first match
  12. javascript find first input element
  13. javascript find first child of type
  14. javascript find first matching item in array
  15. javascript find first parent with class
  16. javascript find first duplicate in array
  17. javascript find first alpha character
  18. javascript find first non whitespace character
  19. javascript find first non numeric character
  20. javascript find first element