1

We're storing birth dates in the format of yyyy-mm-dd. When this format is provided to the angular-bootstrap date picker, it selects the incorrect date in the popup. Converting it to a date object causes both the display and selection to be incorrect. See my plnk for examples (ignore validation stuff, that's a whole other issue).

// Displays '2015-09-25', but 24th is selected
var date = '2015-09-25';

// Displays '2015-09-24', selects 24th
var date = new Date("2015-09-25");
Brian
  • 1,363
  • 3
  • 12
  • 26

1 Answers1

3

From javascript Date timezone issue: "In JavaScript, a value in the format of YYYY-MM-DD is interpreted as a UTC value, rather than a local-time value."

One workaround is to replace the hyphens with slashes:

var s = "2015-09-25";
var dt = new Date(s.replace(/-/g, '/'));

I would recommend using moment.js though. It works for me and I had the same problem with a javascript datepicker.

var s = "2015-09-25";
var dt = moment(s, 'YYYY-MM-DD').toDate();
Community
  • 1
  • 1
Always Learning
  • 312
  • 1
  • 10
  • Instead of linking to other answers it is better to mark questions as duplicates – lightswitch05 Nov 23 '15 at 15:34
  • 1
    +1 To using Moment.js for parsing dates. When doing that, make sure you pass a specific format string: `moment(str, fmt)`. This is key. From the [docs](http://momentjs.com/docs/#/parsing/): "Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers. / For consistent results parsing anything other than ISO 8601 strings, you should use String + Format." – Ates Goral Nov 23 '15 at 15:36