【问题标题】:Custom date format “fengyuanchen/datepicker”自定义日期格式“fengyuanchen/datepicker”
【发布时间】:2021-08-06 18:32:03
【问题描述】:

我正在使用“fengyuanchen/datepicker”日期选择器插件,我需要 2021 年 5 月 17 日的日期格式,但是当我更改日期格式时,它给了我未定义。需要帮助,提前致谢。

$('[data-toggle="datepicker"]').datepicker({
    autoHide: true,
    format: 'dd MM yyyy',

});

HTML

<input type='text' value="05 May 2021" data-toggle="datepicker" />

【问题讨论】:

  • 试试:format: 'dd-mm-yyyy'?
  • 嗨 Shree,感谢您的回复,我试过了,但它给了我这种格式 17-05-2021 但我想要 2021 年 5 月 17 日的格式
  • 那是dd MMM yyyy,而不是dd MM yyyy。看起来该库不支持开箱即用的格式:fengyuanchen.github.io/datepicker。您必须自己将日期输出格式化为所需的格式 - 或者使用更好的库来让您使用该格式。

标签: javascript jquery jquery-ui


【解决方案1】:

每当用户选择任何日期时,您都可以按照您需要的格式对其进行自定义,并使用 .val() 在您的日期选择器中设置该值。

演示代码

$(document).ready(function() {
  $('[data-toggle="datepicker"]').datepicker({
    autoHide: true,
    pick: function(e) {
      e.preventDefault(); //prvent any default action..
      var pickedDate = e.date; //get date
      var date = e.date.getDate()
      var month = $(this).datepicker('getMonthName')
      var year = e.date.getFullYear()
      var new_date = date + " " + month + " " + year
      //set date 
      // $(this).val(`${date} ${month} ${year}`)
      $(this).val(new_date)

    }

  });
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<link href="https://fengyuanchen.github.io/datepicker/css/datepicker.css" rel="stylesheet" />
<script src="https://fengyuanchen.github.io/datepicker/js/datepicker.js"></script>

<input type='text' value="05 May 2021" data-toggle="datepicker" />

【讨论】:

  • 非常感谢 swati 的帮助,上面的解决方案在 crome 中有效,但在 10 n 11 becoz 的模板文字中无效,是否有模板文字的替代方案?
  • 尝试:var date = e.date.getDate() &lt; 10 ? ("0"+e.date.getDate()) : e.date.getDate() 这仍然不是完美的解决方案。您可以在堆栈上搜索您会发现类似的问题尝试一下。
猜你喜欢
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
  • 2012-07-22
  • 1970-01-01
相关资源
最近更新 更多