Angular Format-date with date decreased by one day

2

I'm using an angular directive to handle dates

My policy is this:

app.directive("formatDate", function() {
    return {
        require: 'ngModel',
        link: function(scope, elem, attr, modelCtrl) {
            modelCtrl.$formatters.push(function(modelValue) {
                if (modelValue){
                    return new Date(modelValue);
                }
                else {
                    return null;
                }
            });
        }
    };
});

When I display the date of my listing comes the correct date. But when I open the modal for data editing the date comes with a less day.

Let's see;

My listing looks like this:

InthelistingIformatthedatethisway:

<td>{{pes.dataNascimento|date:'dd/MM/yyyy'}}</td>

ButwhenIclickonthemodaltochangetheselectedperson,thedateisloadedwithonedaylessthen:

Mycomponentlookslikethis:

<divclass="es col-md-4">

                    <label class="lb" style="margin-top: -5px;">Data Nascimento <label class="lb1"
                      data-toggle="tooltip" title="ÁREA DE TEXTO OBRIGATÓRIO"> *</label>
                    </label> <input  title="ÁREA DE TEXTO OBRIGATÓRIO"  style="margin-top: -70px;"
                      required="required" class="form-control "  name="data" id="data" type="date" ng-model="pessoa.dataNascimento" format-date/>

                  </div>

I do not know if it's relevant but when you click on change the data captured from the listing it looks like this:

Does anyone have any idea what could be happening ??

    
asked by anonymous 19.02.2018 / 19:13

1 answer

3

This is timezone problem. You can use ng-options to resolve it like this:

ng-model-options="{timezone: 'UTC'}"

Or even so:

ng-model-options="{timezone: timezone}"

And in the controller:

$scope.timezone = ((date.getTimezoneOffset() / 60) * -100)

fiddle link with the example

    
27.02.2018 / 14:48