gpt4 book ai didi

date - Flatpickr 和 Moment.js 意外的日期格式

转载 作者:行者123 更新时间:2023-12-02 00:45:51 24 4
gpt4 key购买 nike

我正在为 Flatpickr 使用以下实例化代码。

$("#entry_date_time").flatpickr({
enableTime: true,
altInput: true,
defaultDate: moment().format("YYYY-MM-DD HH:MM:SS"),
dateFormat: "YYYY-MM-DD HH:MM:SS",
minuteIncrement: 1
});

我遇到的问题是 moment().format("YYYY-MM-DD HH:MM:SS"); 给了我正确的数据,但是 的输出$("#entry_date_time").val() 等于

2017201720172017-JanJan-SatSat 0000:JanJan:0000

而不是我提供的预期格式。

任何关于可能导致此问题的想法都很好,感谢您的帮助!

最佳答案

Flatpickr 有自己的 formating tokens与 moment 支持的不同。但好处是您可以使用 parseDateformatDate 配置选项来支持自定义格式标记。

/* A custom datestring parser */
parseDate: (date: string, format: string) => Date;

/* Allows using a custom date formatting function instead of the built-in. Generally unnecessary. */
formatDate: (date: Date, format: string, locale: Locale) => string;

带有 moment.js 兼容 token 的示例 https://jsfiddle.net/armujahid/pwqhznj0/

const fp = flatpickr(".date", {
altInput: true,
dateFormat: "YYYY-MM-DD",
altFormat: "DD-MM-YYYY",
allowInput: true,
parseDate: (datestr, format) => {
return moment(datestr, format, true).toDate();
},
formatDate: (date, format, locale) => {
// locale can also be used
return moment(date).format(format);
}
});

引用: 我在 https://github.com/flatpickr/flatpickr/issues/1549#issuecomment-537939826 的评论

更新:此代码段也已添加到 official docs

关于date - Flatpickr 和 Moment.js 意外的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43967298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com