0

I am using moment.js in my application, I pass my date in a particular format to moment.js and it compares it with the system time and give me the pretty time format, like this:

moment(time, "YYYY-MM-DD HH:mm:ss.SSSSSS").fromNow();

Here the time is given by me as a timestamp in the specified format.

My problem is that fromNow() function takes the system date and not the server date. So, if a user uses his phone with 5 min delay time in his system he will get 4 min ago as the latest updated cards.

I want to compare "time" that I pass with the server time, that also will be passed by me.

Gargaroz
  • 313
  • 9
  • 28
vaibhav
  • 762
  • 2
  • 12
  • 34
  • While I agree the question in the title is a duplicate, the body is actually asking something different. Maybe the title could just be improved to match the question asked (e.g. "How to format durations between 2 dates in moment.js") – tavnab Jun 12 '15 at 15:15

1 Answers1

3

You could pass in 2 dates from your server: the existing timestamp, and the server's "now" timestamp.

Then (assuming your server passes in its timestamp in the same format) you can use .from() instead of .fromNow() do:

moment(time, "YYYY-MM-DD HH:mm:ss.SSSSSS").from(serverTime, "YYYY-MM-DD HH:mm:ss.SSSSSS");

If this is the only thing you're using the timestamp for, you could just have the server calculate & return the delta directly (let's call it serverDeltaMs) & format that using moment.duration(serverDeltaMs).humanize().

tavnab
  • 2,594
  • 1
  • 19
  • 26