ahmed elbarbary

ahmed elbarbary

  • 640
  • 1.6k
  • 284.3k

why compare date journal with current date not match condition working

Jan 9 2025 12:00 AM

I work on the client side using jQuery and I am facing an issue where the journal date is

less than the current date but still matches the criteria, causing the condition to be met

and the code to enter the if block.

Steps I followed:

1- Get the current date in the format dd/MM/yyyy, e.g., 01/09/2024.

2- Retrieve the journal date from the UI datetime picker control Id txt_journal_date, e.g., 31/12/2024.

3- Compare the journal date with the current date.

4- If the journal date is greater than the current date, display an error message.

My issue is that when the journal date (31/12/2024) is smaller than the current date (01/09/2024), it still displays an error and enters the if block.

The expected result is that if the journal date is greater than the current date, it should display an error.

I need to compare the journal date in the format dd/MM/yyyy with the current date in the

same format. If the journal date is greater than today's date, it should display an error.

However, the condition is being applied even though the journal date is less than today's date.

Correct behavior:

If the journal date > current date, display an error.

If the journal date < current date, proceed without any issue.

Here is the code I tried:

function formatCurrentDate() {
    let currentDate = new Date();
    let month = String(currentDate.getMonth() + 1).padStart(2, '0'); // Months are 0-based
    let day = String(currentDate.getDate()).padStart(2, '0');
    let year = currentDate.getFullYear();
    return `${day}/${month}/${year}`;
}

let currentDateFormatted = formatCurrentDate(); 09/01/2025 dd/MM/yyyy
let pJournalDate = $("#txt_journal_date").val(); //31/12/2024 dd/MM/yyyy

if (pJournalDate > currentDateFormatted) {
    ModelSuccessDanger(
        MYLang.ReturnLang() === "ar-KW" ? '???? !!' : 'Warning !!',
        MYLang.ReturnLang() === "ar-KW" ? "??? ?? ?? ???? ????? ????? ???? ?? ????? ?????" : "You should specify a date for closing that is not greater than the current date",
        '0',
        null
    );
    return false;
}

 


Answers (2)