Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native implementation of format #83

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test failed. Native new Date does not support am/pm as far as i know :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed/passes for me (and in Travis?), but does adding hour12: true in the object do it for you?

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