fecha
轻量级日期数据格式化和解析(~2KB)。旨在替代moment.js的解析和格式化功能。
https://github.com/taylorhakes/fecha
同 element-ui 使用的日期格式化处理解析一样,把 YYYY-MM-DD 改成了 yyyy-MM-dd 与java后台 统一格式
Fecha vs Moment
比较项 | Fecha | Moment |
---|---|---|
Size (Min. and Gzipped) | 2.1KBs | 13.1KBs |
Date Parsing | ✓ | ✓ |
Date Formatting | ✓ | ✓ |
Date Manipulation | ✓ | |
I18n Support | ✓ | ✓ |
如何使用
Formatting
fecha.format
接受日期对象(或时间戳)和字符串格式,并返回格式化的字符串。请参阅下面的可用格式标记。
Note:
fecha.format
当传递无效参数时会抛出错误
fecha.format(<Date Object>, <String Format>);
// Custom formats
fecha.format(new Date(2015, 10, 20), 'DDDD MMMM Do, yyy'); // 'Friday November 20th, 2015'
fecha.format(new Date(1998, 5, 3, 15, 23, 10, 350), 'YYYY-MM-DD hh:mm:ss.SSS A'); // '1998-06-03 03:23:10.350 PM'
// Named masks
fecha.format(new Date(2015, 10, 20), 'mediumDate'); // 'Nov 20, 2015'
fecha.format(new Date(2015, 2, 10, 5, 30, 20), 'shortTime'); // '05:30'
// Literals
fecha.format(new Date(2001, 2, 5, 6, 7, 2, 5), '[on] MM-dd-yyyy [at] HH:mm'); // 'on 03-05-2001 at 06:07'
Parsing
fecha.parse
接受日期字符串和字符串格式并返回日期对象。请参阅下面的可用格式标记。
Note:
fecha.parse
当传递无效参数时会抛出错误
// Custom formats
fecha.parse('February 3rd, 2014', 'MMMM d, yyyy'); // new Date(2014, 1, 3)
fecha.parse('10-12-10 14:11:12', 'YY-MM-DD HH:mm:ss'); // new Date(2010, 11, 10, 14, 11, 12)
// Named masks
fecha.parse('5/3/98', 'shortDate'); // new Date(1998, 4, 3)
fecha.parse('November 4, 2005', 'longDate'); // new Date(2005, 10, 4)
i18n Support
// Override fecha.i18n to support any language
fecha.i18n = {
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'],
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
amPm: ['am', 'pm'],
// D is the day of the month, function returns something like... 3rd or 11th
DoFn: function (D) {
return D + [ 'th', 'st', 'nd', 'rd' ][ D % 10 > 3 ? 0 : (D - D % 10 !== 10) * D % 10 ];
}
}
Custom Named Masks
fecha.masks = {
default: 'DDD MMM dd YYYY HH:mm:ss',
shortDate: 'M/d/YY',
mediumDate: 'MMM d, yyyy',
longDate: 'MMMM d, yyyy',
fullDate: 'DDDD, MMMM d, yyyy',
shortTime: 'HH:mm',
mediumTime: 'HH:mm:ss',
longTime: 'HH:mm:ss.SSS'
};
// Create a new mask
fecha.masks.myMask = 'HH:mm:ss yy/MM/dd';
// Use it
fecha.format(new Date(2014, 5, 6, 14, 10, 45), 'myMask'); // '14:10:45 14/06/06'
Formatting Tokens
Token | Output | |
---|---|---|
Month | M | 1 2 ... 11 12 |
MM | 01 02 ... 11 12 | |
MMM | Jan Feb ... Nov Dec | |
MMMM | January February ... November December | |
Day of Month | d | 1 2 ... 30 31 |
Do | 1st 2nd ... 30th 31st | |
dd | 01 02 ... 30 31 | |
Day of Week | D | 0 1 ... 5 6 |
DDD | Sun Mon ... Fri Sat | |
DDDD | Sunday Monday ... Friday Saturday | |
Year | yy | 70 71 ... 29 30 |
yyyy | 1970 1971 ... 2029 2030 | |
AM/PM | A | AM PM |
a | am pm | |
Hour | H | 0 1 ... 22 23 |
HH | 00 01 ... 22 23 | |
h | 1 2 ... 11 12 | |
hh | 01 02 ... 11 12 | |
Minute | m | 0 1 ... 58 59 |
mm | 00 01 ... 58 59 | |
Second | s | 0 1 ... 58 59 |
ss | 00 01 ... 58 59 | |
Fractional Second | S | 0 1 ... 8 9 |
SS | 0 1 ... 98 99 | |
SSS | 0 1 ... 998 999 | |
Timezone | ZZ | -0700 -0600 ... +0600 +0700 |
作者:hugh 创建时间:2023-12-26 11:58
最后编辑:hugh 更新时间:2024-11-25 19:17
最后编辑:hugh 更新时间:2024-11-25 19:17