How to subtract two angularjs date variables

后端 未结 7 1089
说谎
说谎 2020-12-01 21:53

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

7条回答
  •  一向
    一向 (楼主)
    2020-12-01 22:25

    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();
    

提交回复
热议问题