0

I am facing one issue. I need to compare the given date with the today date using Javascript. I am explaining my code below.

function getCurrentDate(){
    var today = new Date();
    var dd = today.getDate();

    var mm = today.getMonth()+1; 
    var yyyy = today.getFullYear();
    if(dd<10) 
    {
        dd='0'+dd;
    } 

    if(mm<10) 
    {
        mm='0'+mm;
    } 
    today = dd+'-'+mm+'-'+yyyy;
    return today;
}

var givendate=new Date('19-01-2018');
var todaydate=$scope.getCurrentDate();
if (givendate >= todaydate) {
    console.log('bool',true);
}else{
    console.log('bool',false);
}

Here I should get the result true but here I am getting the console message as false. Please help me to resolve this issue.

4 Answers4

1

As you said in the comments givendate is "Invalid Date" - that's why you have false.

So, before parsing a date, check how to detect invalid date

new Datetries to parse following ISO 8601 format. Quoting:

The formats are as follows. Exactly the components shown here must be present, with exactly this punctuation. Note that the "T" appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601.

   Year:
      YYYY (eg 1997)
   Year and month:
      YYYY-MM (eg 1997-07)
   Complete date:
      YYYY-MM-DD (eg 1997-07-16)
   Complete date plus hours and minutes:
      YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
   Complete date plus hours, minutes and seconds:
      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
   Complete date plus hours, minutes, seconds and a decimal fraction of a
second
      YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:

     YYYY = four-digit year
     MM   = two-digit month (01=January, etc.)
     DD   = two-digit day of month (01 through 31)
     hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
     mm   = two digits of minute (00 through 59)
     ss   = two digits of second (00 through 59)
     s    = one or more digits representing a decimal fraction of a second
     TZD  = time zone designator (Z or +hh:mm or -hh:mm)

Make sure you pass a correctly formatted date when constructing a date object. All the details you need on the subject can be found in JS MDN, on the Date.parse topic

Adelin
  • 7,809
  • 5
  • 37
  • 65
1

The below code works. Like the others said, you need to ensure that you are specifying the date in the proper format as expected by the Date constructor.

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>
<p id="today"></p>
<p id="text"></p>

<script>
today = new Date();
jan19 = new Date('2018-01-19')
document.getElementById("demo").innerHTML = jan19;
document.getElementById("today").innerHTML = today;
if (today < jan19) {
    document.getElementById("text").innerHTML = 'true';
}
else {
    document.getElementById("text").innerHTML = 'false';
}
</script>

</body>
</html>
Aditya Gupta
  • 633
  • 1
  • 4
  • 11
1

If you want to continue with your current date format(dd-MM-yyyy), then you may need to split the string by - and rearrange accordingly to make a valid date.

JS Code

function getCurrentDate() {
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth() + 1;
        var yyyy = today.getFullYear();
        if (dd < 10)
            dd = '0' + dd;
            if (mm < 10)
                mm = '0' + mm;
            today = dd + '-' + mm + '-' + yyyy;
            return today;
        }

    var todayDate = getCurrentDate().toString();
    var givenDate = '19-01-2018';

    var todayDateArray = todayDate.split('-');
    var givenDateArray = givenDate.split('-');
    
    todayDate = new Date(todayDateArray[2] + '-' 
                       + todayDateArray[1] + '-' 
                       + todayDateArray[0]);
    
    givenDate = new Date(givenDateArray[2] + '-' 
                       + givenDateArray[1] + '-' 
                       + givenDateArray[0]);
    
    if(givenDate >= todayDate)
        console.log('bool', true);
    else
        console.log('bool', false);
Ullas
  • 11,450
  • 4
  • 33
  • 50
  • I must admit, that looks quite ugly. Also, you're splitting the same string 3 times, instead of splitting it once and use the resulting array 3 times. – Adelin Jan 17 '18 at 08:08
  • @Adelin : It's possible to split the string and store in a single array variable – Ullas Jan 17 '18 at 08:10
  • Of course it's possible - but your code doesn't reflect that – Adelin Jan 17 '18 at 08:11
0

This works for me. If you don't want a custom formatted date then you can use this.

function getCurrentDate(){
    var today = new Date();
    return today;
}

var givendate=new Date("2018-01-19");
var todaydate= getCurrentDate();
console.log(givendate);
console.log(todaydate);
if (givendate >= todaydate) {
    console.log('bool',true);
}else{
    console.log('bool',false);
}
DragonBorn
  • 1,809
  • 5
  • 20
  • 44