date-format-parse
Lightweight date format and parse. Meant to replace the primary functions of format and parse of momentjs.
NPM
$ npm install date-format-parse --save
Usage
Format
Format Date to String.
import { format } from 'date-format-parse';
format(new Date(), 'YYYY-MM-DD HH:mm:ss.SSS');
const obj = { ... }
format(new Date(), 'YYYY-MM-DD', { locale: obj })
Parse
Parse String to Date
import { parse } from 'date-format-parse';
parse('2019-12-10 14:11:12', 'YYYY-MM-DD HH:mm:ss');
parse('10:00', 'HH:mm', { backupDate: new Date(2019, 5, 6) })
const obj = { ... }
parse('2019-12-10 14:11:12', 'YYYY-MM-DD HH:mm:ss', { locale: obj });
Locale
interface Locale {
months: string[];
monthsShort: string[];
weekdays: string[];
weekdaysShort: string[];
weekdaysMin: string[];
meridiem?: (hours: number, minutes: number, isLowercase: boolean) => string;
meridiemParse?: RegExp;
isPM?: (input: string) => boolean;
ordinal?: () => string;
}
const locale = {
months: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
],
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
weekdays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
weekdaysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
meridiem: (h: number, m: number, isLowercase: boolean) => {
const word = h < 12 ? 'AM' : 'PM';
return isLowercase ? word.toLocaleLowerCase() : word;
};
meridiemParse: /[ap]\.?m?\.?/i,
isPM: (input) => {
return (input + '').toLowerCase().charAt(0) === 'p';
}
};
Tokens
Uint | Token | output |
---|
Year | YY | 70 71 ... 10 11 |
| YYYY | 1970 1971 ... 2010 2011 |
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 |
| DD | 01 02 ... 30 31 |
Day of Week | d | 0 1 ... 5 6 |
| dd | Su Mo ... Fr Sa |
| ddd | Sun Mon ... Fri Sat |
| dddd | Sunday Monday ... Friday Saturday |
AM/PM | A | AM PM |
| a | am pm |
Hour | H | 0 1 ... 22 23 |
| HH | 00 01 ... 22 23 |
| h | 1 2 ... 12 |
| hh | 01 02 ... 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 | 00 01 ... 98 99 |
| SSS | 000 001 ... 998 999 |
Time Zone | Z | -07:00 -06:00 ... +06:00 +07:00 |
| ZZ | -0700 -0600 ... +0600 +0700 |
Week of Year | w | 1 2 ... 52 53 |
| ww | 01 02 ... 52 53 |
Unix Timestamp | X | 1360013296 |
Unix Millisecond Timestamp | x | 1360013296123 |