b0bu
b0bu

Reputation: 1230

Angular date filter not working

Not working or I'm not using it correctly. I have a date saved in mongodb (which is correct) as:

"2015-12-10T12:00:00.000Z"

I have an angular filter of date as:

date: 'medium' that shows Dec 10, 2015 7:00:00 AM in the view

date: 'medium' : +0500 bumpeds it to Dec 10, 2015 5:00:00 PM

date: 'medium' : -0500 it shows as: Dec 10, 2015 7:00:00 AM AGAIN

How the hell do I get it to show at 12pm?! lol I'm on EST time

Upvotes: 1

Views: 1095

Answers (2)

JB Nizet
JB Nizet

Reputation: 691745

The Z at the end means "UTC". So, that date represents the instant that is displayed as 2015-12-10T12:00:00.000 in the UTC time zone.

And you want to display it as if you were in the UTC time zone, since you don't want the time part to be different from the one it has in UTC.

So, use UTC as the time zone:

date:'medium':'UTC'

Output:

Dec 10, 2015 12:00:00 PM

Upvotes: 3

jusopi
jusopi

Reputation: 6813

filter

Create a custom filter first:

.filter( 'trimDateTime', function(){
    function(ds){
        var z = ds.indexOf('Z')
        return ds.substr(0, z)
    }
 }

html

Then apply 2 filters to the data

<p>{{ dateString | trimDateTime | date: 'medium' }}</p>

why

Angular filters are powerful mechanisms for leaving your data intact but rendering a special way. You could have easily trimmed the 'Z' from your date string but then later on, another UX might be expecting that data to be intact.

codepen

http://codepen.io/jusopi/pen/pgvOBr

Upvotes: 0

Related Questions