diff --git a/README.md b/README.md index 226ad3e..e8aaa61 100644 --- a/README.md +++ b/README.md @@ -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 | ✅ | ✅ | ✅ | ✅ | @@ -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'); @@ -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" ``` diff --git a/__tests__/index.js b/__tests__/index.js index 2486063..fdc905a 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -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);