0

I'm dealing with an issue with dates format: in my formular I'm using uib-datepicker-popup to get the calendar and in the controller I need to format this date so I'm using a $filter('date'),

so here is the input field:

<div class="input-group">
    <input id="fechasolicitudc" type="text" class="form-control"
        ng-class="(form.fechasolicitudc.$invalid) && (submitted) ? 'error': '' "
        ng-style="(form.fechasolicitudc.$invalid) && (submitted) && {'background-color':'pink'}"
        name="fechasolicitudc" uib-datepicker-popup="dd/MM/yyyy"
        ng-model="vm.peticion.contacto.fechaSol" 
        is-open="vm.datePickerOpenStatus.fechasolicitudc"/>
    <span class="input-group-btn">
        <button type="button" class="btn btn-default"
            ng-click="vm.openCalendar('fechasolicitudc')">
            <i class="glyphicon glyphicon-calendar"></i>
        </button>
    </span>
</div>

and in my controller:

if (vm.peticion.contacto.fechaSol != null) {
    var d = $filter('date')(vm.peticion.contacto.fechaSol, 'yyyy-MM-dd');
    vm.peticion.contacto.fechaSol = d;
}

what happenes is that the date is updated in the DB but the field becomes erased. When I try give the input type="date" the field is not erased but the calendar popup does not work.

1 Answer 1

1

In your markup:

//...
uib-datepicker-popup="{{dateFormat}}" ng-model="vd"
datepicker-options="vdOptions"
// ... 

In your controller:

$scope.dateFormat = 'dd MMM yyyy';  // or your custom date

// and if you want to have your date set as today
$scope.vd = new Date();
$scope.vdOptions = {
      formatYear: 'yy',
      formatMonth: 'MM',
      maxDate: new Date(),
      minDate: minDate,
      startingDay: 1,
      showWeeks: false,
      yearRows: 2,
      yearColumns: 2
};

vd = valueDate

I do not see the point of using $filter in this case.

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

2 Comments

in my DB the format is yyyy-MM-dd and the datepicker-popup="{{dateFormat}}" changes just the appearence of the date not its format, if i don't do the $filter the insert in the DB does not work (i don'treally understand why)
I have updated the answer with the vdOptions property where you can customise your date options

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.