Andre
Andre

Reputation: 1322

AngularJS date filter incorrect time

I have a date object formatted to isotime. I'm using the |date filter to nicely format this in my template, but it incorrectly changing the time.

This Code...

    <td>[[ user.last_online | date:'dd MMM yyyy - hh:mm a' ]]</td>
    <td>[[ user.last_online ]]</td>

Results in this...

enter image description here

Now I know that the 1 hour difference is because of the Timezone, this is what I'm expecting. The Minutes however is incorrect.

In the first row, 13 minutes gets added when the filter is applied. In the second row, 5 minutes gets added.

Not only are these two values wrong, but they are also inconsistent.

Upvotes: 2

Views: 8713

Answers (3)

zs2020
zs2020

Reputation: 54514

If you check ISO8601, you can see the correct time stamp format is

yyyy-MM-ddTHH:mm:ss.SSSZ

The milliseconds should consists of 3 digits. I did a simple test and you can see after correcting the milliseconds part, the dates will be rendered correctly.

{{"2013-08-09T15:36:31.764546+02:00" | date:'dd MMM yyyy - hh:mm a'}}<br />
{{"2013-08-09T15:34:14.318753+02:00" | date:'dd MMM yyyy - hh:mm a'}}<br />
{{"2013-08-09T15:36:31.764+02:00" | date:'dd MMM yyyy - hh:mm a'}}<br />
{{"2013-08-09T15:34:14.318+02:00" | date:'dd MMM yyyy - hh:mm a'}}<br />

The result is

09 Aug 2013 - 09:49 AM
09 Aug 2013 - 09:39 AM
09 Aug 2013 - 09:36 AM
09 Aug 2013 - 09:34 AM

Demo

Update

Python's datetime.isoformat() return the time with microseconds 0 <= microsecond < 1000000. Angularjs doesn't like, though this format is correct according to ISO8601, since ISO8601 only requires one or more digits representing a decimal fraction of a second

So I guess you can use strftime to format it.

Upvotes: 4

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241525

Your dates are correctly formatted. ISO8601 doesn't actually require any particular number of decimals. There could be anywhere from zero to 7 decimals or more. If you look at an actual copy of the ISO8601 spec, section 4.2.2.4 says the following:

... with as many digits as necessary following the decimal sign ...

There are a few older browsers where this mattered when passed directly to the new Date() constructor, but AFAIK those were consider bugs and were fixed.

You are experiencing a bug in AngularJS, which was fixed in version 1.1.5. You can find it referenced in their change log as follows:

date filter: correctly format dates with more than 3 sub-second digits (4f2e3606)

Upvotes: 1

EpokK
EpokK

Reputation: 38092

I think the value of user.last_online is incorrect or has a bad format. If you check ISO8601, you can see the correct time stamp format is:

yyyy-MM-ddTHH:mm:ss.SSSZ

My plunker

Upvotes: 2

Related Questions