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/
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.
The date filter formats a date to a specified format. By default, the format is "MMM d, y" (Jan 5, 2016).
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With