1

I have following code. I don't want to use momentjs and want to compare the date with the below codes.

var i = $filter('date')(Date1, 'short');
var j = $filter('date')(Date2, 'short');
var k = j - i;
if (k<0) 
{
   return true;
}
else 
{
   return false;
}

I want to compare my Date1 with Date2 and want to display which one is greatest. But it's not working. So anyone can help me here.

7
  • may be help u stackoverflow.com/questions/30577172/… Commented Mar 1, 2016 at 12:09
  • return (Date2 - Date1) < 0? Commented Mar 1, 2016 at 12:11
  • Not helpfull.. i don't want to code more in length. Commented Mar 1, 2016 at 12:12
  • The Comment from hadiJZ is helpful but my suggestion is Use Momentjs which handles all these things pretty good and Precise Commented Mar 1, 2016 at 12:12
  • can anyone modify this code?? Commented Mar 1, 2016 at 12:13

2 Answers 2

2

I changed your code. You need to use Date.parse().

var i = $filter('date')(Date1, 'short');
var j = $filter('date')(Date2, 'short');
var k = Date.parse(j) - Date.parse(i);
if (k<0) 
{
   return true;
}
else 
{
   return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

 var i = $filter('date')(Date1, 'short');
 var j = $filter('date')(Date2, 'short');

Now directly compares both of them withour any third variables else as abpve mentioned you need to use parseInt

 if (i > j) 
   return true;
  return false;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.