Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Angular date filter displaying wrong date?

Tags:

angularjs

I'm using a date input field, and formatting the selected date in my ui with Angular. But the formatted date is always 1 day less than the selected date. Why is that, and how can I fix it?

HTML:

<div ng-app="miniapp">
    <div>
    <label class="control-label" for="inputStart">Start Date:</label>
        <input type="date" id="inputStart" data-ng-model="startDate" /><br />
        Selected: <span>{{ startDate }}</span><br />
        fullDate: <span>{{ startDate | date:'fullDate' }}</span><br />
        mediumDate: <span>{{ startDate | date:'mediumDate' }}</span><br />
        MMMM d yyyy<span>{{ startDate | date:'MMMM d yyyy' }}</span>
    </div>    
</div>

JS:

var app = angular.module('miniapp', []);

I have a fiddle that demonstrates the issue: http://jsfiddle.net/wittersworld/uY3s9/

EDIT: I updated the fiddle with a working solution: http://jsfiddle.net/wittersworld/uY3s9/2/

like image 737
witters Avatar asked Jun 08 '13 20:06

witters


People also ask

Is date a filter in Angular JS?

AngularJS date filter is used to convert a date into a specified format. When the date format is not specified, the default date format is 'MMM d, yyyy'. Parameter Values: The date filter contains format and timezone parameters which is optional.

What is the date format in angular?

The date filter formats a date to a specified format. By default, the format is "MMM d, y" (Jan 5, 2016).


1 Answers

This is a timezone issue.

If you enter a date of, say, June 8, 2013 into your date picker, that's midnight GMT. If you live west of England, say, in the U.S., it's June 7, 2013.

Change the line

{{ startDate | date:'fullDate' }}

to

{{ startDate | date:'medium' }}

to see the time!

like image 166
Ray Toal Avatar answered Oct 22 '22 12:10

Ray Toal