Gathering detailed insights and metrics for fecha
Gathering detailed insights and metrics for fecha
Gathering detailed insights and metrics for fecha
Gathering detailed insights and metrics for fecha
npm install fecha
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Lightweight date formatting and parsing (~2KB). Meant to replace parsing and formatting functionality of moment.js.
npm install fecha --save
yarn add fecha
Fecha | Moment | |
---|---|---|
Size (Min. and Gzipped) | 2.1KBs | 13.1KBs |
Date Parsing | ✓ | ✓ |
Date Formatting | ✓ | ✓ |
Date Manipulation | ✓ | |
I18n Support | ✓ | ✓ |
format
accepts a Date object (or timestamp) and a string format and returns a formatted string. See below for
available format tokens.
Note: format
will throw an error when passed invalid parameters
1import { format } from 'fecha'; 2 3type format = (date: Date, format?: string, i18n?: I18nSettings) => str; 4 5// Custom formats 6format(new Date(2015, 10, 20), 'dddd MMMM Do, YYYY'); // 'Friday November 20th, 2015' 7format(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' 8 9// Named masks 10format(new Date(2015, 10, 20), 'isoDate'); // '2015-11-20' 11format(new Date(2015, 10, 20), 'mediumDate'); // 'Nov 20, 2015' 12format(new Date(2015, 10, 20, 3, 2, 1), 'isoDateTime'); // '2015-11-20T03:02:01-05:00' 13format(new Date(2015, 2, 10, 5, 30, 20), 'shortTime'); // '05:30' 14 15// Literals 16format(new Date(2001, 2, 5, 6, 7, 2, 5), '[on] MM-DD-YYYY [at] HH:mm'); // 'on 03-05-2001 at 06:07'
parse
accepts a Date string and a string format and returns a Date object. See below for available format tokens.
NOTE: parse
will throw an error when passed invalid string format or missing format. You MUST specify a format.
1import { parse } from 'fecha'; 2 3type parse = (dateStr: string, format: string, i18n?: I18nSettingsOptional) => Date|null; 4 5// Custom formats 6parse('February 3rd, 2014', 'MMMM Do, YYYY'); // new Date(2014, 1, 3) 7parse('10-12-10 14:11:12', 'YY-MM-DD HH:mm:ss'); // new Date(2010, 11, 10, 14, 11, 12) 8 9// Named masks 10parse('5/3/98', 'shortDate'); // new Date(1998, 4, 3) 11parse('November 4, 2005', 'longDate'); // new Date(2005, 10, 4) 12parse('2015-11-20T03:02:01-05:00', 'isoDateTime'); // new Date(2015, 10, 20, 3, 2, 1) 13 14// Override i18n 15parse('4 de octubre de 1983', 'M de MMMM de YYYY', { 16 monthNames: [ 17 'enero', 18 'febrero', 19 'marzo', 20 'abril', 21 'mayo', 22 'junio', 23 'julio', 24 'agosto', 25 'septiembre', 26 'octubre', 27 'noviembre', 28 'diciembre' 29 ] 30}); // new Date(1983, 9, 4)
1import {setGlobalDateI18n} from 'fecha'; 2 3/* 4Default I18n Settings 5{ 6 dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'], 7 dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], 8 monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], 9 monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], 10 amPm: ['am', 'pm'], 11 // D is the day of the month, function returns something like... 3rd or 11th 12 DoFn(dayOfMonth) { 13 return dayOfMonth + [ 'th', 'st', 'nd', 'rd' ][ dayOfMonth % 10 > 3 ? 0 : (dayOfMonth - dayOfMonth % 10 !== 10) * dayOfMonth % 10 ]; 14 } 15} 16*/ 17 18setGlobalDateI18n({ 19 dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'], 20 dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], 21 monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], 22 monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], 23 amPm: ['am', 'pm'], 24 // D is the day of the month, function returns something like... 3rd or 11th 25 DoFn: function (D) { 26 return D + [ 'th', 'st', 'nd', 'rd' ][ D % 10 > 3 ? 0 : (D - D % 10 !== 10) * D % 10 ]; 27 } 28}); 29
1import { format, setGlobalDateMasks } from 'fecha'; 2/* 3Default global masks 4{ 5 default: 'ddd MMM DD YYYY HH:mm:ss', 6 shortDate: 'M/D/YY', 7 mediumDate: 'MMM D, YYYY', 8 longDate: 'MMMM D, YYYY', 9 fullDate: 'dddd, MMMM D, YYYY', 10 shortTime: 'HH:mm', 11 mediumTime: 'HH:mm:ss', 12 longTime: 'HH:mm:ss.SSS' 13} 14*/ 15 16// Create a new mask 17setGlobalDateMasks({ 18 myMask: 'HH:mm:ss YY/MM/DD'; 19}); 20 21// Use it 22format(new Date(2014, 5, 6, 14, 10, 45), 'myMask'); // '14:10:45 14/06/06'
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 | Z | -07:00 -06:00 ... +06:00 +07:00 |
ZZ | -0700 -0600 ... +0600 +0700 |
No vulnerabilities found.
No security vulnerabilities found.