My page was working fine and displaying dates and times as expected until I had to add new Date()
to avoid the momentjs deprecation warning. Now my date is 5 hours off what it should be.
How can I fix this? vd
and v
should both be 12:14:26 and, in this instance, fn should be "Seconds ago".
Here is the full code:
var k = key;
var v = obj[key];
var vd = Date.parse(obj[key]));
if (moment(vd).isValid()) {
var fn = moment(vd).fromNow();
v = fn;
}
return {
name: k,
value: v
};
I tried the following based on this post, but it brought back the momentjs deprecation warning:
var vd = new Date(Date.parse(obj[key])).toUTCString();
(and still didn't work)
__________________________________________________________________________________
Threaded comments would be cool
What happens if you just run
moment(v)
? Should work. – Maggie Pint
It does work, but I get the deprecation warning.
I just looked closer. The format of V is ISO8601 with offset, so you shouldn't be getting a deprecation if you just call
moment(v)
. If you are, trymoment(v, moment.ISO_8601)
– Maggie Pint
Again, works, but I still get the deprecation warning.
instead of
var vd = new Date(Date.parse(obj[key])).toUTCString();
did you tryvar vd = new Date(Date.parse(obj[key])).toISOString();
? or even simpliervar vd = Date.parse(obj[key])
– user3
With .toISOString()
it outputs "Invalid Date". var vd = Date.parse(obj[key])
Is what I had originally, it works as expected but I get the warning.
Your v string appears to use ISO8601 format already? Maybe append the Z, or ask moment to recognise it automatically as UTC when timezone info is missing – Bergi
I just tried var vd = new Date(Date.parse(obj[key] + "Z"));
and it works!