Skip to content

Commit

Permalink
Add native implementation of format
Browse files Browse the repository at this point in the history
  • Loading branch information
Amorymeltzer committed Oct 28, 2019
1 parent ae06823 commit 3d7828c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ If you are not using timezone but only a few simple functions from moment.js, th
| [Moment.js](https://momentjs.com/) | 329K(69.6K) | No | 42.1k | High | OO | Good(moment-timezone) | 123 |
| [Luxon](https://moment.github.io/luxon/) | 59.9K(17.2K) | No | 8.6k | High | OO | Good(Intl) | - |
| [date-fns](https://date-fns.org) | 78.4k(13.4k) without tree-shaking | Yes | 19k | High | Functional | Not yet | 50 |
| [dayjs](https://github.com/iamkun/dayjs) | 6.5k(2.6k) without plugins | No | 23k | High | OO | Not yet | 130 |
| [dayjs](https://github.com/iamkun/dayjs) | 6.5k(2.6k) without plugins | No | 23k | High | OO | Not yet | 130 |

## Voice of Developers

Expand Down Expand Up @@ -140,7 +140,7 @@ npm install --save-dev eslint-plugin-you-dont-need-momentjs
| Day of Week |||||
| Day of Year |||||
| Week of Year |||| ⚠️ |
| Days in Month |||||
| Days in Month |||| |
| Weeks in Year |||| ⚠️ |
| Maximum of the given dates |||| ⚠️ |
| Minimum of the given dates |||| ⚠️ |
Expand All @@ -152,7 +152,7 @@ npm install --save-dev eslint-plugin-you-dont-need-momentjs
| End of Time |||||
| | | | | |
| **Display** | | | | |
| Format | ||||
| Format | ||||
| Time from now |||| ⚠️ |
| Time from X |||| ⚠️ |
| Difference |||||
Expand Down Expand Up @@ -915,6 +915,20 @@ moment().format('dddd, MMMM Do YYYY, h:mm:ss A');
moment().format('ddd, hA');
// => "Sun, 7PM"

// Native
new Date().toLocaleDateString(undefined, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
});
// => "Sunday, September 9, 2018, 7:12:49 PM" ⚠️ does not support 9th
new Date().toLocaleDateString(undefined, { weekday: 'short', hour: 'numeric' });
// => "Sun, 7 PM"

// date-fns
import format from 'date-fns/format';
format(new Date(), 'eeee, MMMM do YYYY, h:mm:ss aa');
Expand All @@ -935,7 +949,7 @@ dayjs().format('dddd, MMMM Do YYYY, h:mm:ss A');

// Luxon
DateTime.fromMillis(time).toFormat('EEEE, MMMM dd yyyy, h:mm:ss a');
// => "Sunday, September 9 2018, 7:12:49 PM" ⚠️ not support 9th
// => "Sunday, September 9 2018, 7:12:49 PM" ⚠️ does not support 9th
DateTime.fromMillis(time).toFormat('EEE, ha');
// => "Sun, 7PM"
```
Expand Down
31 changes: 23 additions & 8 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,22 +395,37 @@ describe('Manipulate', () => {

describe('Display', () => {
it('Format', () => {
const m = moment(time).format('dddd, MMMM D YYYY, h:mm:ss A');
const d = date.format(new Date(time), 'eeee, MMMM d yyyy, h:mm:ss aa', {
const m = moment(time).format('dddd, MMMM D, YYYY, h:mm:ss A');
const n = new Date(time).toLocaleDateString(undefined, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
});
const d = date.format(new Date(time), 'eeee, MMMM d, yyyy, h:mm:ss aa', {
awareOfUnicodeTokens: true,
});
const day = dayjs(time).format('dddd, MMMM D YYYY, h:mm:ss A');
const day = dayjs(time).format('dddd, MMMM D, YYYY, h:mm:ss A');
const l = DateTime.fromMillis(time).toFormat(
'EEEE, MMMM d yyyy, h:mm:ss a'
'EEEE, MMMM d, yyyy, h:mm:ss a'
);
expect(m).toBe(n);
expect(m).toBe(d);
expect(m).toBe(day);
expect(m).toBe(l);

const m2 = moment(time).format('ddd, hA');
const d2 = date.format(new Date(time), 'eee, ha');
const day2 = dayjs(time).format('ddd, hA');
const l2 = DateTime.fromMillis(time).toFormat('EEE, ha');
const m2 = moment(time).format('ddd, h A');
const n2 = new Date(time).toLocaleDateString(undefined, {
weekday: 'short',
hour: 'numeric',
});
const d2 = date.format(new Date(time), 'eee, h a');
const day2 = dayjs(time).format('ddd, h A');
const l2 = DateTime.fromMillis(time).toFormat('EEE, h a');
expect(m2).toBe(n2);
expect(m2).toBe(d2);
expect(m2).toBe(day2);
expect(m2).toBe(l2);
Expand Down

0 comments on commit 3d7828c

Please sign in to comment.