Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract two angularjs date variables

I am fairly new to angularjs, but here it goes. I am able two dates through angularjs in the form dd/mm/yyyy, but what I need to do is somehow subtract the two dates to get the difference in days between the two. I created a jquery function to do this, but I don't know how to pass in the two dates to the function. So I was wondering if there was a different way to go about this?

I am trying to set up a trigger system depending on the number of days in between the two dates for certain things to be stylized. For example if it's within 10 days I want it to use style 1 and if its within 20 days use style 2 and so on.

like image 411
user1399078 Avatar asked Mar 08 '13 16:03

user1399078


2 Answers

Basic javascript way:

var d1 = new Date('01/16/2013');
var d2 = new Date('02/26/2013');
var milliseconds = d2-d1;
var seconds = milliseconds / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;

Using one of the Date libraries (such as moment.js):

var d1 = moment("01/16/2013");
var d2 = moment("02/26/2013");
var days = moment.duration(d2.diff(d1)).asDays();
like image 154
Stewie Avatar answered Sep 18 '22 09:09

Stewie


you simply convert date into timestamp and then subtract.

var Date1 = 08/16/2004;
var Date2= 10/24/2005;

var timestamp1 = new Date(Date1).getTime();
var timestamp2 = new Date(Date2).getTime();

var diff = timestamp1 - timestamp2


var newDate = new Date (diff);
like image 41
Pawan Singh Avatar answered Sep 21 '22 09:09

Pawan Singh