Derek Lim
Derek Lim

Reputation: 351

jQuery date format not working

I am using jQuery datepicker for my project. I have this code:

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

My gripe is that although the datepicker shows, the date format is still 'mm-dd-yyyy') and I had also tried

$('#date-show').datepicker("option","dateFormat","dd-mm-yy");

It still displays as mm-dd-yyyy. Am I missing out something?

Upvotes: 0

Views: 1822

Answers (1)

Dekel
Dekel

Reputation: 62676

Not sure if the problem you has was with day/month or with the number of digits for year, but this code works:

$('#date-show').datepicker({ dateFormat: 'dd-mm-yy' });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
<input id="date-show" name="date-show" />

If you need the year to be 2 digits (instead of 4) you should use only 1 y (and not 2).

$('#date-show').datepicker({ dateFormat: 'dd-mm-y' });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
<input id="date-show" name="date-show" />

Upvotes: 1

Related Questions