1

i have 1 problem, i wanna compare two dates between date born and today, the condition is date born must be less than today date, it worked at first, but if i pick another date from datepicker which is less than today, its still pop up alert. And it not ok. Anyone know why this this happened?

    function submitForm() {
        
        var dateborn = document.getElementById('dateborn').value;   
        var today = document.getElementById('today').value;
        
        if(dateborn != '')
        {           
            if(dateborn > today)
            {
            swal("Oops", "Date born must less than date today", "error");       
            return false;
            }
        }

            
    }
  • 1
    `.value` returns a string. You're comparing two strings, and not two dates. – Rickard Elimää Aug 09 '21 at 06:10
  • 1) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse 2) https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Aval Sarri Aug 09 '21 at 06:13
  • Since I guess you will now need to know how to properly compare them, convert the string into a date like [this](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) and compare afterwards. – bluejambo Aug 09 '21 at 06:13
  • thanks for the solution, really appreciate it – confused_cat Aug 11 '21 at 07:42

3 Answers3

1

If I were you I can use the timestamp to compare 2 dates.

function submitForm() {
        
        var dateborn = document.getElementById('dateborn').value;   
        var today = document.getElementById('today').value;
        const time1 = new Date(dateborn).getTime();
        const time2 = new Date(today).getTime(); // or new Date().getTime();
        if(dateborn != '')
        {           
            if(time1 > time2)
            {
            swal("Oops", "Date born must less than date today", "error");       
            return false;
            }
        }
Adem yalçın
  • 176
  • 1
  • 6
0

Try this

function submitForm() {
    
    var dateborn = document.getElementById('dateborn').value;   
    var today = document.getElementById('today').value;
    
    if(dateborn != '')
    {           
        if(Date(dateborn).getTime() > Date(today).getTime())
        {
        swal("Oops", "Date born must less than date today", "error");       
        return false;
        }
    }

        
}
0

As for your question why this happened. i think your date picker does not give the date in ISO format. if your value is formatted like: YYYY-MM-DD (from biggest to smallest) you can compare it as a string. You can see this from this fiddle i made: JSfiddle

Does your date picker look like this:

<input type="date" id="d2" onChange="change(this)"/>
Jesper
  • 1,007
  • 7
  • 24
  • thanks for the solution, really appreciate it, Well btw i just only have 1 date picker which is the dateborn, while today value i get it from hidden input. – confused_cat Aug 11 '21 at 06:08
  • @confused_cat If today is truly only for getting the current date.. Why not get it from javascript with `new Date()`? – Jesper Aug 11 '21 at 06:21