4

I have a form with <input type="date">.

When I bind data in this input, it shows me the date -1 day.

The HTML:

<div class="input-field col s12">
    <label>Fecha Nacimiento </label>
    <input type="date" class="form-control" id="fnac" name="fnac" ng-model="unapersona.fnac">
</div>

The Controller:

$scope.cargarpersona = function(id) {
    $http.get("modelos/personas_json.php?id="+id)
    .success(function(data) {
        $scope.unapersona = eval(data);
        //... Other data
        $scope.unapersona.fnac = new Date($scope.unapersona[0]["fnac"]);
        //... Other data
    })
    .error(function(data) {
        console.log('Error: ' + data);
    });
}

Screen Capture

6
  • 1
    what date format does $scope.unapersona[0]["fnac"] return? Commented Mar 17, 2016 at 14:17
  • 1
    What data comes back from your ajax call? Commented Mar 17, 2016 at 14:18
  • return the JSON format '2016-03-17'. In list view show date correctly, when I save changes, save correctly (the date of <input>+1). Commented Mar 17, 2016 at 14:25
  • try new Date($scope.unapersona[0]["fnac"] + " UTC") Commented Mar 17, 2016 at 14:27
  • Sorry @FabioG, Don't work... Commented Mar 17, 2016 at 14:34

3 Answers 3

9

Solved!! Just I put the ng-model-options = "{timezone 'UTC'} into the input date

<input type="date" class="form-control" id="fnac" name="fnac" ng-model="unapersona.fnac" ng-model-options="{timezone:'UTC'}">

https://docs.angularjs.org/api/ng/directive/ngModelOptions

Thanks for yours answers and time!

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

1 Comment

I just stumbled on this solution myself, glad you found it also :)
1

You're probably running into timezone problems. Consider the following Code (Plunkr here)

JS

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

app.controller('MainCtrl', function($scope) {
  $scope.aDate = new Date(Date.UTC(2016, 02, 17, 0, 0, 0));
  $scope.aSecondDate = new Date(2016, 02, 17, 0, 0, 0);
});

HTML

<body ng-controller="MainCtrl">
    <h4>UTC Date</h4>
    <p>{{aDate}}</p>
    <h4>Local Date</h4>
    <p>{{aSecondDate}}</p>
  </body>

Output (on a CET browser):

UTC Date

"2016-03-17T00:00:00.000Z"

Local Date

"2016-03-16T23:00:00.000Z"

In the first case the date is set with UTC as the timezone.

In the second case, the date is set with the local timezone (your browser's settings), and then converted to UTC (which at the moment differs by 1h from CET), and because that puts the date over midnight, this is a different day.

5 Comments

Sorry... with the Date.UTC function, don't show any date in <input>. I don't think it Timezone problems, because in the list view displays the correct date
How did you try the code? Can you provide the changed code? And can you provide your list view in your original question also? Which timezone are you in?
$scope.unapersona.fnac = new Date(Date.UTC($scope.unapersona[0]["fnac"]));
This won't work, Date.UTC expects multiple arguments: developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
I tried this, but does not show the date in the input: var date = new Date($scope.unapersona[0]["fnac"]); $scope.unapersona.fnac = new Date(Date.UTC(date.getFullYear(),date.getMonth() + 1),date.getDate());
0

I believe the date input type expects / instead of -, havent tested but hope it works:

   .success(function(data) {
        $scope.unapersona = eval(data);

        var from_data = $scope.unapersona[0]["fnac"];
        var date_replace = from_data.replace(/-/g, '/');

        var date = new Date(date_replace );
        $scope.unapersona.fnac = ((date.getMonth() + 1) + '/' +  // JS months are 0 indexed and days are 1 indexed
                                   date.getDate() + '/' +                                                                                  
                                   date.getFullYear());
        })

3 Comments

what date does it return? for example if $scope.unapersona[0]["fnac"] was 2016-03-17?
and what do you see in the html input type= date ?
ok I updated my answer, seems to be that with - istead of / there is some kind of js bug, tested answer in console and seems to work fine

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.