steiacopi
steiacopi

Reputation: 27

Check if input date is in given range

I have a form with a simple input type. I'm trying to define a function to check if the date is no more than 6 months older compared to date provided by the input type. I know i have to convert the String provided by the input in a Date object to make this comparison and then work on get methods of Date object, but i can't figure out how to do this.

Current code:

$scope.compareDates = function(d1) {
        d1 = new Date(d1); //convert String into date Object                
        var d = new Date(); // today date           
        d.setYear(d.getFullYear());
        d.setMonth(d.getMonth() - 6);           

        if(d1 > d ) {
            console.log("ok");
        } else {
            console.log("error");               
        }
    }   

EDIT:

I'm sorry, i forgot to add my input. Here it is:

<input class="form-control" type="text" placeholder="gg/mm/aaaa" ng-model="sStartDate" ng-change="change()">

Angular Controller:

$scope.sStartDate = '';

$scope.change = function(){

    var startDt = $scope.sStartDate;
    $scope.compareDates(startDt);

}

Upvotes: 0

Views: 1283

Answers (4)

Rajesh
Rajesh

Reputation: 24925

If you are comparing dates and not time, make sure to reset time in your date object using d1.setHours(0,0,0,0).

When you do new Date(), output is a datetime object, but when you do new Date(2015,6,19), only date is assigned and time is set to 00:00:00.

Following code depicts the same:

function compareDates(d1,d2){
  return +d2 > +d1;
}

function compareDatesWithHoursReset(d1,d2){
  d1.setHours(0,0,0,0);
  d2.setHours(0,0,0,0);
  return +d2 > +d1;
}

function main(){
  var d1 = new Date();
  var d2 = new Date(2015, 6, 19);
  d1.setMonth(d1.getMonth() - 6)
  
  console.log(d1);
  console.log(d2);
  
  console.log(compareDates(d2,d1))
  console.log(compareDatesWithHoursReset(d2,d1))
}

main();

Upvotes: 0

epascarello
epascarello

Reputation: 207521

If I am reading your code correctly, your date format is days/month/year which is not valid format. You need to swap the month and days.

var parts = d1.split(),
    dateStr = parts[1] + "/" + parts[0] + "/" parts[2],
    d1 = new Date(d1),
    d = new Date();
d.setMonth(d.getMonth() - 6);
if(d1 > d ) {
    console.log("ok");
} else {
    console.log("error");               
}

Upvotes: 2

Daniel Nalbach
Daniel Nalbach

Reputation: 1233

I used momentjs for this in our app. It makes this process very easy and smooth.

// is6MonthsOld will be a boolean value
var is6MonthsOld = moment(d1).isBefore(moment().subtract(6, 'months'));

Upvotes: 0

Yoplaboom
Yoplaboom

Reputation: 554

What's about using d.getTime()?

$scope.compareDates = function(d1){
        d1 = new Date(d1); //convert String into date Object                
        var d = new Date(); // today date           
        d.setMonth(d.getMonth() - 6);           

        if(d1.getTime() > d.getTime() ) {
            console.log("ok");
        } else {
            console.log("error");                
        }
    }   

Regards.

Upvotes: 1

Related Questions