【问题标题】:how to remove 0 before single number date in js flatpickr?如何在 js flatpickr 中删除单个数字日期之前的 0?
【发布时间】:2023-01-29 20:55:01
【问题描述】:

下面给出js代码:

var endDatePicker = flatpickr("#end-date-picker", {
  enableTime: false,
  singleDate: true,
  selectForward: false,
  dateFormat: "M d, Y",
  onChange: function(selectedDates, dateStr, instance) {
    document.getElementById("end-str-date").innerHTML = dateStr;
  }
});

// Set calendar for table field
  
var StartDatePicker = flatpickr("#beg-date-picker", {
  enableTime: false,
  singleDate: true,
  selectForward: false,
  dateFormat: "m/d/Y",
  year: "numeric",
  month: "1-digit",
  day: "1-digit",
  onChange: function(selectedDates, dateStr, instance) {
    document.getElementById("beg-str-date").innerHTML = dateStr;
  }
});

enter image description here

我想在单个数字日/月日期之前不使用 0 来执行此格式。

enter image description here

但它显示为零。

【问题讨论】:

    标签: javascript jquery date-formatting flatpickr


    【解决方案1】:

    只需将格式从 m/d/Y 更改为 n/j/Y

    你可以阅读更多关于它的内容in the official documentation,但我也会在这里发布:

    Character Description Example
    d Day of the month, 2 digits with leading zeros 01 to 31
    D A textual representation of a day Mon through Sun
    l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
    j Day of the month without leading zeros 1 to 31
    J Day of the month without leading zeros and ordinal suffix 1st, 2nd, to 31st
    w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday)
    W Numeric representation of the week 0 (first week of the year) through 52 (last week of the year)
    F A full textual representation of a month January through December
    m Numeric representation of a month, with leading zero 01 through 12
    n Numeric representation of a month, without leading zeros 1 through 12
    M A short textual representation of a month Jan through Dec
    U The number of seconds since the Unix Epoch 1413704993
    y A two digit representation of a year 99 or 03
    Y A full numeric representation of a year, 4 digits 1999 or 2003
    Z ISO Date format 2017-03-04T01:23:43.000Z

    还要检查工作示例。

    var endDatePicker = flatpickr("#end-date-picker", {
      enableTime: false,
      singleDate: true,
      selectForward: false,
      dateFormat: "M d, Y",
      onChange: function(selectedDates, dateStr, instance) {
        document.getElementById("end-str-date").innerHTML = dateStr;
      }
    });
    
    // Set calendar for table field
      
    var StartDatePicker = flatpickr("#beg-date-picker", {
      enableTime: false,
      singleDate: true,
      selectForward: false,
      dateFormat: "j/n/Y",
      year: "numeric",
      month: "1-digit",
      day: "1-digit",
      onChange: function(selectedDates, dateStr, instance) {
        document.getElementById("beg-str-date").innerHTML = dateStr;
      }
    });
    <head>
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
            <link rel="stylesheet" type="text/css" href="https://npmcdn.com/flatpickr/dist/themes/dark.css">
            <script src="https://npmcdn.com/flatpickr/dist/flatpickr.min.js"></script>
        <script src="https://npmcdn.com/flatpickr/dist/l10n/ru.js"></script>
    </head>
    <body>
    
    <div id="datepicker">
      <input type="text" id="end-date-picker">
        <input type="text" id="beg-date-picker">
    </div>
    <span id="end-str-date"></span>
    <br>
    <span id="beg-str-date"></span>
    </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-20
      • 2019-10-16
      • 1970-01-01
      • 2020-03-07
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多