0

I'm trying to valid the CheckIn, CheckOut datete, but when checkIn===checkOut was be false instead of true. How you can see, both are object and both equal.

function inverterData(date) {
  try {
    const regex = '^([0-9]{2})([0-9]{2})([0-9]{4})';
    const dataFormatada = date.match(regex);
    return `${dataFormatada[2]}/${dataFormatada[1]}/${dataFormatada[3]}`;
  } catch (error) {
    return [{ mensagem: 'Date is not valid!' }];
  }
}

function isValidDate(d) {
  return d instanceof Date && !isNaN(d);
}

function validacao(checkin, checkout) {
  // Check if entries is not null, undefined ou empty
  if (!checkin || !checkout) return [{ mensagem: 'Fill CheckIn and / or Checkout.' }];
  // Allow only string types
  if (typeof (checkin) === 'string' && typeof (checkout) === 'string') {
    let checkIn = new Date(inverterData(checkin));
    let checkOut = new Date(inverterData(checkout));
     console.log(typeof(checkIn),typeof(checkOut));
     console.log(checkIn,checkOut);
     console.log(checkIn===checkOut);
    // Date is not valid
    if (!isValidDate(checkIn) || !isValidDate(checkOut)) return [{ mensagem: 'Date is not valid' }];
    // CheckIn cannot be equal Checkout
    if (checkIn === checkOut) return [{ mensagem: 'CheckIn cannot be equal to checkOut' }];

    // Checkout can be less than CheckIn
    if (checkOut < checkIn) return [{ mensagem: 'Checkout can be less than' }];
  } else {
    return [{ messagem: 'Data type is not allowed.' }];
  }
  return [];
}
console.log(validacao('11022021','11022021'));

Anyone help to understand why It is helping?

Hasunohana
  • 565
  • 8
  • 22
  • 2
    Never write your own date comparison. Use a well tested datetime library (for JS that's [moment.js](https://momentjs.com/)) and never rely on your own code to do the right thing: it can't. Datetime maths has millions of edge cases, and you don't know all (and often, even just "some") of them. – Mike 'Pomax' Kamermans Feb 13 '21 at 22:45
  • They both are equals or their content is? – Buddy Christ Feb 13 '21 at 22:46
  • @peprumo yeah, if you run this code, I showed with console.log the results – Hasunohana Feb 13 '21 at 22:51

2 Answers2

1

You cant compare objects in javascript using ===.

If you want to check dates you should convert it to string first, for example.

Please check - this threads for more details JavaScript Date Object Comparison

Object comparison in JavaScript

Vlad
  • 459
  • 4
  • 8
1

Why compare dates as strings instead of using moment.js? it's much easier and would give you more control over such operations.

Also, you can convert the date to timestamp and check if both within the same day.

Mazen Ak
  • 152
  • 7
  • I thought to use `moment.js` but how that project is for interview, I'm avoiding use some libaries. – Hasunohana Feb 13 '21 at 23:03
  • Try then to parse the date string and split it by the char you have ('/' or '-') and compare each value of it separately, day with day, month with month, year with year. – Mazen Ak Feb 13 '21 at 23:13
  • By using `getTime()` works, I appreciate your help Mazen :) – Hasunohana Feb 13 '21 at 23:38