angular moment time difference calculation error

289 views Asked by At

I'm trying to calculate the difference in time between NOW and a time in the past. I won't go too in depth here as far as how my web system (by API) works, but essentially I am returning an array of publication times.

What I want to achieve:

NOW (UTC) - PUBLICATION (UTC) = DIFFERENCE IN SECONDS

The publication time I am returning from my system is already in datetime format YYYY-MM-DD HH:MM:SS and in UTC time.

I am using this angular-2 module: https://www.npmjs.com/package/angular2-moment

The calculation that is actually being performed at the moment is:

PUBLICATION (UTC) - NOW (NOT IN UTC) = DIFFERENCE IN SECONDS

My HTML:

<td><time>{{nextDay | amDifference: (i['pubDate']):'seconds' : false}}</time></td>

Example of what I want to do:

right now UTC (2018-07-18 14:10:55) - publication time utc (2018-07-18 12:32:51) = 5884 seconds

It must be a matter of switching the piping around in the html, but I can't find any documentation on how the calculation is made, but I replicated my issue in Python and found that it is taking the wrong times from each other. All help greatly appreciated!

1

There are 1 answers

11
dAxx_ On

So its appear that you just calculate the current time in the wrong way, So I've tried with the moment npm package to get the right result and it works fine.
Try this:

let currentTime = new Date().toUTCString();

and in your HTML:

{{currentTime | amDifference: '2018-07-18 14:23:45' :'seconds' : true}}

I've tried that and it works, In your case, just change the '2018-07-18 14:23:45' to your actual date of the PUBLICATION date.

--UPDATE
You should create the currentTime variable in the ngOnInit(), it will create for one time when the component loaded. If you want a dynamic timestamp, lets say every 1 sec this date should update, You need to create Observable and set Interval for every 1000ms to update the time, so you will always get the current time.

--UPDATE 2
check this working example and compare: Example