How to subtract a date and time from another date and time in Ionic 4?

Asked

Viewed 173 times

-1

I need to pick two dates and time, already defined in the template like this:

<ion-datetime name="ent" [(ngModel)]="ent" displayFormat="DD MMM, HH:mm"></ion-datetime>,

In the class I defined so: ent:Date=new Date();

but I wonder if anyone knows a way to subtract these values so that, for example, I can see how long and days a car has been in the parking lot. Thanks in advance!

1 answer

0


you can use the library Moment.js:

        var data1 = moment('29/04/2019 00:00', "DD/MM/YYYY hh:mm");
        var data2 = moment('30/04/2019 02:00', "DD/MM/YYYY hh:mm");
        var diferenca = data1.diff(data2, 'hours');

        console.log(diferenca); // 2
//
        var data1 = moment('29/04/2019 00:00', "DD/MM/YYYY hh:mm");
        var data2 = moment('30/04/2019 02:00', "DD/MM/YYYY hh:mm");
        var diferenca = data1.diff(data2, 'days');

        console.log(diferenca); // 1


  • I got it, I used the <ion-datatime> even if it takes the string value, I stored it in my class variable through a [(ngModel)] and then I made a button that calls a function in the class with the (click)="nomeDaFuncao()"

  • Check out the class and the complete project on my Github: Full project: https://www.github.com/joaopauloexcel/aplicativo-estacionamento Class: https://github.com/joaopauloexcel/aplicativo-estacionamento/blob/master/applicationStationion/src/app/new-item/new-item.page.ts

Browser other questions tagged

You are not signed in. Login or sign up in order to post.