简体   繁体   English

在jQuery UI日期选择器上更改日期格式

[英]Change Date Format on jQuery UI Date Picker

I have been trying to format the datepicker so that the date comes out like this: 我一直在尝试格式化日期选择器,以便日期如下所示:

Jul 23 15 15年7月23日

I have been reading this question jQuery UI DatePicker - Change Date Format 我一直在阅读这个问题jQuery UI DatePicker-更改日期格式

But for some reason it is not working for me. 但是由于某种原因,它对我不起作用。 I am still learning jQuery so any help on this would be greatly appreciated. 我仍在学习jQuery,因此在此方面的任何帮助将不胜感激。

My code is below: 我的代码如下:

  $('input').datepicker({ onSelect: function(dateText, inst) { var date = $(this).datepicker('getDate'), day = date.getDate(), month = date.getMonth() + 1, year = date.getFullYear(); var parent_div = $(this).closest(".clickable-text"); console.log(parent_div); $(".Jquery_day", parent_div).html(day); $(".Jquery_month", parent_div).html(month); $(".Jquery_year", parent_div).html(year); $(parent_div).addClass("is_selected"); } }); $(window).load(function() { $('input').datepicker(); $('input').datepicker("setDate", new Date()); var dateFormat = $('input').datepicker({dateFormat: "Mdy"}).val(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

Updated snippet of code below: 更新的代码片段如下:

  $('input.start').datepicker({ onSelect: function(dateText) { // alert(dateText); var date = $(this).datepicker('getDate'), day = date.getDate(), month = date.getMonth() + 1, year = date.getFullYear(); var parent_div = $(this).closest(".clickable-text"); console.log(parent_div); $(".Jquery_day", parent_div).html(day); $(".Jquery_month", parent_div).html(month); $(".Jquery_year", parent_div).html(year); $(parent_div).addClass("is_selected"); }, dateFormat: "M dy" }); $('input.start').datepicker(); $('input.start').datepicker("setDate", new Date()); 

Please see jsfiddle below. 请参阅下面的jsfiddle。 It appears to work kind of okay here for some reason it is adding an extra calendar, it may be something else within my code that is causing the issue. 由于某种原因,它似乎添加了额外的日历,因此在这里似乎可以正常工作,可能是我的代码中的其他原因导致了问题。 If I am coding something wrong then please do let me know. 如果我编码有误,请告诉我。 I am struggling to get back the selected value on the 'Number of Adults' section as well but it seems to be working here. 我也在努力找回“成人人数”部分中的选定值,但它似乎在这里起作用。

Demo 演示版

Were you after this? 你在这之后吗?

$('input').datepicker({  

    onSelect: function(date) {
        alert(date);
    },
    dateFormat: "M d y"
});

Following are the allowed formats: 以下是允许的格式:

  • Default: "mm/dd/yy" 默认值: "mm/dd/yy"
  • ISO 8601: "yy-mm-dd" ISO 8601: "yy-mm-dd"
  • Short: "d M, y" 简短: "d M, y"
  • Medium: "d MM, y" 中: "d MM, y"
  • Full: "DD, d MM, yy" 完整: "DD, d MM, yy"
  • With text: "'day' d 'of' MM 'in the year' yy" 带有文本: "'day' d 'of' MM 'in the year' yy"

A Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM