{"id":24874630,"url":"https://github.com/dirheimerb/date-fns-format","last_synced_at":"2025-03-27T02:15:41.852Z","repository":{"id":236991956,"uuid":"793583782","full_name":"dirheimerb/date-fns-format","owner":"dirheimerb","description":"Format options for date-fns","archived":false,"fork":false,"pushed_at":"2024-04-29T14:01:46.000Z","size":90,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T07:28:59.563Z","etag":null,"topics":["date-fns","format"],"latest_commit_sha":null,"homepage":"https://github.com/dirheimerb/#readme.md","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dirheimerb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-29T13:50:10.000Z","updated_at":"2024-04-29T14:01:49.000Z","dependencies_parsed_at":"2024-04-29T15:11:42.552Z","dependency_job_id":null,"html_url":"https://github.com/dirheimerb/date-fns-format","commit_stats":null,"previous_names":["dirheimerb/date-fns-format"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirheimerb%2Fdate-fns-format","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirheimerb%2Fdate-fns-format/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirheimerb%2Fdate-fns-format/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dirheimerb%2Fdate-fns-format/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dirheimerb","download_url":"https://codeload.github.com/dirheimerb/date-fns-format/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245767360,"owners_count":20668826,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["date-fns","format"],"created_at":"2025-02-01T07:28:03.068Z","updated_at":"2025-03-27T02:15:41.832Z","avatar_url":"https://github.com/dirheimerb.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\nDate: 2024-04-29\nTitle: Date Format Utility with date-fns\n---\n\n## Date Format Utility with `date-fns`\n\n## Introduction\n\nThis document describes the use of the `DateFormat` enum in conjunction with `date-fns` to standardize date formatting across a JavaScript/TypeScript project. By encapsulating commonly used date format strings into an enum, we enhance code readability, maintainability, and reduce the risk of typos and inconsistencies.\n\n## Overview\n\nThe `DateFormat` enum includes a comprehensive list of date format options that can be used with the `date-fns` library to format dates. This approach standardizes date formatting throughout the application and ensures consistency across different parts of the application.\n\n## Installation\n\nFirst, ensure that you have `date-fns` installed in your project. If not, you can add it by running:\n\n```bash\nnpm install date-fns\n```\n\n## Usage\n\nHere’s how to use the `DateFormat` enum in your project:\n\n### Basic Usage\n\nImport the enum and the `format` function from `date-fns` wherever you need to format dates:\n\n```typescript\nimport { format } from 'date-fns';\nimport { DateFormat } from 'date-fns-format';\n\nconst today = new Date();\nconst formattedDate = format(today, DateFormat.YearFourDigits);\nconsole.log('Formatted Year:', formattedDate);\n```\n\n### Advanced Usage\n\nYou can use the enum in more complex scenarios such as components or services where multiple date formats are required:\n\n```typescript\nimport { format } from 'date-fns';\nimport { DateFormat } from './path/to/DateFormat';\n\nconst logFormattedDates = (date: Date) =\u003e {\n    console.log('Full Date:', format(date, DateFormat.LongLocalizedDate));\n    console.log('Month:', format(date, DateFormat.MonthFull));\n    console.log('Weekday:', format(date, DateFormat.DayOfWeekFull));\n}\n```\n\n## Benefits\n\n-*Consistency**: Using the enum ensures that date formats are consistent throughout the application.\n-*Maintenance**: Easy to update and maintain date formats in one location.\n-*Readability**: More readable codebase with clear references to date formats instead of raw strings.\n\n## Conclusion\n\nUsing the `DateFormat` enum with `date-fns` enhances your project's date handling by providing a clear, maintainable method for dealing with date formats. This method reduces errors and improves the development experience.\n\n## Table Display\n\nAccepted patterns:\n| Unit                            | Pattern  | Result examples                   | Notes |\n| ------------------------------- | -------- | --------------------------------- | ----- |\n| Era                             | G..GGG   | AD, BC                            |       |\n|                                 | GGGG     | Anno Domini, Before Christ        | 2     |\n|                                 | GGGGG    | A, B                              |       |\n| Calendar year                   | y        | 44, 1, 1900, 2017                 | 5     |\n|                                 | yo       | 44th, 1st, 0th, 17th              | 5,7   |\n|                                 | yy       | 44, 01, 00, 17                    | 5     |\n|                                 | yyy      | 044, 001, 1900, 2017              | 5     |\n|                                 | yyyy     | 0044, 0001, 1900, 2017            | 5     |\n|                                 | yyyyy    | ...                               | 3,5   |\n| Local week-numbering year       | Y        | 44, 1, 1900, 2017                 | 5     |\n|                                 | Yo       | 44th, 1st, 1900th, 2017th         | 5,7   |\n|                                 | YY       | 44, 01, 00, 17                    | 5,8   |\n|                                 | YYY      | 044, 001, 1900, 2017              | 5     |\n|                                 | YYYY     | 0044, 0001, 1900, 2017            | 5,8   |\n|                                 | YYYYY    | ...                               | 3,5   |\n| ISO week-numbering year         | R        | -43, 0, 1, 1900, 2017             | 5,7   |\n|                                 | RR       | -43, 00, 01, 1900, 2017           | 5,7   |\n|                                 | RRR      | -043, 000, 001, 1900, 2017        | 5,7   |\n|                                 | RRRR     | -0043, 0000, 0001, 1900, 2017     | 5,7   |\n|                                 | RRRRR    | ...                               | 3,5,7 |\n| Extended year                   | u        | -43, 0, 1, 1900, 2017             | 5     |\n|                                 | uu       | -43, 01, 1900, 2017               | 5     |\n|                                 | uuu      | -043, 001, 1900, 2017             | 5     |\n|                                 | uuuu     | -0043, 0001, 1900, 2017           | 5     |\n|                                 | uuuuu    | ...                               | 3,5   |\n| Quarter (formatting)            | Q        | 1, 2, 3, 4                        |       |\n|                                 | Qo       | 1st, 2nd, 3rd, 4th                | 7     |\n|                                 | QQ       | 01, 02, 03, 04                    |       |\n|                                 | QQQ      | Q1, Q2, Q3, Q4                    |       |\n|                                 | QQQQ     | 1st quarter, 2nd quarter, ...     | 2     |\n|                                 | QQQQQ    | 1, 2, 3, 4                        | 4     |\n| Quarter (stand-alone)           | q        | 1, 2, 3, 4                        |       |\n|                                 | qo       | 1st, 2nd, 3rd, 4th                | 7     |\n|                                 | qq       | 01, 02, 03, 04                    |       |\n|                                 | qqq      | Q1, Q2, Q3, Q4                    |       |\n|                                 | qqqq     | 1st quarter, 2nd quarter, ...     | 2     |\n|                                 | qqqqq    | 1, 2, 3, 4                        | 4     |\n| Month (formatting)              | M        | 1, 2, ..., 12                     |       |\n|                                 | Mo       | 1st, 2nd, ..., 12th               | 7     |\n|                                 | MM       | 01, 02, ..., 12                   |       |\n|                                 | MMM      | Jan, Feb, ..., Dec                |       |\n|                                 | MMMM     | January, February, ..., December  | 2     |\n|                                 | MMMMM    | J, F, ..., D                      |       |\n| Month (stand-alone)             | L        | 1, 2, ..., 12                     |       |\n|                                 | Lo       | 1st, 2nd, ..., 12th               | 7     |\n|                                 | LL       | 01, 02, ..., 12                   |       |\n|                                 | LLL      | Jan, Feb, ..., Dec                |       |\n|                                 | LLLL     | January, February, ..., December  | 2     |\n|                                 | LLLLL    | J, F, ..., D                      |       |\n| Local week of year              | w        | 1, 2, ..., 53                     |       |\n|                                 | wo       | 1st, 2nd, ..., 53th               | 7     |\n|                                 | ww       | 01, 02, ..., 53                   |       |\n| ISO week of year                | I        | 1, 2, ..., 53                     | 7     |\n|                                 | Io       | 1st, 2nd, ..., 53th               | 7     |\n|                                 | II       | 01, 02, ..., 53                   | 7     |\n| Day of month                    | d        | 1, 2, ..., 31                     |       |\n|                                 | do       | 1st, 2nd, ..., 31st               | 7     |\n|                                 | dd       | 01, 02, ..., 31                   |       |\n| Day of year                     | D        | 1, 2, ..., 365, 366               | 9     |\n|                                 | Do       | 1st, 2nd, ..., 365th, 366th       | 7     |\n|                                 | DD       | 01, 02, ..., 365, 366             | 9     |\n|                                 | DDD      | 001, 002, ..., 365, 366           |       |\n|                                 | DDDD     | ...                               | 3     |\n| Day of week (formatting)        | E..EEE   | Mon, Tue, Wed, ..., Sun           |       |\n|                                 | EEEE     | Monday, Tuesday, ..., Sunday      | 2     |\n|                                 | EEEEE    | M, T, W, T, F, S, S               |       |\n|                                 | EEEEEE   | Mo, Tu, We, Th, Fr, Sa, Su        |       |\n| ISO day of week (formatting)    | i        | 1, 2, 3, ..., 7                   | 7     |\n|                                 | io       | 1st, 2nd, ..., 7th                | 7     |\n|                                 | ii       | 01, 02, ..., 07                   | 7     |\n|                                 | iii      | Mon, Tue, Wed, ..., Sun           | 7     |\n|                                 | iiii     | Monday, Tuesday, ..., Sunday      | 2,7   |\n|                                 | iiiii    | M, T, W, T, F, S, S               | 7     |\n|                                 | iiiiii   | Mo, Tu, We, Th, Fr, Sa, Su        | 7     |\n| Local day of week (formatting)  | e        | 2, 3, 4, ..., 1                   |       |\n|                                 | eo       | 2nd, 3rd, ..., 1st                | 7     |\n|                                 | ee       | 02, 03, ..., 01                   |       |\n|                                 | eee      | Mon, Tue, Wed, ..., Sun           |       |\n|                                 | eeee     | Monday, Tuesday, ..., Sunday      | 2     |\n|                                 | eeeee    | M, T, W, T, F, S, S               |       |\n|                                 | eeeeee   | Mo, Tu, We, Th, Fr, Sa, Su        |       |\n| Local day of week (stand-alone) | c        | 2, 3, 4, ..., 1                   |       |\n|                                 | co       | 2nd, 3rd, ..., 1st                | 7     |\n|                                 | cc       | 02, 03, ..., 01                   |       |\n|                                 | ccc      | Mon, Tue, Wed, ..., Sun           |       |\n|                                 | cccc     | Monday, Tuesday, ..., Sunday      | 2     |\n|                                 | ccccc    | M, T, W, T, F, S, S               |       |\n|                                 | cccccc   | Mo, Tu, We, Th, Fr, Sa, Su        |       |\n| AM, PM                          | a..aa    | AM, PM                            |       |\n|                                 | aaa      | am, pm                            |       |\n|                                 | aaaa     | a.m., p.m.                        | 2     |\n|                                 | aaaaa    | a, p                              |       |\n| AM, PM, noon, midnight          | b..bb    | AM, PM, noon, midnight            |       |\n|                                 | bbb      | am, pm, noon, midnight            |       |\n|                                 | bbbb     | a.m., p.m., noon, midnight        | 2     |\n|                                 | bbbbb    | a, p, n, mi                       |       |\n| Flexible day period             | B..BBB   | at night, in the morning, ...     |       |\n|                                 | BBBB     | at night, in the morning, ...     | 2     |\n|                                 | BBBBB    | at night, in the morning, ...     |       |\n| Hour [1-12]                     | h        | 1, 2, ..., 11, 12                 |       |\n|                                 | ho       | 1st, 2nd, ..., 11th, 12th         | 7     |\n|                                 | hh       | 01, 02, ..., 11, 12               |       |\n| Hour [0-23]                     | H        | 0, 1, 2, ..., 23                  |       |\n|                                 | Ho       | 0th, 1st, 2nd, ..., 23rd          | 7     |\n|                                 | HH       | 00, 01, 02, ..., 23               |       |\n| Hour [0-11]                     | K        | 1, 2, ..., 11, 0                  |       |\n|                                 | Ko       | 1st, 2nd, ..., 11th, 0th          | 7     |\n|                                 | KK       | 01, 02, ..., 11, 00               |       |\n| Hour [1-24]                     | k        | 24, 1, 2, ..., 23                 |       |\n|                                 | ko       | 24th, 1st, 2nd, ..., 23rd         | 7     |\n|                                 | kk       | 24, 01, 02, ..., 23               |       |\n| Minute                          | m        | 0, 1, ..., 59                     |       |\n|                                 | mo       | 0th, 1st, ..., 59th               | 7     |\n|                                 | mm       | 00, 01, ..., 59                   |       |\n| Second                          | s        | 0, 1, ..., 59                     |       |\n|                                 | so       | 0th, 1st, ..., 59th               | 7     |\n|                                 | ss       | 00, 01, ..., 59                   |       |\n| Fraction of second              | S        | 0, 1, ..., 9                      |       |\n|                                 | SS       | 00, 01, ..., 99                   |       |\n|                                 | SSS      | 000, 001, ..., 999                |       |\n|                                 | SSSS     | ...                               | 3     |\n| Timezone (ISO-8601 w/ Z)        | X        | -08, +0530, Z                     |       |\n|                                 | XX       | -0800, +0530, Z                   |       |\n|                                 | XXX      | -08:00, +05:30, Z                 |       |\n|                                 | XXXX     | -0800, +0530, Z, +123456          | 2     |\n|                                 | XXXXX    | -08:00, +05:30, Z, +12:34:56      |       |\n| Timezone (ISO-8601 w/o Z)       | x        | -08, +0530, +00                   |       |\n|                                 | xx       | -0800, +0530, +0000               |       |\n|                                 | xxx      | -08:00, +05:30, +00:00            | 2     |\n|                                 | xxxx     | -0800, +0530, +0000, +123456      |       |\n|                                 | xxxxx    | -08:00, +05:30, +00:00, +12:34:56 |       |\n| Timezone (GMT)                  | O...OOO  | GMT-8, GMT+5:30, GMT+0            |       |\n|                                 | OOOO     | GMT-08:00, GMT+05:30, GMT+00:00   | 2     |\n| Timezone (specific non-locat.)  | z...zzz  | GMT-8, GMT+5:30, GMT+0            | 6     |\n|                                 | zzzz     | GMT-08:00, GMT+05:30, GMT+00:00   | 2,6   |\n| Seconds timestamp               | t        | 512969520                         | 7     |\n|                                 | tt       | ...                               | 3,7   |\n| Milliseconds timestamp          | T        | 512969520900                      | 7     |\n|                                 | TT       | ...                               | 3,7   |\n| Long localized date             | P        | 04/29/1453                        | 7     |\n|                                 | PP       | Apr 29, 1453                      | 7     |\n|                                 | PPP      | April 29th, 1453                  | 7     |\n|                                 | PPPP     | Friday, April 29th, 1453          | 2,7   |\n| Long localized time             | p        | 12:00 AM                          | 7     |\n|                                 | pp       | 12:00:00 AM                       | 7     |\n|                                 | ppp      | 12:00:00 AM GMT+2                 | 7     |\n|                                 | pppp     | 12:00:00 AM GMT+02:00             | 2,7   |\n| Combination of date and time    | Pp       | 04/29/1453, 12:00 AM              | 7     |\n|                                 | PPpp     | Apr 29, 1453, 12:00:00 AM         | 7     |\n|                                 | PPPppp   | April 29th, 1453 at ...           | 7     |\n|                                 | PPPPpppp | Friday, April 29th, 1453 at ...   | 2,7   |\n\nNotes:\n\n1. \"Formatting\" units (e.g. formatting quarter) in the default en-US locale\n   are the same as \"stand-alone\" units, but are different in some languages.\n   \"Formatting\" units are declined according to the rules of the language\n   in the context of a date. \"Stand-alone\" units are always nominative singular:\n\n   `format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=\u003e '6. listopad'`\n\n   `format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=\u003e '6. listopadu'`\n\n2. Any sequence of the identical letters is a pattern, unless it is escaped by\n   the single quote characters (see below).\n   If the sequence is longer than listed in table (e.g. `EEEEEEEEEEE`)\n   the output will be the same as default pattern for this unit, usually\n   the longest one (in case of ISO weekdays, `EEEE`). Default patterns for units\n   are marked with \"2\" in the last column of the table.\n\n   `format(new Date(2017, 10, 6), 'MMM') //=\u003e 'Nov'`\n\n   `format(new Date(2017, 10, 6), 'MMMM') //=\u003e 'November'`\n\n   `format(new Date(2017, 10, 6), 'MMMMM') //=\u003e 'N'`\n\n   `format(new Date(2017, 10, 6), 'MMMMMM') //=\u003e 'November'`\n\n   `format(new Date(2017, 10, 6), 'MMMMMMM') //=\u003e 'November'`\n\n3. Some patterns could be unlimited length (such as `yyyyyyyy`).\n   The output will be padded with zeros to match the length of the pattern.\n\n   `format(new Date(2017, 10, 6), 'yyyyyyyy') //=\u003e '00002017'`\n\n4. `QQQQQ` and `qqqqq` could be not strictly numerical in some locales.\n   These tokens represent the shortest form of the quarter.\n\n5. The main difference between `y` and `u` patterns are B.C. years:\n\n   | Year | `y` | `u` |\n   | ---- | --- | --- |\n   | AC 1 | 1   | 1   |\n   | BC 1 | 1   | 0   |\n   | BC 2 | 2   | -1  |\n\n   Also `yy` always returns the last two digits of a year,\n   while `uu` pads single digit years to 2 characters and returns other years unchanged:\n\n   | Year | `yy` | `uu` |\n   | ---- | ---- | ---- |\n   | 1    | 01   | 01   |\n   | 14   | 14   | 14   |\n   | 376  | 76   | 376  |\n   | 1453 | 53   | 1453 |\n\n   The same difference is true for local and ISO week-numbering years (`Y` and `R`),\n   except local week-numbering years are dependent on `options.weekStartsOn`\n   and `options.firstWeekContainsDate` (compare [getISOWeekYear](https://date-fns.org/docs/getISOWeekYear)\n   and [getWeekYear](https://date-fns.org/docs/getWeekYear)).\n\n6. Specific non-location timezones are currently unavailable in `date-fns`,\n   so right now these tokens fall back to GMT timezones.\n\n7. These patterns are not in the Unicode Technical Standard #35:\n   - `i`: ISO day of week\n   - `I`: ISO week of year\n   - `R`: ISO week-numbering year\n   - `t`: seconds timestamp\n   - `T`: milliseconds timestamp\n   - `o`: ordinal number modifier\n   - `P`: long localized date\n   - `p`: long localized time\n\n8. `YY` and `YYYY` tokens represent week-numbering years but they are often confused with years.\n   You should enable `options.useAdditionalWeekYearTokens` to use them. See: [unicodeTokens](https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md)\n\n9. `D` and `DD` tokens represent days of the year but they are often confused with days of the month.\n   You should enable `options.useAdditionalDayOfYearTokens` to use them. See: [unicodeTokens](https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md)\n\n@typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).\n\n@param date - The original date\n@param format - The string of tokens\n@param options - An object with options\n\n@returns The formatted date string\n\n@throws `date` must not be Invalid Date\n@throws `options.locale` must contain `localize` property\n@throws `options.locale` must contain `formatLong` property\n@throws use `yyyy` instead of `YYYY` for formatting years using [format provided] to the input [input provided]; see: [unicodeTokens](https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md)\n@throws use `yy` instead of `YY` for formatting years using [format provided] to the input [input provided]; see: [unicodeTokens](https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md)\n@throws use `d` instead of `D` for formatting days of the month using [format provided] to the input [input provided]; see: [unicodeTokens](https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md)\n@throws use `dd` instead of `DD` for formatting days of the month using [format provided] to the input [input provided]; see: [unicodeTokens](https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md)\n@throws format string contains an unescaped latin alphabet character\n\n@example\n// Represent 11 February 2014 in middle-endian format:\nconst result = format(new Date(2014, 1, 11), 'MM/dd/yyyy')\n//=\u003e '02/11/2014'\n\n@example\n// Represent 2 July 2014 in Esperanto:\nimport { eoLocale } from 'date-fns/locale/eo'\nconst result = format(new Date(2014, 6, 2), \"do 'de' MMMM yyyy\", {\n  locale: eoLocale\n})\n//=\u003e '2-a de julio 2014'\n\n@example\n\n```js\n// Escape string by single quote characters:\nconst result = format(new Date(2014, 6, 2, 15), \"h 'o''clock'\")\n//=\u003e \"3 o'clock\"\n```\n\n## License\n\nSpecify your project's license here, if applicable.\n\n## Deployment\n\nInclude additional sections if necessary, detailing deployment processes, additional configurations, or contributions guidelines, depending on the complexity and usage of your project.\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirheimerb%2Fdate-fns-format","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdirheimerb%2Fdate-fns-format","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdirheimerb%2Fdate-fns-format/lists"}