Skip to content

Commit

Permalink
Added narrow weekdays
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Blackbourn committed May 1, 2023
1 parent 937d1bf commit d916d90
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@jetblack/date",
"private": false,
"version": "2.3.0",
"version": "2.4.0",
"description": "Date utilities",
"keywords": [
"date",
Expand Down
6 changes: 4 additions & 2 deletions src/formatDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Timezone } from './Timezone'

/** @internal */
const token =
/d{1,4}|D{2}|m{1,4}|yy(?:yy)?|([HhMSt])\1?|F{1,3}|W{1,2}|[opZN]|"[^"]*"|'[^']*'/g
/d{1,4}|D{2,3}|m{1,4}|yy(?:yy)?|([HhMSt])\1?|F{1,3}|W{1,2}|[opZN]|"[^"]*"|'[^']*'/g

/** @internal */
const getDayOfWeek = (date: Date, tz: Timezone): number => {
Expand All @@ -23,6 +23,7 @@ const flags: Record<
ddd: (date, tz, localeInfo) => localeInfo.weekday.short[tz.weekday(date)],
dddd: (date, tz, localeInfo) => localeInfo.weekday.long[tz.weekday(date)],
DD: (date, tz, localeInfo) => localeInfo.dayPlurals[tz.day(date) - 1],
DDD: (date, tz, localeInfo) => localeInfo.weekday.narrow[tz.weekday(date)],
m: (date, tz) => tz.monthIndex(date) + 1,
mm: (date, tz) => String(tz.monthIndex(date) + 1).padStart(2, '0'),
mmm: (date, tz, localeInfo) => localeInfo.month.short[tz.monthIndex(date)],
Expand Down Expand Up @@ -82,9 +83,10 @@ const flags: Record<
* | ------- | ----------- |
* | d | Day of the month as digits; no leading zero for single-digit days. |
* | dd | Day of the month as digits; leading zero for single-digit days. |
* | ddd | Day of the week as a three-letter abbreviation. |
* | ddd | Day of the week as the short representation (for en a three-letter abbreviation). |
* | dddd | Day of the week as its full name. |
* | DD | Day of the month with the plural suffix. |
* | DDD | Day of the week as the narrow representation. |
* | m | Month as digits; no leading zero for single-digit months. |
* | mm | Month as digits; leading zero for single-digit months. |
* | mmm | Month as a three-letter abbreviation. |
Expand Down
22 changes: 10 additions & 12 deletions test/formatDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,13 @@ describe('formatDate', () => {
})

it('should pad d when necessary', () => {
expect(formatDate(new Date('2001-01-09T00:00:00Z'), 'd', tzUtc, 'en')).toBe(
'9'
)
expect(formatDate(new Date('2001-01-19T00:00:00Z'), 'd', tzUtc, 'en')).toBe(
'19'
)
expect(formatDate(new Date('2001-01-09T00:00:00Z'), 'd', tzUtc)).toBe('9')
expect(formatDate(new Date('2001-01-19T00:00:00Z'), 'd', tzUtc)).toBe('19')
})

it('should always pad dd when necessary', () => {
expect(
formatDate(new Date('2001-01-09T00:00:00Z'), 'dd', tzUtc, 'en')
).toBe('09')
expect(
formatDate(new Date('2001-01-19T00:00:00Z'), 'dd', tzUtc, 'en')
).toBe('19')
expect(formatDate(new Date('2001-01-09T00:00:00Z'), 'dd', tzUtc)).toBe('09')
expect(formatDate(new Date('2001-01-19T00:00:00Z'), 'dd', tzUtc)).toBe('19')
})

it('should get ddd', () => {
Expand All @@ -43,6 +35,12 @@ describe('formatDate', () => {
).toBe('Tuesday')
})

it('should get DDD', () => {
expect(
formatDate(new Date('2001-01-09T00:00:00Z'), 'DDD', tzUtc, 'en')
).toBe('T')
})

it('should pluralize', () => {
expect(
formatDate(new Date('2001-01-01T00:00:00Z'), 'DD', tzUtc, 'en')
Expand Down

0 comments on commit d916d90

Please sign in to comment.