0

I'm using ng2-eonasdan-datetimepicker in an Angular 5 project, but I'm having issues with the value when I use it to select year only.

I select the year and the value displays correctly in the input, but other value is shown when I display the value from formBuilder.get:

enter image description here

I need only the year to send to my API. How to solve this?

My Input:

<div class="form-group" [ngClass]="{'has-error' : hasError('modelYear', 'vehicle')}">
    <label>Ano {{ createRemovalForm.get('vehicle.modelYear').value }}</label>
    <input type="text" a2e-datetimepicker [options]="{locale: 'pt-br', format: 'YYYY'}" class="form-control dt-placeholder" placeholder="Ano" formControlName="modelYear">
</div>

FormBuilder:

this.createRemovalForm = this.fb.group({
    idAuction: ['', Validators.required],
    idRemovalType: ['', Validators.required],
    addressRemLat: ['', Validators.required],
    addressRemLng: ['', Validators.required],
    addressRemShort: ['', Validators.required],
    addressRemLong: ['', Validators.required],
    addressRemCity: ['', Validators.required],
    financedInsuredName: ['', Validators.required],
    bankInsurerName: ['', Validators.required],
    contract: ['', Validators.required],
    acessory: ['', Validators.required],
    scheduled: [true, Validators.required],
    dtToRemove: ['', Validators.required],
    contactName: ['', Validators.required],
    contactPhone: ['', Validators.required],
    vehicle: this.fb.group({
        idRequestVehicleType: ['', Validators.required],
        manufacturer: ['', Validators.required],
        model: ['', Validators.required],
        color: ['', Validators.required],
        modelYear: ['', Validators.required],
        plate: ['', Validators.required],
        chassis: ['', Validators.required],
        observation: [],
    })
});

1 Answer 1

0

can you check with passing option like -

options = { format: "YYYY", };

check here - https://www.npmjs.com/package/ng2-eonasdan-datetimepicker

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

5 Comments

I'm using this in [options] in the input
Input field showing Year only but you are not getting same in form control - modelYear ?
Exactly this! The problem is in the value in the form control. The input field is ok!
ok, you can use pipe inside the component to format date before sending to api like - this.formName.value.modelYear = this.datePipe.transform(new Date(this.formName.value.modelYear), 'YYYY'); look at - stackoverflow.com/questions/44844861/…
Solved! Thank you!

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.