Skip to content
Open
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
32 changes: 17 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,23 @@ formatDate(date, 'YYYY-MM-DD hh:mm:ss A')

The format is a combination of the following masks:

| Mask | Description | Example |
| ------ | --------------------- | ---------- |
| `YYYY` | 4-digits year | `1984` |
| `YY` | 2-digits year | `84` |
| `MMMM` | Month name | `January` |
| `MMM` | Short month name | `Jan` |
| `DD` | 2-digits day | `17` |
| `dddd` | Day of the week | `Tuesday` |
| `ddd` | Short day of the week | `Tue` |
| `A` | Day period | `AM`, `PM` |
| `a` | Lowercased day period | `am`, `pm` |
| `HH` | 24-hours hour | `16` |
| `hh` | 12-hours hour | `04` |
| `mm` | 2-digit minute | `13` |
| `ss` | 2-digit second | `37` |
| Mask | Description | Example |
| ------ | --------------------- | ------------------|
| `YYYY` | 4-digits year | `1984` |
| `YY` | 2-digits year | `84` |
| `MMMM` | Month name | `January` |
| `MMM` | Short month name | `Jan` |
| `DD` | 2-digits day | `17` |
| `dddd` | Day of the week | `Tuesday` |
| `ddd` | Short day of the week | `Tue` |
| `A` | Day period | `AM`, `PM` |
| `a` | Lowercased day period | `am`, `pm` |
| `HH` | 24-hours hour | `16` |
| `hh` | 12-hours hour | `04` |
| `mm` | 2-digit minute | `13` |
| `ss` | 2-digit second | `37` |
| `Z` | Short Timezone Name | `EST` |
| `ZZ` | Long Timezone Name | `America/Pacific` |

Masks will be replaced by their associated date part.

Expand Down
6 changes: 4 additions & 2 deletions src/formatDate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CustomFormatters, DateParts, Formatters, FormatterMask } from './types'

const defaultPattern = '[YMDdAaHhms]+'
const defaultPattern = '[ZYMDdAaHhms]+'

const formatters: Formatters = {
YYYY: parts => parts.year,
Expand All @@ -17,7 +17,9 @@ const formatters: Formatters = {
HH: parts => ('0' + (Number(parts.lhour) % 24)).slice(-2),
hh: parts => parts.hour,
mm: parts => parts.minute,
ss: parts => parts.second
ss: parts => parts.second,
Z: parts => parts.timeZoneName,
ZZ: parts => parts.ltimeZoneName,
}

const createCustomPattern = (customFormatters: CustomFormatters) =>
Expand Down
6 changes: 4 additions & 2 deletions src/parseDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const intlFormattersOptions = [
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
second: '2-digit',
timeZoneName: 'short',
},
{
month: 'long',
hour: '2-digit',
hour12: false
hour12: false,
timeZoneName: 'long',
}
]

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type DatePartName =
| 'lhour'
| 'minute'
| 'second'
| 'timeZoneName'
| 'ltimeZoneName'

export type DateParts = { [k in DatePartName]: string }

Expand All @@ -31,6 +33,8 @@ export type FormatterMask =
| 'hh'
| 'mm'
| 'ss'
| 'Z'
| 'ZZ'

export type Formatter = (tokens: DateParts, date: Date) => string

Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default {
lhour: '16',
hour: '04',
minute: '13',
second: '37'
second: '37',
timeZoneName: 'UTC',
ltimeZoneName: 'Coordinated Universal Time'
}

export const tokensDate = new Date(Date.UTC(1984, 0, 17, 16, 13, 37, 0))
Loading