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;
}
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 8, 2025, 11:19 PM EST
Hello,
You can try this one,
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}`; } function parseDate(dateString) { // Convert dd/MM/yyyy to a Date object let [day, month, year] = dateString.split('/').map(Number); return new Date(year, month - 1, day); // Month is 0-based } let currentDateFormatted = formatCurrentDate(); let pJournalDate = $("#txt_journal_date").val(); if (pJournalDate) { let currentDate = parseDate(currentDateFormatted); let journalDate = parseDate(pJournalDate); if (journalDate > currentDate) { 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; } }Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 9, 2025, 3:23 AM EST
It looks like the issue is due to comparing the dates as strings rather than as actual date objects. When you compare strings, "31/12/2024" is considered greater than "09/01/2025" because string comparison is lexicographical.
To fix this, you need to convert the dates from strings to
Dateobjects and then compare them. Here's how you can do it: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}`; } function parseDate(dateString) { let parts = dateString.split('/'); // Note: months are 0-based in JavaScript Date object return new Date(parts[2], parts[1] - 1, parts[0]); } let currentDateFormatted = formatCurrentDate(); // 09/01/2025 dd/MM/yyyy let pJournalDate = $("#txt_journal_date").val(); // 31/12/2024 dd/MM/yyyy let currentDate = parseDate(currentDateFormatted); let journalDate = parseDate(pJournalDate); if (journalDate > currentDate) { 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; }In this code, the
parseDatefunction converts the date string indd/MM/yyyyformat to aDateobject. This way, you can accurately compare the dates.Give this a try and let me know if it resolves your issue! ??