最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does changing the dateFormat of my jQuery datepicker fail? - Stack Overflow

programmeradmin1浏览0评论

I know there are loads of questions on here about changing the datepicker dateFormat, but i'm following the instructions exactly and they seem to have no effect. Here is the exact code that was suggested on other answers:

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

Yet when I run it, and test the datepicker value with alert($('#datepicker').datepicker("getDate"));

I get the date back in the undesired format! This is extremely confusing. Am I something wrong? See the described behaviour at /

edit: even more confusing is that if I read the dateformat using alert($("#datepicker").datepicker('option', 'dateFormat'));

... the returned dateFormat is correct, even though calling getDate does not return the date in the desired format. See /

I know there are loads of questions on here about changing the datepicker dateFormat, but i'm following the instructions exactly and they seem to have no effect. Here is the exact code that was suggested on other answers:

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

Yet when I run it, and test the datepicker value with alert($('#datepicker').datepicker("getDate"));

I get the date back in the undesired format! This is extremely confusing. Am I something wrong? See the described behaviour at http://jsfiddle/TmdM8/

edit: even more confusing is that if I read the dateformat using alert($("#datepicker").datepicker('option', 'dateFormat'));

... the returned dateFormat is correct, even though calling getDate does not return the date in the desired format. See http://jsfiddle/fWmBe/

Share Improve this question edited Jun 29, 2011 at 19:09 hughes asked Jun 29, 2011 at 18:43 hugheshughes 5,7113 gold badges40 silver badges55 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

the date format setting is for what the format looks like when it's show in the text box the picker is applied to.

have a look at my updated version of your fiddle:

http://jsfiddle/TmdM8/1/

as you can see, when you pcick in the text box, and pick a date, it is in the format you have specified.
as far as i know, the getDate function returns a javascript date.

EDIT: if you wanted to format the output from the javascript date, you'll need to use a custom script, or a library like date.js (i've had great success with this).

http://code.google./p/datejs/

with that you could do something like :

$('#datepicker').datepicker("getDate").toString('dd-MM-yyyy');

to get your date in the format you like

I agree that it returns a javascript date. You can use this utility function:

$.datepicker.formatDate( format, date, settings ) - Format a date into a string value with a specified format.

from http://docs.jquery./UI/Datepicker/formatDate

What is happening here is getDate is return a javascript date object. You are seeing the date object's toString method.

Try this:

$(document).ready(function() {
   $('#datepicker').datepicker({
   onSelect: function(dateText, inst) { alert(dateText) }
   });
$('#datepicker').datepicker( "option" , 'dateFormat', 'yyyy-mm-dd' )


});

in case someone looking for this answer like me. here is a working code:

<script>
$(function() {
$( "#datepicker" ).datepicker({inline: true, dateFormat:'yy-mm-dd'}); });
</script>
发布评论

评论列表(0)

  1. 暂无评论