How to subtract two angular date variables

I am new to angularjs but here it goes. I can two dates through angularjs in the form dd/mm/yyyy , but I need to do so to subtract the two dates in order to get the difference between the days between them. I created a jquery function for this, but I don't know how to pass two dates to a function. So I was wondering if there is another way to do this?

I am trying to set up a trigger system based on the number of days between two dates to stylize certain things. For example, if he is within 10 days, I want him to use style 1 , and if he uses style 2 for 20 days, etc.

+7
source share
7 answers

I'm starting to work with corner nights too, but can't handle it by making properties available through your javascript view model?

For example, there is a style field that changes according to the date fields (for example, the style returns style 1 if the difference is 10 days) and, hopefully, updates will be distributed to the screen through the angularjs binding.

I think the correct term for this is a computed property or computed property

EDIT

Not sure if this is what you are looking for, but see a script, for example, to calculate the date and change the style based on the properties of the view model (scope)

scope.diff and scope.col are 2 properties to bind to

http://jsfiddle.net/chrismoutray/wfjv6/

HTML

 <script src="http://code.angularjs.org/0.10.4/angular-0.10.4.min.js" ng:autobind></script> <div ng:controller="ComputedPropertiesCtrl"> first date <input ng:model="firstdate"> second date <input ng:model="seconddate"> difference {{diff}} <div>when the difference is greater than 10 color changes to green</div> <div>eg set the second date to 15/01/2013</div> <div style="background-color:{{col}};"> State </div> </div> 

Js

 function ComputedPropertiesCtrl() { var scope = this; scope.firstdate = '01/01/2013'; scope.seconddate = '10/01/2013'; scope.data_before = []; scope.differenceInDays = function() { var dt1 = scope.firstdate.split('/'), dt2 = scope.seconddate.split('/'), one = new Date(dt1[2], dt1[1]-1, dt1[0]), two = new Date(dt2[2], dt2[1]-1, dt2[0]); var millisecondsPerDay = 1000 * 60 * 60 * 24; var millisBetween = two.getTime() - one.getTime(); var days = millisBetween / millisecondsPerDay; return Math.floor(days); }; scope.color = function() { return (scope.differenceInDays() > 10) ? 'green' : 'red'; }; scope.$watch('[firstdate, seconddate]', function(currScope,newVal,oldVal) { scope.data_before = oldVal; scope.diff = scope.differenceInDays(); }); scope.$watch('[firstdate, seconddate]', function(currScope,newVal,oldVal) { scope.data_before = oldVal; scope.col = scope.color(); }); } 

CSS

 h2 { position: fixed; right: 10px; top: 10px; color: red; background:white;z-index:1000; } input { width: 100px; } div { margin: 10px; padding: 10px; } 
+2
source

The main 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 (e.g. moment.js):

 var d1 = moment("01/16/2013"); var d2 = moment("02/26/2013"); var days = moment.duration(d2.diff(d1)).asDays(); 
+12
source

you just convert the date to a 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); 
+4
source

You can use angular-moment to calculate the difference using the amDifference filter:

Get the difference between two dates in milliseconds. Parameters: date, units and usePrecision. The default date corresponds to the current date. Example:

 <span>Difference: {{ dateFrom | amDifference : dateTo : 'days' }} days</span> 
+4
source

This works, and here are 2 flawless javascript date functions that you should never be without ...

 // Returns the days between a & b date objects... function dateDiffInDays(a, b) { var _MS_PER_DAY = 1000 * 60 * 60 * 24; // Discard the time and time-zone information. var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate()); var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate()); return Math.floor((utc2 - utc1) / _MS_PER_DAY); } // Calculate how many days between now and an event... function daysTill(e) { var eventE = new Date(e); var today = new Date(); return dateDiffInDays(today, eventE); } 
+2
source

At that moment, when the JavaScript library is really very useful and easy to use:


 var parsedServerOutTime = moment(serverOutTime, "HH:mm:ss"); var parsedServerInTime = moment(serverInTime, "HH:mm:ss"); var milliseconds= parsedServerOutTime.diff(parsedServerInTime) //default milliseconds var days = moment.duration(parsedServerOutTime .diff(parsedServerInTime )).asDays();// For days 

asWeeks(); asMonths(); asYears(); etc. etc. more details. http://momentjs.com/docs/

0
source

I tried the bottom one and it worked for me

 var selecteddate = new Date($rootscope.selectvalue); $scope.firstDate = selecteddate .getTime(); $scope.SecondDate = new Date().getTime(); 

the selected date is the one selected from the calender, and it comes from the parent area. the second date will be today. I will find the difference later using the diff moment.

 var differenceinDays=parseInt( moment.duration( moment($scope.firstDate).diff( moment($scope.SecondDate) ) ).asDays() ); 

I use parseInt because I want to return only an integer value

Hope this works

-one
source

All Articles