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

field with Pikaday is automatically going backwards by one day everytime record is edited. #39

Closed
memoht opened this issue Feb 14, 2013 · 18 comments

Comments

@memoht
Copy link

memoht commented Feb 14, 2013

I have a field called last_issue on a rails model. If a date exists in the the last_issue field, every time you edit the record, the date appears to be dropping back by one day. Edit a record seven times and your date will be off by a week from where it started.

I am using the following setting for this field:
picker = new Pikaday(field: $("#issue_date")[0])

Any suggestions as to why this is happening?

@memoht
Copy link
Author

memoht commented Feb 14, 2013

Adding moment.js makes this problem disappear, but still would be curious as to what I may have done wrong or whether or not this is a bug.

@dbushell
Copy link
Member

Difficult to say what's going on here, doesn't look like you're doing anything wrong. Can you upload an example?

@memoht
Copy link
Author

memoht commented Feb 15, 2013

Hello again David.
I created a simple Rails app and pushed it up to Github. I don't know what you use locally, so if you need me to push this out to Heroku so you can hit it from the web I can do that. I made you a collaborator. This app seems to reproduce the problem I experienced.

@rikkert
Copy link
Member

rikkert commented Feb 18, 2013

Could you indeed please host it, I would like to have a look myself.

@memoht
Copy link
Author

memoht commented Feb 18, 2013

I updated the app and pushed it to Heroku. I loaded a single post. It is a simple app, but hope it is useful for you. I'll keep it up for a week so you have enough time to check it out. The app is at http://backaday.herokuapp.com


Rikkert, I added you as a collaborator on the Github repo in case you are interested in the underlying code. Again, this is a Rails site.

@rikkert
Copy link
Member

rikkert commented Feb 19, 2013

Great thanks.
Very weird but I can't seem to reproduce it. :|

I edited the post a couple times:
edit -> submit
edit -> click on pikaaday -> submit
edit -> click on pikaaday -> choose 1 jan -> submit
edit -> click on pikaaday -> choose 2 jan -> submit

All of these actions had the expected result: either date was 1 jan or 2 jan.
Should I do something else?

@memoht
Copy link
Author

memoht commented Feb 19, 2013

To quote a recent movie. "You had my curiosity. Now you have my attention." I have uploaded a short movie in which I went through various browsers on Mac OS Mountain Lion, Windows XP, Windows 7 and Windows 8. Long story short, I did notice that the problem did not occur in Safari on Windows 7 or 8, but did occur in all other browsers on all platforms.
Here is the video: http://youtu.be/me5XBWPqNzw


  • I don't currently have any Linux OS loaded so could not test that platform. Perhaps you are on Linux?
  • The only other thing I can think of is that I have disabled/uninstalled Oracle Java from my systems due to ongoing security issues with Java. I of course have not disabled JavaScript...

Thanks for taking a look. BTW, I did want to mention that I really like Pikaday. Stopped using JQuery-UI datepicker when I found this little gem. :0) So keep up the great work!

@rikkert
Copy link
Member

rikkert commented Feb 19, 2013

Yep, I was on Linux with chrome 26.
Will try again with firefox later.

@memoht
Copy link
Author

memoht commented Feb 20, 2013

I loaded up Ubuntu and it seems to do this is Firefox.

@rikkert
Copy link
Member

rikkert commented Feb 20, 2013

It is caused by your local (browser) time zone settings.
I put my chromebook from Amsterdam (+1 GMT) to EST and started to see the same problem in chrome.
Could you try with my pull request #42 ? Maybe that fixes it.

Otherwise I can`t really debug your app because it is minimized.

@memoht
Copy link
Author

memoht commented Feb 20, 2013

Unfortunately it still goes back. In my Rails app I declare a time zone which I thought helped keep things happy with time based fields since I offset from GMT. I appreciate your effort. My fix was just to load moment.js which is a recommended option anyway so harmless and easy.


If you need me to try anything else let me know.. I will go ahead and close this ticket with this comment. Again, thanks for developing Pikaday, I like that it is lighter and cleaner to use than JQuery datepicker. LLAP.

@memoht memoht closed this as completed Feb 20, 2013
@rikkert
Copy link
Member

rikkert commented Feb 20, 2013

You gave enough info to reproduce the problem. It was only a bit tricky because I had to change my local time-zone.
I found that the problem is the format of the date in your input field. For JavaScript it should be: day month year:
http://tools.ietf.org/html/rfc2822#page-14
Not sure how we can prevent something like this, but it was a fun hunt anyway and thank you!

@alberro
Copy link

alberro commented Jan 14, 2014

Hi guys,

We has the same problem and I fixed creating the new Date splitting the date and not using the Date.parse method.

In the setDate method, replace the entire if with:

if (typeof date === 'string') {
    var p = date.split('-');     // We use the format YYYY-MM-DD in our dates.
    date = new Date(p[0], p[1] - 1, p[2]);
}

And that's it, it works perfect without adding Momentjs to your project.

Hope this help,
Greetings.

@TimGeyssens
Copy link

Still having this issue with latest version.. seems to have to do with timezone setting, not sure how to fix any help appreciated

@eybarta
Copy link

eybarta commented Nov 13, 2015

I'm also experiencing a similar issue..
I'm using moment() and have been until now no problems..
When adding moment-timezone.js and setting it (e.g: moment.tz.setDefault('America/New_York');) I'm getting the same problem as described above, the display dates for input fields after init is a day back.. Am I doing something wrong or is this a bug? thnx

@sdbondi
Copy link

sdbondi commented Nov 17, 2015

Note that when you have a date (like that returned from getDate()) it will have the timezone of your computers clock. When calling date.toUTCString() the date will be in the GMT timezone which may confuse some people into thinking pikaday has chosen the previous day. The call to toUTCString might be transparent to you e.g. an api library might call that method to serialize the date.

.getDate()
Wed Dec 02 2015 00:00:00 GMT+0200 (SAST)

.getDate().toUTCString()
"Tue, 01 Dec 2015 22:00:00 GMT"

This took me a few minutes to realise so I thought I'd put this here in case other are getting confused.

@gregmatthewcrossley
Copy link

For anyone using Ruby/Rails, I found that adding .strftime("%a %b %d %Y") to a Date fixed the issue.

@jacquesporveau
Copy link

This worked for me. In the setDate function of Pikaday.js add:

if (typeof date === 'object') {
  var utc = date.getTime() + (date.getTimezoneOffset() * 60000 + 500);
  date = new Date(utc)
}

I am adding 500 milliseconds because sometimes the calculation is a little off and since my app is saving dates and midnight it would occasionally show the wrong day if the calculation came back slightly short.

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

9 participants