user5443928
user5443928

Reputation:

Can not compare dates properly using Angular.js/Javascript

I need one help.I am unable to compare the selected date with today's date using Angular.js or JavaScript.I am explaining my code below.

var today=new Date();
console.log('2 dates',today,$scope.date);

From the above console i am getting the output like below.

2 dates Fri Jun 03 2016 18:29:16 GMT+0530 (India Standard Time) 03-06-2016

if(today > new Date($scope.date.split("-").reverse().join(","))){
    alert('Please select  future delivery date');
}

Here i need suppose my selected date is 03-06-2016 and today's date is 03-06-2016 the alert prompt should not come.But in my case its not happening like this.This alert message is coming also if selected date is 03-06-2016. Here i need the alert message should come if selected date is more than today's date only.Please help me.

Upvotes: 0

Views: 172

Answers (3)

Daniel Beck
Daniel Beck

Reputation: 21475

Months are zero-indexed when constructing Dates:

console.log(new Date(2016,06,03));

"2016-07-03T04:00:00.000Z" // you expected June, but got July.

Note also that that is set to midnight: any new Date() run during the same day will be greater than that value.

String manipulation of dates is risky and error-prone, which is why everyone's knee-jerking momentjs at you (it is in fact a very good library and worth using if you're doing much date manipulation).

At the least, you may want to consider using a less ambiguous date format in $scope.date, but if you're stuck with that, in this case, you need to subtract 1 from the month when constructing a Date from it:

// this sample code will only work correctly on June 3 2016 :)

    var $scope = {
      date: "03-06-2016", // I am assuming DD-MM-YYYY format here
      tomorrow: "04-06-2016"
    };
    var today = new Date();

    // test against today:
    var dArr = $scope.date.split('-');
    var testDate = new Date(dArr[2],dArr[1]-1,dArr[0]);
    
    if (today > testDate) {
      console.log("today is greater than testDate."); 
     /* Note that anytime during today will be greater than the zero
        o'clock constructed from $scope.date.  If what you really want
        is to alert only on the following day, then add 1 to the day 
        of $scope.date when converting it to a Date(). */
    }

    // now test against tomorrow:
    dArr = $scope.tomorrow.split('-');
    testDate = new Date(dArr[2],dArr[1]-1,dArr[0]);
    
    if (today < testDate) {
      console.log("today is less than (tomorrow's) testDate.");
    }

Upvotes: 2

Dmitriy Khudorozhkov
Dmitriy Khudorozhkov

Reputation: 1623

Don't compare JavaScript dates using native < or > operators; install and use moment.js - it will handle all the difficulties for you:

if(moment(new Date()).isAfter(moment($scope.date, "MM-DD-YYYY"), 'day')) { ...

Upvotes: 0

Huy Chau
Huy Chau

Reputation: 2240

Convert to timestamp to compare it:

$scope.date = '2016-06-01';
var currentDate = new Date().valueOf();
var date = new Date($scope.date).valueOf();

if (currentDate > date) {
  // something
}

It you use multi timezone, please convert both to a timezone.

Upvotes: 0

Related Questions