1

The date on the selector (ngx-bootstrap datepicker) is showing one day older date.

If the date value is

2019-04-26T18:30:00

The datepicker shows it as 04/25/2019. For the datepicker input I doing the following thing-

this.dateAdded = new Date(this.user.dateAdded).toLocaleDateString();

I am at GMT+5:30 so even adjusting for timezone I don't understand why it is going one day beind.

Nemesis
  • 135
  • 1
  • 1
  • 13
  • Identify what is the difference of time and add it up on reaching the server.. Here in your case 5hr 30 Mins should be added to the date value in all cases – Ajith Dec 17 '19 at 14:24
  • @Ajith that's a pretty hacky solution, it's better to identify and fix the source of the issue. – iPhoenix Dec 17 '19 at 14:25
  • Hey, the DateTime value I have written is the one I am receiving from server. – Nemesis Dec 17 '19 at 14:25
  • I can't reproduce this issue, if you add console.logs around that line in the code and check the values of `this.user.dateAdded` and the result value of `this.dateAdded` after the line executes, does it work as intended? – iPhoenix Dec 17 '19 at 14:27
  • console.log(this.user.dateAdded) =2019-04-26T18:30:00; console.log(this.user.dateAdded.toLocaleDateString() = 04/26/2019. However on the form the date appears as 04/25/2019 – Nemesis Dec 17 '19 at 14:30
  • Checkout this.. https://stackoverflow.com/questions/9509360/datepicker-date-off-by-one-day – hbamithkumara Dec 17 '19 at 15:50
  • This one too https://stackoverflow.com/a/31732581/5028715 – hbamithkumara Dec 17 '19 at 15:52
  • 1
    Seems like an on-going issue regarding to ngx-bootstrap datepicker https://github.com/valor-software/ngx-bootstrap/issues/3609 – penleychan Dec 17 '19 at 15:56

1 Answers1

1

After spending the better part of the day and learning about the complexities that come with displaying date in Javascript the answer to this specific question is a bit of an anticlimax. The version of ngx-bootstrap I was using (5.2) has some bugs with the datepicker. Use the 5.1 version so as to not have the issue with the date. It seems like ToLocaleDateString which only gives the date component if adjusted for timezone by bsDatePicker (at midnight) gives the previous day.

https://github.com/valor-software/ngx-bootstrap/issues/3609 Reference for github issue regarding the same.

Nemesis
  • 135
  • 1
  • 1
  • 13
  • We encountered the same problem with ngx-bootstrap 5.2 on a legacy project, and determined that 5.3.2 has the datepicker timezone issue fixed, for anyone who would prefer to move a minor version forward rather than backward. – coryseaman Apr 29 '22 at 16:13