Pikaday JS 如何在没有时刻 js 的情况下使用完整的日期和月份名称作为输入格式

Han*_*com 5 javascript date date-formatting pikaday

我像这样使用 Pikaday.js:

new Pikaday({
            field: document.getElementById('top-banner-datepicker'),
            minDate: new Date()
Run Code Online (Sandbox Code Playgroud)

我知道答案就在文档中的这个例子中:

var picker = new Pikaday({
    field: document.getElementById('datepicker'),
    format: 'D/M/YYYY',
    toString(date, format) {
        // you should do formatting based on the passed format,
        // but we will just return 'D/M/YYYY' for simplicity
        const day = date.getDate();
        const month = date.getMonth() + 1;
        const year = date.getFullYear();
        return `${day}/${month}/${year}`;
    },
    parse(dateString, format) {
        // dateString is the result of `toString` method
        const parts = dateString.split('/');
        const day = parseInt(parts[0], 10);
        const month = parseInt(parts[1], 10) - 1;
        const year = parseInt(parts[2], 10);
        return new Date(year, month, day);
    }
});
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用全天(星期一、星期二、星期三等)和完整的月份名称(一月、二月等)而不是缩写(星期一、星期二、星期三……一月、二月、三月) ... 等等)

我不想使用 Moment.JS,因为它是一个巨大的依赖项。

非常感谢任何帮助!

谢谢

在此处输入图片说明

Muk*_*yuu 7

如果您希望格式化 datepicker 字段,您可以使用toLocaleString()

例如,如果您想获得十月而不是十月:

date.toLocaleString('default', {
      month: 'long' // use localestring month to get the long month
});
Run Code Online (Sandbox Code Playgroud)

如果你想得到星期日而不是星期日:

date.toLocaleString('default', { // use localestring weekday to get the long day
      weekday: 'long'
});
Run Code Online (Sandbox Code Playgroud)

示例片段:

date.toLocaleString('default', {
      month: 'long' // use localestring month to get the long month
});
Run Code Online (Sandbox Code Playgroud)
date.toLocaleString('default', { // use localestring weekday to get the long day
      weekday: 'long'
});
Run Code Online (Sandbox Code Playgroud)
var picker = new Pikaday({
  field: document.getElementById('datepicker'),
  firstDay: 1,
  minDate: new Date(),
  maxDate: new Date(2020, 12, 31),
  yearRange: [2000, 2020],
  format: 'D-M-YYYY',
  toString(date, format) {
    console.log(date.toLocaleString('default', {
      weekday: 'short' // use localestring weekday to get the short abbv of day
    }));
    console.log(date.toLocaleString('default', {
      month: 'short' // use localestring weekday to get the short abbv of month
    }));
    // you should do formatting based on the passed format,
    // but we will just return 'D/M/YYYY' for simplicity
    const day = date.getDate();
    const daylong = date.toLocaleString('default', { // use localestring weekday to get the long day
      weekday: 'long'
    });
    const month = date.getMonth() + 1;
    const monthlong = date.toLocaleString('default', {
      month: 'long' // use localestring month to get the long month
    });
    const year = date.getFullYear();
    return `${daylong}, ${monthlong}, ${day} ${year}`; // just format as you wish
  }
});
Run Code Online (Sandbox Code Playgroud)