How to change date format using jquery?

How do I change date format in JQuery Datepicker?

  • 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.

  • Answer:

    $.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');

media_itoku at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

apologies, should have read the docs, that is right

twistedonion

Try 'yy-mm-dd' 'yy' is the datepicker format for a 4-digit year. 'yyyy' is nothing.

coryinabox

Thanks for the quick responses! I've now tried all three of these; no luck so far.

media_itoku

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.

coryinabox

It works! @coryinabox FTW. Thanks!

media_itoku

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.

Horselover Fat

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.

holloway

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.

yerfatma

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?

media_itoku

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.