CustomParseFormat
CustomParseFormat 扩展了 dayjs() 构造函数以支持输入字符串的自定义格式。
var customParseFormat = require("dayjs/plugin/customParseFormat");
// import customParseFormat from 'dayjs/plugin/customParseFormat' // ES 2015
dayjs.extend(customParseFormat);
dayjs("05/02/69 1:02:03 PM -05:00", "MM/DD/YY H:mm:ss A Z");
// Returns an instance containing '1969-05-02T18:02:03.000Z'
dayjs("2018 Enero 15", "YYYY MMMM DD", "es");
// Returns an instance containing '2018-01-15T00:00:00.000Z'
dayjs("1970-00-00", "YYYY-MM-DD", true); // strict parsing
所有可用解析令牌的列表
输入 | 示例 | 描述 |
---|---|---|
YY | 01 | 两位数年份 |
YYYY | 2001 | 四位数年份 |
M | 1-12 | 月份,从 1 开始 |
MM | 01-12 | 月份,2 位数字 |
MMM | Jan-Dec | 月份名称缩写 |
MMMM | January-December | 完整的月份名称 |
D | 1-31 | 一个月中的第几天 |
DD | 01-31 | 月份中的第几天,2 位数字 |
H | 0-23 | 小时数 |
HH | 00-23 | 小时,2 位数字 |
h | 1-12 | 小时,12 小时制 |
hh | 01-12 | 小时,12 小时制,2 位数字 |
m | 0-59 | 分钟数 |
mm | 00-59 | 分钟,2 位数字 |
s | 0-59 | 秒数 |
ss | 00-59 | 秒,2 位数字 |
S | 0-9 | 数百毫秒,一位数 |
SS | 00-99 | 几十毫秒,2 位 |
SSS | 000-999 | 毫秒,3 位数字 |
Z | -05:00 | 与 UTC 的偏移 |
ZZ | -0500 | 距 UTC 的紧凑偏移量,2 位数字 |
A | 上午下午 | 午后或午前,大写 |
a | am pm | 午后或午前,小写 |
Do | 1st...31st | 带序数的月份中的某一天 |
X | 1410715640.579 | Unix 时间戳 |
x | 1410715640579 | Unix 毫秒时间戳 |