Skip to content

Commit

Permalink
Add native implementation of format
Browse files Browse the repository at this point in the history
Closes #60
  • Loading branch information
Amorymeltzer committed Feb 27, 2020
1 parent 8a95a86 commit defa91e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,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 @@ -921,6 +921,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 @@ -941,7 +955,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 defa91e

Please sign in to comment.