AngularJS date picker value in Spring model setter

Viewed 43

Using angularJS with Spring- for User object DOB field, using input type date on my view file which is bind with attribute "user.dateOfBirth".

Whenever i select any date it is perfectly fine on view itself , no issue.

{{user.dateOfBirth}}

As soon as i submit and date setter of my pojo User object is invoked, the value coming is a day behind.

For ex- On AngularJS Ui, displayed value is- Oct 11, 2019 12:00:00 AM which is as per selected date but on User model- Date setter

public void setDateOfBirth(String dateOfBirth) { 
    //Value is coming as 2019-10-10T18:30:00.000Z 
    //which is a day behind the selected value      

    this.dateOfBirth = dateOfBirth;
}
1 Answers
//Value is coming as 2019-10-10T18:30:00.000Z 
//which is a day behind the selected value      

The AngularJS date picker uses the local timezone when parsing the date.

To set the date to UTC, use the ng-model-options directive:

<input type="date" ng-model="date1"
       ng-model-options="{timezone: 'UTC'}"
/>

Then the AngularJS will encode the date as 00:00 hours Zulu instead of 00:00 hours India (which is 18:30 Zulu of the previous day).

For more information, see

Related