How do I change date format in JQuery Datepicker?
March 19, 2009 4:27 PM   Subscribe

I'm using JQuery Datepicker, and I'm trying to get it to change the date format from the default mm/dd/yyyy to yyyy-mm-dd (so I can insert it into a mySQL field). But somehow, despite reading the documentation until my eyes bleed, I can't get it to change! What am I doing wrong?

OK, so I have the script in the header (between script tags):

$(function() {
$("#datepicker").datepicker();
});

The documentation seems to be saying I add an extra line to the script, but it's not really clear to me where it goes. So, I've tried:

$(function() {
$("#datepicker").datepicker();
$.datepicker.formatDate('yyyy-mm-dd');

});

No luck. What am I doing?
Yes, I'm a javascript noob.
posted by media_itoku to Computers & Internet (11 answers total)
 
$.datepicker.formatDate('yyyy-mm-dd');

This doesn't look right to me. I haven't looked at the docs but try this:

$("#datepicker").formatDate('yyyy-mm-dd');
posted by twistedonion at 4:29 PM on March 19, 2009


apologies, should have read the docs, that is right
posted by twistedonion at 4:30 PM on March 19, 2009


Try 'yy-mm-dd'

'yy' is the datepicker format for a 4-digit year. 'yyyy' is nothing.
posted by coryinabox at 4:33 PM on March 19, 2009


Response by poster: Thanks for the quick responses! I've now tried all three of these; no luck so far.
posted by media_itoku at 4:38 PM on March 19, 2009


Based on your code above, you'd want:

$(function() {
$("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
});

This will set the date format for the actual datepicker control on your page.

The
$.datepicker.formatDate(blahblahblah);

is used when you already have a specific date that you want to get into a different format.
posted by coryinabox at 4:58 PM on March 19, 2009


Response by poster: It works! @coryinabox FTW. Thanks!
posted by media_itoku at 5:00 PM on March 19, 2009


This isn't an answer to your question, but it sounds like you're going to take your formatted date and concatenate it into an sql string. Don't do this; use whatever parametrized query mechanism your platform makes available, and parse the user input into a proper Date data type.
posted by Horselover Fat at 6:33 PM on March 19, 2009


it sounds like you're going to take your formatted date and concatenate it into an sql string. Don't do this;
This.

If your jquery code can break your application that's bad server-side code.
posted by holloway at 10:04 PM on March 19, 2009


A different suggestion for anyone who finds this via Google: you have to download a date.js file to make the date picker work, and you can specify the format right in there, around line 86 on my copy.
posted by yerfatma at 7:12 AM on March 20, 2009


Response by poster: This isn't an answer to your question, but it sounds like you're going to take your formatted date and concatenate it into an sql string. Don't do this; use whatever parametrized query mechanism your platform makes available, and parse the user input into a proper Date data type.

I wish I knew what this meant ;-). I'm feeding the date from a datepicker form into a mySQL database, into a field of type Date. Later on, I will use php to retrieve it and print it. Is this wrong?
posted by media_itoku at 1:04 PM on March 20, 2009


No, that's not what they're saying. What they mean is to make sure you do some sanity checking on the value coming back from the browser to make sure it matches your expected format. You need to do this to avoid SQL Injection attacks. Here's a bit on how to prevent this in PHP and some info on how to prepare MySQL statements.
posted by yerfatma at 9:55 AM on March 24, 2009


« Older How can my brother stop being a loser?   |   white Perl, black oceans Newer »
This thread is closed to new comments.