0

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!

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
dataviews
  • 2,466
  • 7
  • 31
  • 64
  • Why are you perform a calculation with other time zone (not UTC) if you want to achieve the final result in UTC ? – dAxx_ Jul 18 '18 at 15:27
  • @dAxx_ What you mean? – dataviews Jul 18 '18 at 15:29
  • You said: PUBLICATION (UTC) - NOW (NOT IN UTC) = DIFFERENCE IN SECONDS, which your data is in UTC, why do you calculate the NOW timestamp not in UTC – dAxx_ Jul 18 '18 at 15:30
  • @dAxx_ That's precisely what I am trying to achieve....I clearly state that I want to subtract both times IN UTC – dataviews Jul 18 '18 at 15:31
  • How do you get the current time? – dAxx_ Jul 18 '18 at 15:44
  • @dAxx_ Do you see my HTML above? That is how I'm getting the current date and time, using their way "today"...Everything is correct on the calculation except the modules today function is returning my local time NOT in UTC format, which is what I need to make the proper calculation. – dataviews Jul 18 '18 at 15:48
  • The [documentation](https://www.npmjs.com/package/angular2-moment#amdifference-pipe) seems to indicate that "today" should be after the pipe symbol... – Heretic Monkey Jul 18 '18 at 16:56
  • @HereticMonkey it's not right in my case – dataviews Jul 18 '18 at 16:58
  • What's not right? The code you've shown shows `(i['pubDate'])` in the position of what the documentation shows should be the current date. I'm assuming that is the "publication date". It's not clear what `nextDay` is in your example. Try putting `(i['pubDate'])` before the pipe, set `nextDay` to `Date.now().toISOString()` and put it in the place where `(i['pubDate'])` was. See what happens. – Heretic Monkey Jul 18 '18 at 17:06
  • I've already tried that. I am still getting LOCAL time and not LOCAL time in UTC format. I have found another pipe that they offer which is good for what I'm looking to do. I am using the amTimeAgo now; HOWEVER, the local time is still being calculated as local and NOT UTC. Do you know of a way to manually edit the library to use UTC instead of just local? @HereticMonkey – dataviews Jul 18 '18 at 17:11
  • To tell the truth, I have some invalid code in my last comment, so I don't think you've actually tried *exactly* that. Set `nextDay` to `new Date().toISOString()`. That will produce something like "2018-07-18T17:17:16.829Z" -- the Z at the end should make moment use UTC. – Heretic Monkey Jul 18 '18 at 17:19
  • @HereticMonkey and if you read the answer below that another user provided, I have tried both ways. This solution is not working. I can't figure out as to why the function is not accepting the UTC time format. The other work around that I am thinking of is editing the amTimeAgo module to explicitly use UTC format. – dataviews Jul 18 '18 at 17:26
  • I see no reference to `toISOString` in the answer or the comments. If you don't want to try what I've suggested, I will leave you to figure it out. – Heretic Monkey Jul 18 '18 at 17:28
  • @HereticMonkey I'm telling you now that I've tried both ways. I've tried toUTCString as well as the toISOString and they do not send in the proper UTC formatted time. – dataviews Jul 18 '18 at 17:31
  • @HereticMonkey If you can provide where I can manually edit the local time calculation being used for amTimeAgo that would be very helpful at this point. For the amTimeAgo, I pass in my i['pubDate'] variable at which point this time is being subtracted from the local time. However, for this particular pipe, they do not offer a way to direct the local time to be in UTC format. My thought process is to find out where the local time is being stored for amTimeAgo, and manually edit it to always use UTC format. – dataviews Jul 18 '18 at 17:36

1 Answers1

0

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

dAxx_
  • 2,210
  • 1
  • 18
  • 23
  • where are you defining " let currentTime = new Date().toUTCString(); " ? – dataviews Jul 18 '18 at 16:09
  • in app.component ?? – dataviews Jul 18 '18 at 16:10
  • @dataviews, I've add more explanation. Good luck ! – dAxx_ Jul 18 '18 at 16:13
  • I still get a negative time – dataviews Jul 18 '18 at 16:16
  • I should mention, I am using Angular 5 and typescript btw – dataviews Jul 18 '18 at 16:21
  • Yes of course you are. Try to switch the 2 variables: the currentTime and your publication time, in one order its negative and in the other positive, its simply subtract the seconds, I've checked it and it works. – dAxx_ Jul 18 '18 at 16:24
  • even if it is positive, it is not yielding the right time still. at the moment, the difference is 503 +/- This is not correct, because my current UTC time is 2018-07-18 16:25:31 and the publication was 2018-07-18 12:32:51. This should yield 13960 at this very moment. – dataviews Jul 18 '18 at 16:26
  • It's possible that the html isn't even reading the currentTime variable properly. If I take that out and put in something fake like fakeVariableName, the calculation is still being made with the current time. – dataviews Jul 18 '18 at 16:31
  • they have another directive that may be better actually if we can figure out this local UTC time issue. They have amTimeAgo which is better for my case – dataviews Jul 18 '18 at 16:39
  • I've edit again with working example, I dont know, it is working for me, compare with the example, really nothing special, maybe something in your end break it, I dont know. – dAxx_ Jul 18 '18 at 16:40
  • I think what needs to happen is that we need to direct the amTimeAgo pipe to use the UTC string. How can that be achieved? At that point, we must update the module it sounds like – dataviews Jul 18 '18 at 16:43