3

I have a angular app in which i am storing records to firebase with the timestamp using firebase.database.ServerValue.TIMESTAMP

Now i want to check if the timestamp added to the database exceeds five minutes.

I tried using moment in my Component but it just dosent work . It is giving false data . I guess the data stored in Firebase is in epoch where as i am in india so how to make moment work in Angular .

I am using Angular 2 Moment.

This is what i tried

import * as moment from 'moment/moment';

      edit(i:number){
        const val = moment.unix(this.comments[0].createdAt);// the value is a epoch time that is the standard that is stored in firebase
        const present = moment().unix();

        console.log(moment.duration(val.diff(present)));
      }

The Value of this.comments when using it in date

let ts = new Date(this.comments[0].createdAt * 1000); = Wed Aug 05 49474 20:09:20 GMT+0530 (India Standard Time)

where as for now its like -- Tue Jul 04 2017 14:56:46 GMT+0530 (India Standard Time)

Please help why its the comments data not is sink with the time it should be july 3 as it was added on that day on firebase

Rahul
  • 427
  • 3
  • 7
  • 16

2 Answers2

2
const now = new Date();
const ts = new Date(this.comments[0].createdAt);
const diff = now.getTime() -  ts.getTime();

console.log(diff > 5 * 60 * 1000); // this will give you true or false
Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
0

You can use the javascript Date object to do the math, i.e.

let now = new Date();
let ts = new Date(this.comments[0].createdAt * 1000);
let diff = now.getTime() - ts.getTime();

console.log(diff > 5 * 60 * 1000);
ruedamanuel
  • 1,930
  • 1
  • 22
  • 23
  • its dosent show the right info the message is stored last day but still the console .log says false where as it is more than mins should say true right – Rahul Jul 04 '17 at 09:27
  • When i console log them ts - Wed Aug 05 49474 20:09:20 GMT+0530 (India Standard Time) and now - Tue Jul 04 2017 14:56:46 GMT+0530 (India Standard Time) – Rahul Jul 04 '17 at 09:28
  • yes, because when you log a date object it applies toString() to itself, which is a human readable string, that's why you're using .getTime() in the diff so that it transforms the date into millis – ruedamanuel Jul 05 '17 at 15:24