Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pickadate adding +1 to month #391

Closed
Maximilianos opened this issue Apr 6, 2014 · 6 comments
Closed

pickadate adding +1 to month #391

Maximilianos opened this issue Apr 6, 2014 · 6 comments

Comments

@Maximilianos
Copy link

I have php print a date in the data-value attribute of the datepicker input in the format Y-m-d. I have set the formatSubmit property to yyyy-mm-dd and yet the date that comes out in the datepicker is one month more than the date in data-value.

I can visually verify, by looking at the source, that the date gets printed correctly by php as 2014-04-07, for instance, but pickadate for some reason considers it 2014-05-07.

here's my code if it helps:

<?php $today = date( 'Y-m-d' ); ?>
<input id="checkin"
       class="datepicker"
       value="<?php echo $today; ?>"
       data-value="<?php echo $today; ?>"
       data-min="<?php echo $today; ?>"
       data-max="<?php echo data( 'Y-m-d', strtotime( '+ 2 years' ) ); ?>">

and js:

if ( jQuery.fn.pickadate ) {
  $('.datepicker').each(function () {
    var me = $(this),
      min = me.data('min').split('-'),
      max = me.data('max').split('-');

      me.pickadate({
        min: min,
        max: max,
        formatSubmit: 'yyyy-mm-dd',
        hiddenSuffix: ''
      });
  });
}
@psorowka
Copy link

psorowka commented Apr 7, 2014

This issue arises due to the assignment of data-value AND value attribute and is the same issue mentioned in pull request #386

@Maximilianos
Copy link
Author

It was pointed out to me today that it is more likely an issue with JavaScript rather than pickadate. JavaScript indexes months from 0 - 11 rather than the more usual and obviously too boring 1 - 12. Therefore a month with number 04 is indeed May and not April.

This question on SO suggests a workaround is using the formats yyyy/mm/dd || dd/mm/yyyy.

@psorowka
Copy link

psorowka commented Apr 7, 2014

Right the cause if the issue is the month indexing of JavaScript. According to the docs, pickadate should parse the value given in the data-value attribute with one-based indexing, when a formatSubmit is given. Currently this doesn't work when also the value="" attr is given. The above mentioned pull request resolves that

@bterkuile
Copy link

+1 for this one. Support for iso8601 format yyyy-mm-dd would be awesome!
2014-04-10 in the input field on load is interpreted as 10 May 2014. When selecting 10 June 2014 the input field becomes: 2014-06-10, which is the expected outcome. (ps: v3.4.0 from github today)

@adrianstride
Copy link

I fixed it be removing the !options.fromValue from line 582 in picker.date.js. I tested and if value is a string, I would always assume that month needs monthIndex = 1. The only exception to that is if it is not a string in the set value.

line 582

monthIndex = typeof value == 'string' ? 1 : 0

@amsul
Copy link
Owner

amsul commented Apr 12, 2014

Thanks for the possible solutions everyone. Closing this in favor of #358 PR

@amsul amsul closed this as completed Apr 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants