0

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.

7
  • Are you stripping time out of the dates? Accounting for timezone differences? What do you get if you console log the dates inside your if statement? Commented Jun 3, 2016 at 13:12
  • 1
    If you have a lot of date logic to include and can use plugins - I'd recommend momentjs Commented Jun 3, 2016 at 13:12
  • I have already printed the log messages.today is giving the todays date time and $scope.date is my selected date. Commented Jun 3, 2016 at 13:15
  • @ tymeJV : Here todays date is including while comparing but i need to compare with future date instead of todays date for that alert message. Commented Jun 3, 2016 at 13:17
  • string manipulation of dates is risky and error-prone, which is why everyone's knee-jerking moment.js at you. That's not the source of your problem here, though; you have an off-by-one error on the month. Commented Jun 3, 2016 at 13:22

3 Answers 3

2

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.");
    }

Sign up to request clarification or add additional context in comments.

6 Comments

I'm not sure whether $scope.date is MM-DD-YYYY or DD-MM-YYYY -- this is the problem with ambiguous date formats. Whichever of those is the month, subtract one from it, that should work.
@subhra - then it'd be very helpful if you can create a short plunckr replicaing the problem.
@DanielBeck : suppose i selected 03-06-2016 and todays date is also 03-06-2016 how it became greater .Please check your code.
I have added more demonstration code and explanation; @subhra I suspect the fact that new Date() includes the time of day may also have been an issue for you here...
@DanielBeck : Dont compare with tomorrows date.Because user will select any date that will compare with only todays date.
|
0

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')) { ...

11 Comments

but my code is working but not working as per my requirement.
@subhra in other words, your code is not working correctly. The above solution is intended to make it work.
@DmitriyKhudorozhkov momentjs is great, but would not work as written in your answer: moment($scope.date) throws 'Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release' and returns an "Invalid date" because $scope.date is not in a format momentjs accepts.
@DanielBeck you can use it this way, then: moment($scope.date, "MM-DD-YYYY");
@subhra that's rather presumptuous. If you're not willing to put in the bare minimum effort to help us understand where you're going wrong, people are unlikely to want to continue trying to help you. Just saying "it's not working" without explanation doesn't give us much to go on.
|
0

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.

1 Comment

Make sure you using AngularJS. If not, update $scope.date to a JavaScript variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.