首页 > 解决方案 > 如何使用 jquery 将“2020-10-08 09:38:08”转换为 2020 年 3 月格式

问题描述

$.datepicker.formatDate('M dd', date)

我已经尝试过了,但它给了我错误

jquery-ui.js:8924 Uncaught TypeError: date.getDate is not a function

标签: jquerydatejquery-uidate-formattingjquerydatetimepicker

解决方案


formatDate方法需要一个Date实例。

如果您从该特定字符串开始,您可以轻松地将其转换为 ISO 8601 格式,然后将其解析为Date实例

const date = "2020-10-08 09:38:08"
const dateInstance = new Date(date.replace(" ", "T"))

const formatted = $.datepicker.formatDate('M dd', dateInstance)

推荐阅读