17

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/

2
  • In your fiddle dates look just fine. Commented Jun 8, 2013 at 20:45
  • 1
    @Stewie that's because Sarajevo is GMT+1. If you try the OP's fiddle at 0:05 your time when GMT is 23:05 the previous day, it will look like the fiddle is showing one day later than you entered. Commented Jun 8, 2013 at 20:54

1 Answer 1

15

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Ray! That got me on the right track to a solution. I found an article that helped me the rest of the way, and updated the fiddle with a working method. techrepublic.com/article/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.