skip to Main Content

I have a function where user selects dates and it gets stored in an array object.

There is check to see if user does not select dates that are already passed.

It should ideally allow user to select current date but when I do so it is not working and the check gets triggered.

datesArr = [{ date: '05/10/2023' }, { date: '05/11/2023' }];

let result = datesArr.every(({ date }) => {
  return new Date(date).toLocaleDateString() < new Date().toLocaleDateString();
});

console.log(result);

result === false ? promptMessages(`Cannot use an older date.`, 'error') : '';

So as per the array, the dates selected by the user should be valid, instead I get the result as false.

I only need to match dates, so time is not relevant.

How do I ensure the current date is not treated as an old date.

3

Answers


  1. You can use the toISOString but you need to consolidate. If I run the script in my timezone, the 10th at midnight becomes the 9th

    const now = consolidate(new Date()); 
    const consolidate = date => (date.setHours(15,0,0,0), date.toISOString().split("T")[0]);  
    let result = datesArr.every(({ date }) => now <= consolidate(new Date(date)));
    

    Example

    datesArr = [{ date: '05/10/2023' }, { date: '05/11/2023' }];
    
    const consolidate = date => (date.setHours(15,0,0,0), date.toISOString().split("T")[0]);  
    const now = consolidate(new Date()); // only need to do this once
    let result = datesArr.every(({ date }) => {
    console.log(now,consolidate(new Date(date)))
      return now <= consolidate(new Date(date))
    });
    
    const message = result ? `Dates are on or in the future from ${now}` :  'Cannot use an older date. Error';
    
    console.log(message);
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
    Login or Signup to reply.
  2. Instead of comparing the result of toLocaleDateString(), I would compare the integer value using valueOf MDN page for Date.valueOf.

    Your code would look like this:

    const datesArr = [{ date: '05/10/2023' }, { date: '05/11/2023' }];
    const now = new Date().valueOf();
    
    const result = datesArr.every((date) => new Date(date).valueOf() < now);
    
    console.log(result);
    
    result === false ? promptMessages(`Cannot use an older date.`, 'error') : '';
    
    Login or Signup to reply.
  3. The issues with the code you have written is new Date(date).toLocaleDateString() < new Date().toLocaleDateString() this will convert your date to a sting and you are converting the strings there, that’s incorrect. you cant compare two strings and see the date is greater or less

    Also you have to see if the date is not a past date, sees like you have used the comparison operator incorrectly.

    The logic I did is

    • Take each dates and create a date object. When you create a date object from a date string, it will give a date object with time as zeros. new Date("05/10/2023") is "Wed May 10 2023 00:00:00 GMT+0200 (Central European Summer Time) {}". Take the time of this particular date.
    • Similarly get the current date and set hours to zero, this will give you the getTime of that date.
    • Compare these two times.

    JavaScript getTime will return the number of milliseconds for this date since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC. In the time values that we are comparing its against two particular date which has time 00:00:00. Its enough to compare these two times in your case.

    Working Fiddle

    datesArr = [{ date: "05/10/2023" }, { date: "05/11/2023" }];
    
    let result = datesArr.every(({ date }) => new Date(date).getTime() >= new Date().setHours(0, 0, 0, 0));
    
    console.log(result);
    
    result === false ? console.log(`Cannot use an older date.`, "error") : "";
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search