-1

I need to calculate the difference between two dates to try find the end of services of the employee

favorite Possible Duplicate: How do I get the difference between two Dates in ionic3?

How to get the difference between two dates using ionic3.I need the exact day difference between those two dates.Here is my code

This is a Ts file

 <!-- begin snippet: js hide: false console: true babel: false -->

    <!-- language: lang-js -->

        import { Component } from '@angular/core';
        import { NavController, DateTime,NavParams } from 'ionic-angular';
        import { IfStmt } from '@angular/compiler';
        import { DatePipe, DOCUMENT, getLocaleMonthNames } from '@angular/common';
        import { daysInMonth, parseDate } from 'ionic-angular/util/datetime-util';
        import { toBase64String } from '@angular/compiler/src/output/source_map';
        import { Console } from '@angular/core/src/console';



        @Component({
          selector: 'page-about',
          templateUrl: 'about.html'
        })
        export class AboutPage {

        datef;
        datett;
        dated1
        dated2;
        timeDiff;

        typeOfDateYear;
        typeOfDateMonth;

          constructor(public navCtrl: NavController) {

          }

        performFunction(type){

        var d1 = parseFloat(this.dated1);  
        var d2 = parseFloat(this.dated2);  
        var f = parseInt (this.datef);
        var tt = parseInt (this.datett);


        console.log(type);

        if(type =='a'){


        this.typeOfDateYear = tt - f 

        this.typeOfDateMonth = this.typeOfDateYear / (24* 3600* 1000)


        }
        }
        }

Any suggestion?

  • 1
    Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – P Ackerman Jun 30 '18 at 16:49
  • How do you define “exact day difference”? What is the type and value of *dated1* and *dated2*? Without that information, an answer is just a guess. – RobG Jun 30 '18 at 23:21

1 Answers1

0

You can refer following example using moment:

1) Install via NPM dependencies:

npm install moment

2) Import in your Typescript file:

import * as moment from 'moment'

3) Typescript file:

let date1 = moment("2014-10-1", "DD-MM-YYYY");
let date2 = moment("2015-12-1", "DD-MM-YYYY"); 
let duration = moment.duration(date1.diff(date2));
let days = duration.asDays();

Moment provides the feature for date difference in many ways.

Pradnya Sinalkar
  • 1,116
  • 4
  • 12
  • 26