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);
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.
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 = []; ...
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 ...
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. ...