├── .dockerignore
├── .editorconfig
├── .github
└── workflows
│ └── deploy.yml
├── .gitignore
├── Dockerfile
├── LICENSE
├── README.md
├── crowdin.yaml
├── docker-compose.yml
├── docs
├── customization
│ ├── calendar.md
│ ├── customization.md
│ ├── month-abbreviations.md
│ ├── month-names.md
│ ├── relative-time.md
│ ├── weekday-abbreviations.md
│ ├── weekday-min.md
│ └── weekday-names.md
├── display
│ ├── as-array.md
│ ├── as-iso-string.md
│ ├── as-javascript-date.md
│ ├── as-json.md
│ ├── as-object.md
│ ├── as-string.md
│ ├── calendar-time.md
│ ├── days-in-month.md
│ ├── difference.md
│ ├── display.md
│ ├── format.md
│ ├── from-now.md
│ ├── from.md
│ ├── to-now.md
│ ├── to.md
│ ├── unix-timestamp-milliseconds.md
│ └── unix-timestamp.md
├── durations
│ ├── add.md
│ ├── as-iso-string.md
│ ├── as-json.md
│ ├── as.md
│ ├── clone.md
│ ├── creating.md
│ ├── days.md
│ ├── diffing.md
│ ├── durations.md
│ ├── format.md
│ ├── get.md
│ ├── hours.md
│ ├── humanize.md
│ ├── is-a-duration.md
│ ├── locale.md
│ ├── milliseconds.md
│ ├── minutes.md
│ ├── months.md
│ ├── seconds.md
│ ├── subtract.md
│ ├── weeks.md
│ └── years.md
├── get-set
│ ├── date.md
│ ├── day-of-year.md
│ ├── day.md
│ ├── get-set.md
│ ├── get.md
│ ├── hour.md
│ ├── iso-week-year.md
│ ├── iso-week.md
│ ├── iso-weekday.md
│ ├── iso-weeks-in-year.md
│ ├── max.md
│ ├── millisecond.md
│ ├── min.md
│ ├── minute.md
│ ├── month.md
│ ├── quarter.md
│ ├── second.md
│ ├── set.md
│ ├── week-year.md
│ ├── week.md
│ ├── weekday.md
│ └── year.md
├── i18n
│ ├── changing-locale.md
│ ├── getting-locale.md
│ ├── i18n.md
│ ├── instance-locale.md
│ ├── listing-months-weekdays.md
│ ├── loading-into-browser.md
│ ├── loading-into-nodejs.md
│ └── locale-data.md
├── installation
│ ├── browser.md
│ ├── download.md
│ ├── installation.md
│ ├── node-js.md
│ └── typescript.md
├── manipulate
│ ├── add.md
│ ├── end-of.md
│ ├── local.md
│ ├── manipulate.md
│ ├── start-of.md
│ ├── subtract.md
│ ├── utc-offset.md
│ └── utc.md
├── parse
│ ├── array.md
│ ├── date.md
│ ├── dayjs-clone.md
│ ├── is-valid.md
│ ├── now.md
│ ├── object.md
│ ├── parse.md
│ ├── string-format.md
│ ├── string.md
│ ├── unix-timestamp-milliseconds.md
│ ├── unix-timestamp.md
│ └── utc.md
├── plugin
│ ├── advanced-format.md
│ ├── array-support.md
│ ├── bad-mutable.md
│ ├── bigint-support.md
│ ├── buddhist-era.md
│ ├── calendar.md
│ ├── custom-parse-format.md
│ ├── day-of-year.md
│ ├── dev-helper.md
│ ├── duration.md
│ ├── is-between.md
│ ├── is-leap-year.md
│ ├── is-same-or-after.md
│ ├── is-same-or-before.md
│ ├── is-today.md
│ ├── is-tomorrow.md
│ ├── is-yesterday.md
│ ├── iso-week.md
│ ├── iso-weeks-in-year.md
│ ├── loading-into-browser.md
│ ├── loading-into-nodejs.md
│ ├── locale-data.md
│ ├── localized-format.md
│ ├── min-max.md
│ ├── object-support.md
│ ├── plugin.md
│ ├── plural-get-set.md
│ ├── preparse-postformat.md
│ ├── quarter-of-year.md
│ ├── relative-time.md
│ ├── timezone.md
│ ├── to-array.md
│ ├── to-object.md
│ ├── update-locale.md
│ ├── utc.md
│ ├── week-of-year.md
│ ├── week-year.md
│ └── weekday.md
├── query
│ ├── is-a-dayjs.md
│ ├── is-after.md
│ ├── is-before.md
│ ├── is-between.md
│ ├── is-leap-year.md
│ ├── is-same-or-after.md
│ ├── is-same-or-before.md
│ ├── is-same.md
│ └── query.md
└── timezone
│ ├── converting-to-zone.md
│ ├── guessing-user-timezone.md
│ ├── parsing-in-zone.md
│ ├── set-default-timezone.md
│ └── timezone.md
└── website
├── .prettierrc
├── README.md
├── core
└── Footer.js
├── languages.js
├── package.json
├── pages
└── en
│ └── index.js
├── plugin.js
├── pnpm-lock.yaml
├── sidebars.json
├── siteConfig.js
├── static
├── .circleci
│ └── config.yml
├── baidu_verify_turlRX3rKX.html
├── baidu_verify_ubAnOPDvDq.html
├── css
│ └── custom.css
├── handsontable.png
├── img
│ ├── favicon.ico
│ ├── logo.png
│ └── sponsors
│ │ ├── chudovo.svg
│ │ ├── duohui-cn.png
│ │ └── duohui.png
└── js
│ ├── add.js
│ └── sponsors.js
└── yarn.lock
/.dockerignore:
--------------------------------------------------------------------------------
1 | */node_modules
2 | *.log
3 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | charset = utf-8
5 | end_of_line = lf
6 | insert_final_newline = true
7 |
--------------------------------------------------------------------------------
/.github/workflows/deploy.yml:
--------------------------------------------------------------------------------
1 | name: Deploy to GitHub Pages
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 |
8 | jobs:
9 | deploy:
10 | name: Deploy to GitHub Pages
11 | if: github.ref == 'refs/heads/master'
12 | runs-on: ubuntu-latest
13 | steps:
14 | - uses: actions/checkout@v2
15 | - uses: pnpm/action-setup@v2
16 | with:
17 | package_json_file: 'website/package.json'
18 | version: 7
19 | - name: Git config
20 | env:
21 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22 | run: |
23 | git config --global user.name github-actions[bot]
24 | git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com
25 | echo "machine github.com login github-actions[bot] password ${GITHUB_TOKEN}" > ~/.netrc
26 | - uses: actions/setup-node@v3
27 | with:
28 | node-version: 16.x
29 | cache: 'pnpm'
30 | cache-dependency-path: 'website/pnpm-lock.yaml'
31 | - name: Install dependencies
32 | run: pnpm install
33 | working-directory: 'website'
34 | - name: Write English translations
35 | run: pnpm run write-translations
36 | working-directory: 'website'
37 | - name: Upload && download translations
38 | env:
39 | CROWDIN_DAYJS_PROJECT_API_KEY: ${{ secrets.CROWDIN_DAYJS_PROJECT_API_KEY }}
40 | run: |
41 | pnpm run crowdin-upload
42 | pnpm run crowdin-download
43 | working-directory: 'website'
44 | - name: Publish to GitHub Pages
45 | run: GIT_USER=github-actions[bot] pnpm run publish-gh-pages
46 | working-directory: 'website'
47 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .idea
3 |
4 | node_modules
5 |
6 | lib/core/metadata.js
7 | lib/core/MetadataBlog.js
8 |
9 | website/translated_docs
10 | website/build/
11 | website/node_modules
12 | website/i18n/*
13 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM node:8.11.4
2 |
3 | WORKDIR /app/website
4 |
5 | EXPOSE 3000 35729
6 | COPY ./docs /app/docs
7 | COPY ./website /app/website
8 | RUN yarn install
9 |
10 | CMD ["yarn", "start"]
11 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 iamkun
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |

4 | Fast 2kB alternative to Moment.js with the same modern API
5 |
6 |
7 | [Day.js](https://github.com/iamkun/dayjs) website repo.
8 |
--------------------------------------------------------------------------------
/crowdin.yaml:
--------------------------------------------------------------------------------
1 | project_id: 365295
2 | api_token_env: "CROWDIN_DAYJS_PROJECT_API_KEY"
3 | base_path: "./"
4 | preserve_hierarchy: true
5 |
6 | files:
7 | [
8 | {
9 | source: "/docs/**/*.md",
10 | translation: "/website/translated_docs/%locale%/**/%original_file_name%",
11 | },
12 | {
13 | source: "/website/i18n/en.json",
14 | translation: "/website/i18n/%locale%.json",
15 | },
16 | ]
17 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: "3"
2 |
3 | services:
4 | docusaurus:
5 | build: .
6 | ports:
7 | - 3000:3000
8 | - 35729:35729
9 | volumes:
10 | - ./docs:/app/docs
11 | - ./website/blog:/app/website/blog
12 | - ./website/core:/app/website/core
13 | - ./website/i18n:/app/website/i18n
14 | - ./website/pages:/app/website/pages
15 | - ./website/static:/app/website/static
16 | - ./website/sidebars.json:/app/website/sidebars.json
17 | - ./website/siteConfig.js:/app/website/siteConfig.js
18 | working_dir: /app/website
19 |
--------------------------------------------------------------------------------
/docs/customization/calendar.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: calendar
3 | title: Calendar
4 | ---
5 |
6 | `Locale#calendar` should have the following formatting strings.
7 |
8 | @>UpdateLocale
9 |
10 | ```js
11 | dayjs.extend(updateLocale)
12 |
13 | dayjs.updateLocale('en', {
14 | calendar: {
15 | lastDay: '[Yesterday at] LT',
16 | sameDay: '[Today at] LT',
17 | nextDay: '[Tomorrow at] LT',
18 | lastWeek: '[last] dddd [at] LT',
19 | nextWeek: 'dddd [at] LT',
20 | sameElse: 'L'
21 | }
22 | })
23 | ```
24 |
25 | Each of the `Locale#calendar` keys can also be a callback function with the scope of the current Day.js object and first argument a Day.js object that depicts now. It should return a formatting string.
26 |
27 | ```js
28 | function callback (now) {
29 | return '[hoy a la' + ((this.hour() !== 1) ? 's' : '') + ']' + now.format();
30 | }
31 | ```
32 |
--------------------------------------------------------------------------------
/docs/customization/customization.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: customization
3 | title: Customize
4 | ---
5 |
6 | Day.js is very easy to customize.
7 |
8 | You can create a new locale.
9 |
10 | ```js
11 | var localeObject = {...} // Day.js locale Object, detailed below
12 | dayjs.locale('en-my-settings', localeObject);
13 | ```
14 |
15 | Update an existing locale.
16 |
17 | @>UpdateLocale
18 |
19 | ```js
20 | dayjs.extend(updateLocale)
21 |
22 | dayjs.updateLocale('en', {
23 | /**/
24 | })
25 | ```
26 |
27 | Template of a Day.js locale Object.
28 |
29 | ```javascript
30 | const localeObject = {
31 | name: 'es', // name String
32 | weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array
33 | weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
34 | weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
35 | weekStart: 1, // OPTIONAL, set the start of a week. If the value is 1, Monday will be the start of week instead of Sunday。
36 | yearStart: 4, // OPTIONAL, the week that contains Jan 4th is the first week of the year.
37 | months: 'Enero_Febrero ... '.split('_'), // months Array
38 | monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
39 | ordinal: n => `${n}º`, // ordinal Function (number) => return number + output
40 | formats: {
41 | // abbreviated format options allowing localization
42 | LTS: 'h:mm:ss A',
43 | LT: 'h:mm A',
44 | L: 'MM/DD/YYYY',
45 | LL: 'MMMM D, YYYY',
46 | LLL: 'MMMM D, YYYY h:mm A',
47 | LLLL: 'dddd, MMMM D, YYYY h:mm A',
48 | // lowercase/short, optional formats for localization
49 | l: 'D/M/YYYY',
50 | ll: 'D MMM, YYYY',
51 | lll: 'D MMM, YYYY h:mm A',
52 | llll: 'ddd, MMM D, YYYY h:mm A'
53 | },
54 | relativeTime: {
55 | // relative time format strings, keep %s %d as the same
56 | future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
57 | past: '%s ago',
58 | s: 'a few seconds',
59 | m: 'a minute',
60 | mm: '%d minutes',
61 | h: 'an hour',
62 | hh: '%d hours', // e.g. 2 hours, %d been replaced with 2
63 | d: 'a day',
64 | dd: '%d days',
65 | M: 'a month',
66 | MM: '%d months',
67 | y: 'a year',
68 | yy: '%d years'
69 | },
70 | meridiem: (hour, minute, isLowercase) => {
71 | // OPTIONAL, AM/PM
72 | return hour > 12 ? 'PM' : 'AM'
73 | }
74 | }
75 | ```
76 |
77 | Template of a Day.js locale file (e.g dayjs/locale/es.js).
78 |
79 | ```javascript
80 | import dayjs from 'dayjs'
81 |
82 | const locale = { ... } // Your Day.js locale Object.
83 |
84 | dayjs.locale(locale, null, true) // load locale for later use
85 |
86 | export default locale
87 | ```
88 |
89 |
--------------------------------------------------------------------------------
/docs/customization/month-abbreviations.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: month-abbreviations
3 | title: Month Abbreviations
4 | ---
5 |
6 | `Locale#monthsShort` should be an array of the month abbreviations.
7 |
8 | @>UpdateLocale
9 |
10 | ```js
11 | dayjs.extend(updateLocale)
12 |
13 | dayjs.updateLocale('en', {
14 | monthsShort: [
15 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
16 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
17 | ]
18 | })
19 | ```
20 |
21 | `Locale#monthsShort` can be a callback function the same as [`Locale#months`](./month-names#additional-token-processing).
22 |
--------------------------------------------------------------------------------
/docs/customization/month-names.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: month-names
3 | title: Month Names
4 | ---
5 |
6 | `Locale#months` should be an array of the month names.
7 |
8 | @>UpdateLocale
9 |
10 | ```js
11 | dayjs.extend(updateLocale)
12 |
13 | dayjs.updateLocale('en', {
14 | months: [
15 | "January", "February", "March", "April", "May", "June", "July",
16 | "August", "September", "October", "November", "December"
17 | ]
18 | })
19 | ```
20 |
21 | #### Additional token processing
22 |
23 | If you need more processing to calculate the name of the month, (for example, if there is different grammar for different formats), `Locale#months` can be a function with the following signature. It should always return a month name.
24 |
25 | ```js
26 | dayjs.updateLocale("en", {
27 | months: function (dayjsInstance, format) {
28 | // dayjsInstance is the Day.js object currently being formatted
29 | // format is the formatting string
30 | if (/^MMMM/.test(format)) {
31 | // if the format starts with 'MMMM'
32 | return monthShortFormat[dayjsInstance.month()];
33 | } else {
34 | return monthShortStandalone[dayjsInstance.month()];
35 | }
36 | },
37 | });
38 | ```
39 |
--------------------------------------------------------------------------------
/docs/customization/relative-time.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: relative-time
3 | title: Relative Time
4 | ---
5 |
6 | `Locale#relativeTime` should be an object of the replacement strings for `dayjs#from`.
7 |
8 | @>UpdateLocale
9 |
10 | ```js
11 | dayjs.extend(updateLocale)
12 |
13 | dayjs.updateLocale('en', {
14 | relativeTime: {
15 | future: "in %s",
16 | past: "%s ago",
17 | s: 'a few seconds',
18 | m: "a minute",
19 | mm: "%d minutes",
20 | h: "an hour",
21 | hh: "%d hours",
22 | d: "a day",
23 | dd: "%d days",
24 | M: "a month",
25 | MM: "%d months",
26 | y: "a year",
27 | yy: "%d years"
28 | }
29 | })
30 | ```
31 |
32 | `Locale#relativeTime.future` refers to the prefix/suffix for future dates. `Locale#relativeTime.past` refers to the prefix/suffix for past dates.
33 |
34 | For all others, a single character refers to the singular, and a double character refers to the plural.
35 |
36 | #### Additional token processing
37 |
38 | If a locale requires additional processing for a token, it can set the token as a function with the following signature instead of a string. The function should return a string.
39 |
40 | ```js
41 | relativeTime: {
42 | ...,
43 | yy: function (number, withoutSuffix, key, isFuture) {
44 | return string;
45 | }
46 | }
47 | ```
48 |
49 | The `number` argument refers to the number of units for that key. For `m`, the number is the number of minutes, etc.
50 |
51 | The `withoutSuffix` argument will be true if the token will be displayed without a suffix, and false if it will be displayed with a suffix. (The reason for the inverted logic is because the default behavior is to display with the suffix.)
52 |
53 | The `key` argument refers to the replacement key in the `Locale#relativeTime` object. (eg. `s m mm h`, etc.)
54 |
55 | The `isFuture` argument will be true if it is going to use the future suffix/prefix and false if it is going to use the past prefix/suffix.
56 |
57 | #### Relative Time thresholds and rounding
58 |
59 | You can pass a config object while using this plugin to update its thresholds and rounding config.
60 |
61 | ```js
62 | var config = {
63 | thresholds: [{}],
64 | rounding: function
65 | }
66 | dayjs.extend(relativeTime, config)
67 | ```
68 |
69 | `thresholds` is an `Array` of `Object` defined when a unit is considered a minute, an hour and so on. For example, by default more than 45 seconds is considered a minute, more than 22 hours is considered a day and so on. To change those you can pass a new `thresholds` like this.
70 |
71 | ```js
72 | // strict thresholds
73 | var thresholds = [
74 | { l: 's', r: 1 },
75 | { l: 'm', r: 1 },
76 | { l: 'mm', r: 59, d: 'minute' },
77 | { l: 'h', r: 1 },
78 | { l: 'hh', r: 23, d: 'hour' },
79 | { l: 'd', r: 1 },
80 | { l: 'dd', r: 29, d: 'day' },
81 | { l: 'M', r: 1 },
82 | { l: 'MM', r: 11, d: 'month' },
83 | { l: 'y', r: 1 },
84 | { l: 'yy', d: 'year' }
85 | ]
86 | ```
87 |
88 | You can also add your own thresholds key and update locale accordingly.
89 |
90 | ```js
91 | var thresholds = [
92 | ...,
93 | { l: 'ss', r: 59, d: 'second' }
94 | ]
95 | dayjs.updateLocale('en', {
96 | relativeTime: {
97 | ...,
98 | ss: "%d seconds"
99 | }
100 | })
101 | ```
102 |
103 | `rounding` is an `Function` to process the number before supplying it to the relativeTime format string specified in the locale. To change those you can pass a new `rounding` like this.
104 |
105 | ```js
106 | // Math.round by default
107 | var rounding = Math.floor
108 | ```
109 |
--------------------------------------------------------------------------------
/docs/customization/weekday-abbreviations.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: weekday-abbreviations
3 | title: Weekday Abbreviations
4 | ---
5 |
6 | `Locale#weekdaysShort` should be an array of the weekdays abbreviations.
7 |
8 | @>UpdateLocale
9 |
10 | ```js
11 | dayjs.extend(updateLocale)
12 |
13 | dayjs.updateLocale('en', {
14 | weekdaysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
15 | })
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/customization/weekday-min.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: weekday-min
3 | title: Minimal Weekday Abbreviations
4 | ---
5 |
6 | `Locale#weekdaysMin` should be an array of two letter weekday abbreviations.
7 |
8 | @>UpdateLocale
9 |
10 | ```js
11 | dayjs.extend(updateLocale)
12 |
13 | dayjs.updateLocale('en', {
14 | weekdaysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
15 | })
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/customization/weekday-names.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: weekday-names
3 | title: Weekday Names
4 | ---
5 |
6 | `Locale#weekdays` should be an array of the weekdays names.
7 |
8 | @>UpdateLocale
9 |
10 | ```js
11 | dayjs.extend(updateLocale)
12 |
13 | dayjs.updateLocale('en', {
14 | weekdays: [
15 | "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
16 | ]
17 | })
18 | ```
19 |
--------------------------------------------------------------------------------
/docs/display/as-array.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as-array
3 | title: As Array
4 | ---
5 |
6 | Returns an array that mirrors the parameters
7 |
8 | @>ToArray
9 |
10 | ```js
11 | dayjs.extend(toArray)
12 |
13 | dayjs('2019-01-25').toArray() // [ 2019, 0, 25, 0, 0, 0, 0 ]
14 | ```
15 |
--------------------------------------------------------------------------------
/docs/display/as-iso-string.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as-iso-string
3 | title: As ISO 8601 String
4 | ---
5 |
6 | To format as an ISO 8601 string.
7 |
8 | ```js
9 | dayjs('2019-01-25').toISOString() // '2019-01-25T02:00:00.000Z'
10 | ```
11 |
--------------------------------------------------------------------------------
/docs/display/as-javascript-date.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as-javascript-date
3 | title: As Javascript Date
4 | ---
5 |
6 | To get a copy of the native `Date` object parsed from the Day.js object use `dayjs#toDate`.
7 |
8 | ```js
9 | dayjs('2019-01-25').toDate()
10 | ```
11 |
--------------------------------------------------------------------------------
/docs/display/as-json.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as-json
3 | title: As JSON
4 | ---
5 |
6 | To serialize as an ISO 8601 string.
7 |
8 | ```js
9 | dayjs('2019-01-25').toJSON() // '2019-01-25T02:00:00.000Z'
10 | ```
11 |
--------------------------------------------------------------------------------
/docs/display/as-object.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as-object
3 | title: As Object
4 | ---
5 |
6 | Returns an object with the date's properties.
7 |
8 | @>ToObject
9 |
10 | ```js
11 | dayjs.extend(toObject)
12 |
13 | dayjs('2019-01-25').toObject()
14 | /* { years: 2019,
15 | months: 0,
16 | date: 25,
17 | hours: 0,
18 | minutes: 0,
19 | seconds: 0,
20 | milliseconds: 0 } */
21 | ```
22 |
--------------------------------------------------------------------------------
/docs/display/as-string.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as-string
3 | title: As String
4 | ---
5 |
6 | Returns a string representation of the date.
7 |
8 | ```js
9 | dayjs('2019-01-25').toString() // 'Fri, 25 Jan 2019 02:00:00 GMT'
10 | ```
11 |
--------------------------------------------------------------------------------
/docs/display/calendar-time.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: calendar-time
3 | title: Calendar Time
4 | ---
5 |
6 | Calendar time displays time relative to a given reference time (defaults to now) but does so slightly differently than `dayjs#fromNow`.
7 |
8 | @>Calendar
9 |
10 | ```js
11 | dayjs.extend(calendar)
12 |
13 | dayjs().calendar()
14 | dayjs().calendar(dayjs('2008-01-01'))
15 | ```
16 |
17 | |Key| Value|
18 | | ------ | ----- |
19 | |Last week (lastWeek)| Last Monday at 2:30 AM|
20 | |The day before (lastDay)| Yesterday at 2:30 AM|
21 | |The same day (sameDay) |Today at 2:30 AM|
22 | |The next day (nextDay) |Tomorrow at 2:30 AM|
23 | |The next week (nextWeek) |Sunday at 2:30 AM|
24 | |Everything else (sameElse) |7/10/2011|
25 |
26 | These strings are localized, and [can be customized](../customization/calendar).
27 |
28 | You can also pass specifying calendar output formats as the second parameter.
29 |
30 | To escape characters, wrap them in square brackets (e.g. [Today]).
31 |
32 | ```js
33 | dayjs().calendar(null, {
34 | sameDay: '[Today at] h:mm A', // The same day ( Today at 2:30 AM )
35 | nextDay: '[Tomorrow]', // The next day ( Tomorrow at 2:30 AM )
36 | nextWeek: 'dddd', // The next week ( Sunday at 2:30 AM )
37 | lastDay: '[Yesterday]', // The day before ( Yesterday at 2:30 AM )
38 | lastWeek: '[Last] dddd', // Last week ( Last Monday at 2:30 AM )
39 | sameElse: 'DD/MM/YYYY' // Everything else ( 7/10/2011 )
40 | })
41 | ```
42 |
--------------------------------------------------------------------------------
/docs/display/days-in-month.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: days-in-month
3 | title: Days in Month
4 | ---
5 |
6 | Get the number of days in the current month.
7 |
8 | ```js
9 | dayjs('2019-01-25').daysInMonth() // 31
10 | ```
11 |
--------------------------------------------------------------------------------
/docs/display/difference.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: difference
3 | title: Difference
4 | ---
5 |
6 | This indicates the difference between two date-time in the specified unit.
7 |
8 | To get the difference in milliseconds, use `dayjs#diff`.
9 |
10 | ```js
11 | const date1 = dayjs('2019-01-25')
12 | const date2 = dayjs('2018-06-05')
13 | date1.diff(date2) // 20214000000 default milliseconds
14 | ```
15 |
16 | To get the difference in another unit of measurement, pass that measurement as the second argument.
17 |
18 | ```js
19 | const date1 = dayjs('2019-01-25')
20 | date1.diff('2018-06-05', 'month') // 7
21 | ```
22 |
23 | By default, `dayjs#diff` will truncate the result to zero decimal places, returning an integer. If you want a floating point number, pass true as the third argument.
24 |
25 | ```js
26 | const date1 = dayjs('2019-01-25')
27 | date1.diff('2018-06-05', 'month', true) // 7.645161290322581
28 | ```
29 |
30 | #### List of all available units
31 |
32 | Units are case insensitive, and support plural and short forms. Note, short forms are case sensitive.
33 |
34 | | Unit | Shorthand | Description |
35 | | ------------- | --------- | ---------------------------------------- |
36 | | `day` | `d` | Day |
37 | | `week` | `w` | Week of Year |
38 | | `quarter` | `Q` | Quarter |
39 | | `month` | `M` | Month (January as 0, December as 11) |
40 | | `year` | `y` | Year |
41 | | `hour` | `h` | Hour |
42 | | `minute` | `m` | Minute |
43 | | `second` | `s` | Second |
44 | | `millisecond` | `ms` | Millisecond |
45 |
46 |
--------------------------------------------------------------------------------
/docs/display/display.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: display
3 | title: Display
4 | ---
5 |
6 | Once parsing and manipulation are done, you need some way to display the Day.js object.
7 |
--------------------------------------------------------------------------------
/docs/display/format.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: format
3 | title: Format
4 | ---
5 |
6 | Get the formatted date according to the string of tokens passed in.
7 |
8 | To escape characters, wrap them in square brackets (e.g. `[MM]`).
9 |
10 | ```js
11 | dayjs().format()
12 | // current date in ISO8601, without fraction seconds e.g. '2020-04-02T08:02:17-05:00'
13 |
14 | dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]')
15 | // 'YYYYescape 2019-01-25T00:00:00-02:00Z'
16 |
17 | dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
18 | ```
19 |
20 | #### List of all available formats
21 |
22 | | Format | Output | Description |
23 | | ------ | ---------------- | ------------------------------------- |
24 | | `YY` | 18 | Two-digit year |
25 | | `YYYY` | 2018 | Four-digit year |
26 | | `M` | 1-12 | The month, beginning at 1 |
27 | | `MM` | 01-12 | The month, 2-digits |
28 | | `MMM` | Jan-Dec | The abbreviated month name |
29 | | `MMMM` | January-December | The full month name |
30 | | `D` | 1-31 | The day of the month |
31 | | `DD` | 01-31 | The day of the month, 2-digits |
32 | | `d` | 0-6 | The day of the week, with Sunday as 0 |
33 | | `dd` | Su-Sa | The min name of the day of the week |
34 | | `ddd` | Sun-Sat | The short name of the day of the week |
35 | | `dddd` | Sunday-Saturday | The name of the day of the week |
36 | | `H` | 0-23 | The hour |
37 | | `HH` | 00-23 | The hour, 2-digits |
38 | | `h` | 1-12 | The hour, 12-hour clock |
39 | | `hh` | 01-12 | The hour, 12-hour clock, 2-digits |
40 | | `m` | 0-59 | The minute |
41 | | `mm` | 00-59 | The minute, 2-digits |
42 | | `s` | 0-59 | The second |
43 | | `ss` | 00-59 | The second, 2-digits |
44 | | `SSS` | 000-999 | The millisecond, 3-digits |
45 | | `Z` | +05:00 | The offset from UTC, ±HH:mm |
46 | | `ZZ` | +0500 | The offset from UTC, ±HHmm |
47 | | `A` | AM PM | |
48 | | `a` | am pm | |
49 | | ... | ... | Other formats @>>AdvancedFormat |
50 |
51 | - More available formats `Q Do k kk X x ...` in plugin [`AdvancedFormat`](../plugin/advanced-format)
52 |
53 | ### Localized formats
54 | Because preferred formatting differs based on locale, there are a few localized format tokens that can be used based on its locale.
55 |
56 | @>LocalizedFormat
57 |
58 | ```javascript
59 | dayjs.extend(LocalizedFormat)
60 | dayjs().format('L LT')
61 | ```
62 |
63 | #### List of localized formats
64 | | Format | English Locale | Sample Output |
65 | | ------ | ------------------------- | --------------------------------- |
66 | | `LT` | h:mm A | 8:02 PM |
67 | | `LTS` | h:mm:ss A | 8:02:18 PM |
68 | | `L` | MM/DD/YYYY | 08/16/2018 |
69 | | `LL` | MMMM D, YYYY | August 16, 2018 |
70 | | `LLL` | MMMM D, YYYY h:mm A | August 16, 2018 8:02 PM |
71 | | `LLLL` | dddd, MMMM D, YYYY h:mm A | Thursday, August 16, 2018 8:02 PM |
72 | | `l` | M/D/YYYY | 8/16/2018 |
73 | | `ll` | MMM D, YYYY | Aug 16, 2018 |
74 | | `lll` | MMM D, YYYY h:mm A | Aug 16, 2018 8:02 PM |
75 | | `llll` | ddd, MMM D, YYYY h:mm A | Thu, Aug 16, 2018 8:02 PM |
76 |
77 |
--------------------------------------------------------------------------------
/docs/display/from-now.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: from-now
3 | title: Time from now
4 | ---
5 |
6 | Returns the string of relative time from now.
7 |
8 | @>RelativeTime
9 |
10 | ```js
11 | dayjs.extend(relativeTime)
12 |
13 | dayjs('1999-01-01').fromNow() // 22 years ago
14 | ```
15 |
16 | If you pass true, you can get the value without the suffix.
17 |
18 | ```js
19 | dayjs.extend(relativeTime)
20 |
21 | dayjs('1999-01-01').fromNow(true) // 22 years
22 | ```
23 |
24 | #### List of breakdown range
25 |
26 | The base strings are localized by the current locale and [can be customized](../customization/relative-time). Time is rounded to the nearest second.
27 |
28 | | Range | Key | Sample Output |
29 | | ------------------------ | --- | -------------------------------- |
30 | | 0 to 44 seconds | s | a few seconds ago |
31 | | 45 to 89 seconds | m | a minute ago |
32 | | 90 seconds to 44 minutes | mm | 2 minutes ago ... 44 minutes ago |
33 | | 45 to 89 minutes | h | an hour ago |
34 | | 90 minutes to 21 hours | hh | 2 hours ago ... 21 hours ago |
35 | | 22 to 35 hours | d | a day ago |
36 | | 36 hours to 25 days | dd | 2 days ago ... 25 days ago |
37 | | 26 to 45 days | M | a month ago |
38 | | 46 days to 10 months | MM | 2 months ago ... 10 months ago |
39 | | 11 months to 17months | y | a year ago |
40 | | 18 months+ | yy | 2 years ago ... 20 years ago |
41 |
--------------------------------------------------------------------------------
/docs/display/from.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: from
3 | title: Time from X
4 | ---
5 |
6 | Returns the string of relative time from X.
7 |
8 | @>RelativeTime
9 |
10 | ```js
11 | dayjs.extend(relativeTime)
12 |
13 | var a = dayjs('2000-01-01')
14 |
15 | dayjs('1999-01-01').from(a) // a year ago
16 | ```
17 |
18 | If you pass true, you can get the value without the suffix.
19 |
20 | ```js
21 | dayjs.extend(relativeTime)
22 |
23 | var a = dayjs('2000-01-01')
24 |
25 | dayjs('1999-01-01').from(a, true) // a year
26 | ```
27 |
28 | [List of breakdown range](../display/from-now#list-of-breakdown-range)
29 |
--------------------------------------------------------------------------------
/docs/display/to-now.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: to-now
3 | title: Time to now
4 | ---
5 |
6 | Returns the string of relative time to now.
7 |
8 | @>RelativeTime
9 |
10 | ```js
11 | dayjs.extend(relativeTime)
12 |
13 | dayjs('1999-01-01').toNow() // in 22 years
14 | ```
15 |
16 | If you pass true, you can get the value without the suffix.
17 |
18 | ```js
19 | dayjs.extend(relativeTime)
20 |
21 | dayjs('1999-01-01').toNow(true) // 22 years
22 | ```
23 |
24 | [List of breakdown range](../display/from-now#list-of-breakdown-range)
25 |
--------------------------------------------------------------------------------
/docs/display/to.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: to
3 | title: Time to X
4 | ---
5 |
6 | Returns the string of relative time to X.
7 |
8 | @>RelativeTime
9 |
10 | ```js
11 | dayjs.extend(relativeTime)
12 |
13 | var a = dayjs('2000-01-01')
14 |
15 | dayjs('1999-01-01').to(a) // in a year
16 | ```
17 |
18 | If you pass true, you can get the value without the suffix.
19 |
20 | ```js
21 | dayjs.extend(relativeTime)
22 |
23 | var a = dayjs('2000-01-01')
24 |
25 | dayjs('1999-01-01').to(a, true) // a year
26 | ```
27 |
28 | [List of breakdown range](../display/from-now#list-of-breakdown-range)
29 |
--------------------------------------------------------------------------------
/docs/display/unix-timestamp-milliseconds.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: unix-timestamp-milliseconds
3 | title: Unix Timestamp (milliseconds)
4 | ---
5 |
6 | This returns the number of milliseconds since the Unix Epoch of the Day.js object.
7 |
8 | ```js
9 | dayjs('2019-01-25').valueOf() // 1548381600000
10 | +dayjs(1548381600000) // 1548381600000
11 | ```
12 |
13 | To get a Unix timestamp (the number of **seconds** since the epoch) from a Day.js object, you should use [Unix Timestamp](./unix-timestamp).
14 |
--------------------------------------------------------------------------------
/docs/display/unix-timestamp.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: unix-timestamp
3 | title: Unix Timestamp
4 | ---
5 |
6 | This returns the Unix timestamp (the number of seconds since the Unix Epoch) of the Day.js object.
7 |
8 | ```js
9 | dayjs('2019-01-25').unix() // 1548381600
10 | ```
11 |
12 | This value is floored to the nearest second, and does not include a milliseconds component.
13 |
--------------------------------------------------------------------------------
/docs/durations/add.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: add
3 | title: Add Time
4 | ---
5 |
6 | Returns a cloned duration object with a specified amount of time added.
7 |
8 | ```javascript
9 | var a = dayjs.duration(1, 'd');
10 | var b = dayjs.duration(2, 'd');
11 |
12 | a.add(b).days(); // 3
13 | a.add({ days: 2 } ).days();
14 | a.add(2, 'days');
15 | ```
16 |
17 | [List of all available units](./creating#list-of-all-available-units)
18 |
19 |
--------------------------------------------------------------------------------
/docs/durations/as-iso-string.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as-iso-string
3 | title: As ISO 8601 String
4 | ---
5 |
6 | Returns duration in string as specified by [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) standard.
7 |
8 | ```javascript
9 | dayjs.duration(1, 'd').toISOString() // "P1D"
10 | ```
11 | Format `PnYnMnDTnHnMnS` description:
12 |
13 | | Unit | Meaning |
14 | | ------------- | --------- |
15 | | P | _P_ stands for period. Placed at the start of the duration representation. |
16 | |Y | Year |
17 | |M | Month |
18 | |D | Day |
19 | |T | Designator that precedes the time components. |
20 | |H | Hour |
21 | |M | Minute |
22 | |S | Second |
23 |
--------------------------------------------------------------------------------
/docs/durations/as-json.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as-json
3 | title: As JSON
4 | ---
5 |
6 | When serializing a duration object to JSON, it will be represented as an ISO8601 string.
7 |
8 | ```javascript
9 | JSON.stringify({
10 | postDuration : dayjs.duration(5, 'm')
11 | }); // '{"postDuration":"PT5M"}'
12 | ```
13 |
--------------------------------------------------------------------------------
/docs/durations/as.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: as
3 | title: As Unit of Time
4 | ---
5 |
6 | As an alternate to `Duration#asX`, you can use `Duration#as('x')`.
7 |
8 | ```javascript
9 | var duration = dayjs.duration()
10 |
11 | duration.as('hours');
12 | duration.as('minutes');
13 | duration.as('seconds');
14 | duration.as('milliseconds');
15 | ```
16 |
17 | [List of all available units](./creating#list-of-all-available-units)
18 |
--------------------------------------------------------------------------------
/docs/durations/clone.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: clone
3 | title: Clone
4 | ---
5 |
6 | ```javascript
7 | dayjs.duration().clone();
8 | ```
9 | Create a clone of a duration. Durations are immutable, just like Day.js object objects. Still, this lets you get a snapshot, at some point in time.
10 |
11 |
--------------------------------------------------------------------------------
/docs/durations/creating.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: creating
3 | title: Creating
4 | ---
5 |
6 | To create a duration, call `dayjs.duration()` with the length of time in milliseconds.
7 |
8 | @>Duration
9 |
10 | ```javascript
11 | dayjs.extend(duration)
12 |
13 | dayjs.duration(100); // 100 milliseconds
14 | ```
15 |
16 | If you want to create a duration with a unit of measurement other than milliseconds, you can pass the unit of measurement as well.
17 |
18 | ```javascript
19 | dayjs.duration(2, 'days');
20 | ```
21 | #### List of all available units
22 |
23 | | Unit | Shorthand |
24 | | ------------- | --------- |
25 | | `days` | `d` |
26 | | `weeks` | `w` |
27 | | `months` | `M` |
28 | | `years` | `y` |
29 | | `hours` | `h` |
30 | | `minutes` | `m` |
31 | | `seconds` | `s` |
32 | | `milliseconds` | `ms` |
33 |
34 | You can also pass an object of values if you need multiple different units of measurement.
35 |
36 | ```javascript
37 | dayjs.duration({
38 | seconds: 2,
39 | minutes: 2,
40 | hours: 2,
41 | days: 2,
42 | weeks: 2,
43 | months: 2,
44 | years: 2
45 | });
46 | ```
47 |
48 | Day.js also supports parsing ISO 8601 durations.
49 |
50 | ```js
51 | dayjs.duration('P1Y2M3DT4H5M6S');
52 | dayjs.duration('P1M');
53 | ```
54 |
--------------------------------------------------------------------------------
/docs/durations/days.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: days
3 | title: Days
4 | ---
5 |
6 | ```javascript
7 | dayjs.duration().days();
8 | dayjs.duration().asDays();
9 | ```
10 |
11 | As with the other getters for durations, `dayjs.duration().days()` gets the days (0 - 30).
12 |
13 | `dayjs.duration().asDays()` gets the length of the duration in days.
14 |
--------------------------------------------------------------------------------
/docs/durations/diffing.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: diffing
3 | title: Using Duration with Diff
4 | ---
5 |
6 | You can also use duration with `dayjs#diff` to get the duration between two date times. To do so, simply pass the `dayjs#diff` method into `dayjs#duration` as follows:
7 |
8 | ```javascript
9 | var x = dayjs()
10 | var y = dayjs()
11 |
12 | var duration = dayjs.duration(x.diff(y))
13 | // returns duration object with the duration between x and y
14 | ```
15 | See [here](../display/difference) for more information about `dayjs#diff`.
16 |
--------------------------------------------------------------------------------
/docs/durations/durations.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: durations
3 | title: Durations
4 | ---
5 |
6 | Day.js also has duration objects. Where a Day.js object is defined as single points in time, durations are defined as a length of time.
7 |
8 | Durations do not have a defined beginning and end date. They are contextless.
9 |
10 | A duration is conceptually more similar to '2 hours' than to 'between 2 and 4 pm today'. As such, they are not a good solution to converting between units that depend on context.
11 |
12 | For example, a year can be defined as 366 days, 365 days, 365.25 days, 12 months, or 52 weeks. Trying to convert years to days makes no sense without context. It is much better to use `dayjs#diff` for calculating days or years between two date times than to use Durations.
13 |
14 | @>Duration
15 |
16 | ```javascript
17 | dayjs.extend(duration)
18 |
19 | dayjs.duration({ months: 12 })
20 | ```
21 |
--------------------------------------------------------------------------------
/docs/durations/format.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: format
3 | title: Format
4 | ---
5 |
6 | Get the formatted duration according to the string of tokens passed in.
7 |
8 | To escape characters, wrap them in square brackets (e.g. `[MM]`).
9 |
10 | ```js
11 | dayjs.duration({
12 | seconds: 1,
13 | minutes: 2,
14 | hours: 3,
15 | days: 4,
16 | months: 6,
17 | years: 7
18 | }).format('YYYY-MM-DDTHH:mm:ss') // 0007-06-04T03:02:01
19 | ```
20 |
21 | #### List of all available formats
22 |
23 | | Format | Output | Description |
24 | | ------ | ---------------- | ------------------------------------- |
25 | | `Y` | 18 | The years |
26 | | `YY` | 18 | The years, 2-digits |
27 | | `YYYY` | 2018 | The years, 4-digits |
28 | | `M` | 1-12 | The months, beginning at 1 |
29 | | `MM` | 01-12 | The months, 2-digits |
30 | | `D` | 1-31 | The days |
31 | | `DD` | 01-31 | The days, 2-digits |
32 | | `H` | 0-23 | The hours |
33 | | `HH` | 00-23 | The hours, 2-digits |
34 | | `m` | 0-59 | The minutes |
35 | | `mm` | 00-59 | The minutes, 2-digits |
36 | | `s` | 0-59 | The seconds |
37 | | `ss` | 00-59 | The seconds, 2-digits |
38 | | `SSS` | 000-999 | The milliseconds, 3-digits |
39 |
--------------------------------------------------------------------------------
/docs/durations/get.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: get
3 | title: Get Unit of Time
4 | ---
5 |
6 | As an alternate to `Duration#x()` getters, you can use `Duration#get('x')`.
7 |
8 | ```javascript
9 | var duration = dayjs.duration
10 |
11 | duration.get('hours');
12 | duration.get('minutes');
13 | duration.get('seconds');
14 | duration.get('milliseconds');
15 | ```
16 |
17 | [List of all available units](./creating#list-of-all-available-units)
18 |
--------------------------------------------------------------------------------
/docs/durations/hours.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: hours
3 | title: Hours
4 | ---
5 |
6 | ```javascript
7 | dayjs.duration().hours();
8 | dayjs.duration().asHours();
9 | ```
10 |
11 | As with the other getters for durations, `dayjs.duration().hours()` gets the hours (0 - 23).
12 |
13 | `dayjs.duration().asHours()` gets the length of the duration in hours.
14 |
--------------------------------------------------------------------------------
/docs/durations/humanize.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: humanize
3 | title: Humanize
4 | ---
5 |
6 | Sometimes, you want all the goodness of `dayjs#from` but you don't want to have to create two Day.js objects, you just want to display a length of time.
7 |
8 | @>Duration
9 |
10 | @>RelativeTime
11 |
12 | ```javascript
13 | dayjs.extend(duration)
14 | dayjs.extend(relativeTime)
15 | dayjs.duration(1, "minutes").humanize(); // a minute
16 | dayjs.duration(2, "minutes").humanize(); // 2 minutes
17 | dayjs.duration(24, "hours").humanize(); // a day
18 | ```
19 | By default, the return string is suffixless. If you want a suffix, pass in true as seen below.
20 |
21 | ```javascript
22 | dayjs.duration(1, "minutes").humanize(true); // in a minute
23 | ```
24 |
25 | For suffixes before now, pass in a negative number.
26 |
27 | ```javascript
28 | dayjs.duration(-1, "minutes").humanize(true); // a minute ago
29 | ```
30 |
31 |
--------------------------------------------------------------------------------
/docs/durations/is-a-duration.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-a-duration
3 | title: Is a Duration
4 | ---
5 |
6 | To check if a variable is a Day.js duration object, use `dayjs.isDuration()`.
7 |
8 | ```javascript
9 | dayjs.isDuration() // false
10 | dayjs.isDuration(new Date()) // false
11 | dayjs.isDuration(dayjs()) // false
12 | dayjs.isDuration(dayjs.duration()) // true
13 | dayjs.isDuration(dayjs.duration(2, 'minutes')) // true
14 | ```
15 |
--------------------------------------------------------------------------------
/docs/durations/locale.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: locale
3 | title: Locale
4 | ---
5 |
6 | You can get or set the locale of a duration using `locale`. The locale will affect the duration's string methods, like `humanize()`. See the [i18n](../i18n/i18n) section for more information on internationalization generally.
7 |
8 | @>RelativeTime
9 |
10 | ```javascript
11 | require("dayjs/locale/es");
12 | // import es from 'dayjs/plugin/es' // ES 2015
13 |
14 | dayjs.duration(1, "minutes").locale("en").humanize(); // a minute
15 |
16 | dayjs.duration(1, "minutes").locale("es").humanize(); // un minuto
17 | // dayjs.duration(1, "minutes").locale(es).humanize(); // ES 2015
18 | ```
19 |
--------------------------------------------------------------------------------
/docs/durations/milliseconds.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: milliseconds
3 | title: Milliseconds
4 | ---
5 |
6 | To get the number of milliseconds in a duration, use `dayjs.duration().milliseconds()`.
7 |
8 | It will return a number between 0 and 999.
9 |
10 | ```javascript
11 | dayjs.duration(500).milliseconds(); // 500
12 | dayjs.duration(1500).milliseconds(); // 500
13 | dayjs.duration(15000).milliseconds(); // 0
14 | ```
15 |
16 | If you want the length of the duration in milliseconds, use `dayjs.duration().asMilliseconds()` instead.
17 |
18 | ```javascript
19 | dayjs.duration(500).asMilliseconds(); // 500
20 | dayjs.duration(1500).asMilliseconds(); // 1500
21 | dayjs.duration(15000).asMilliseconds(); // 15000
22 | ```
23 |
--------------------------------------------------------------------------------
/docs/durations/minutes.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: minutes
3 | title: Minutes
4 | ---
5 |
6 | ```javascript
7 | dayjs.duration().minutes();
8 | dayjs.duration().asMinutes();
9 | ```
10 |
11 | As with the other getters for durations, `dayjs.duration().minutes()` gets the minutes (0 - 59).
12 |
13 | `dayjs.duration().asMinutes()` gets the length of the duration in minutes.
14 |
--------------------------------------------------------------------------------
/docs/durations/months.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: months
3 | title: Months
4 | ---
5 |
6 | ```javascript
7 | dayjs.duration().months();
8 | dayjs.duration().asMonths();
9 | ```
10 |
11 | As with the other getters for durations, `dayjs.duration().months()` gets the months (0 - 11).
12 |
13 | `dayjs.duration().asMonths()` gets the length of the duration in months.
14 |
--------------------------------------------------------------------------------
/docs/durations/seconds.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: seconds
3 | title: Seconds
4 | ---
5 |
6 | To get the number of seconds in a duration, use `dayjs.duration().seconds()`.
7 |
8 | It will return a number between 0 and 59.
9 |
10 | ```javascript
11 | dayjs.duration(500).seconds(); // 0
12 | dayjs.duration(1500).seconds(); // 1
13 | dayjs.duration(15000).seconds(); // 15
14 | ```
15 |
16 | If you want the length of the duration in seconds, use `dayjs.duration().asSeconds()` instead.
17 |
18 | ```javascript
19 | dayjs.duration(500).asSeconds(); // 0.5
20 | dayjs.duration(1500).asSeconds(); // 1.5
21 | dayjs.duration(15000).asSeconds(); // 15
22 | ```
23 |
--------------------------------------------------------------------------------
/docs/durations/subtract.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: subtract
3 | title: Subtract Time
4 | ---
5 |
6 | Returns a cloned duration object with a specified amount of time subtracted.
7 |
8 | ```javascript
9 | var a = dayjs.duration(3, 'd');
10 | var b = dayjs.duration(2, 'd');
11 |
12 | a.subtract(b).days(); // 1
13 | a.subtract({ days: 2 } ).days();
14 | a.subtract(2, 'days');
15 | ```
16 |
17 | [List of all available units](./creating#list-of-all-available-units)
18 |
--------------------------------------------------------------------------------
/docs/durations/weeks.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: weeks
3 | title: Weeks
4 | ---
5 |
6 | ```javascript
7 | dayjs.duration().weeks();
8 | dayjs.duration().asWeeks();
9 | ```
10 |
11 | As with the other getters for durations, `dayjs.duration().weeks()` gets the weeks (0 - 4).
12 |
13 | `dayjs.duration().asWeeks()` gets the length of the duration in weeks.
14 |
15 | Pay attention that unlike the other getters for duration, weeks are counted as a subset of the days, and are not taken off the days count.
16 |
17 | Note: The length of a duration in weeks is defined as 7 days.
18 |
--------------------------------------------------------------------------------
/docs/durations/years.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: years
3 | title: Years
4 | ---
5 | ```javascript
6 | dayjs.duration().years();
7 | dayjs.duration().asYears();
8 | ```
9 |
10 | As with the other getters for durations, `dayjs.duration().years()` gets the years.
11 |
12 | `dayjs.duration().asYears()` gets the length of the duration in years.
13 |
--------------------------------------------------------------------------------
/docs/get-set/date.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: date
3 | title: Date of Month
4 | ---
5 |
6 | Gets or sets the day of the month.
7 |
8 | Accepts numbers from 1 to 31. If the range is exceeded, it will bubble up to the months.
9 |
10 | ```js
11 | dayjs().date() // gets day of current month
12 | dayjs().date(1) // returns new dayjs object
13 | ```
14 |
15 | >`dayjs#date` is for the date of the month, and `dayjs#day` is for the day of the week.
16 |
--------------------------------------------------------------------------------
/docs/get-set/day-of-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: day-of-year
3 | title: Day of Year
4 | ---
5 | Gets or sets the day of the year.
6 |
7 | Accepts numbers from 1 to 366.
8 |
9 | If the range is exceeded, it will bubble up to the years.
10 |
11 | @>DayOfYear
12 |
13 | ```js
14 | dayjs.extend(dayOfYear)
15 |
16 | dayjs('2010-01-01').dayOfYear() // 1
17 | dayjs('2010-01-01').dayOfYear(365) // 2010-12-31
18 | ```
19 |
--------------------------------------------------------------------------------
/docs/get-set/day.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: day
3 | title: Day of Week
4 | ---
5 |
6 | Gets or sets the day of the week.
7 |
8 | Accepts numbers from 0 (Sunday) to 6 (Saturday). If the range is exceeded, it will bubble up to other weeks.
9 |
10 | ```js
11 | dayjs().day() // gets day of current week
12 | dayjs().day(0) // returns new dayjs object
13 | ```
14 |
15 | >`dayjs#date` is for the date of the month, and `dayjs#day` is for the day of the week.
16 |
--------------------------------------------------------------------------------
/docs/get-set/get-set.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: get-set
3 | title: Get + Set
4 | ---
5 |
6 | Day.js uses overloaded getters and setters, that is to say, calling these methods without parameters acts as a getter, and calling them with a parameter acts as a setter.
7 |
8 | As dayjs objects are immutable, all setters will return a new dayjs instance.
9 |
10 | These map to the corresponding function on the native `Date` object.
11 |
12 | ```js
13 | dayjs().second(30).valueOf() // => new Date().setSeconds(30)
14 | dayjs().second() // => new Date().getSeconds()
15 | ```
16 |
17 | If you are in [UTC mode](../parse/utc), they will map to the UTC equivalent.
18 |
19 | ```js
20 | dayjs.utc().second(30).valueOf()// => new Date().setUTCSeconds(30)
21 | dayjs.utc().second()// => new Date().getUTCSeconds()
22 | ```
23 |
--------------------------------------------------------------------------------
/docs/get-set/get.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: get
3 | title: Get
4 | ---
5 | String getter, returns the corresponding information getting from Day.js object.
6 |
7 | In general:
8 | ```js
9 | dayjs().get(unit) === dayjs()[unit]()
10 | ```
11 |
12 | Units are case insensitive, and support plural and short forms. Note, short forms are case sensitive.
13 |
14 | ```js
15 | dayjs().get('year')
16 | dayjs().get('month') // start 0
17 | dayjs().get('date')
18 | dayjs().get('hour')
19 | dayjs().get('minute')
20 | dayjs().get('second')
21 | dayjs().get('millisecond')
22 | ```
23 |
24 | #### List of all available units
25 |
26 | | Unit | Shorthand | Description |
27 | | ------------- | --------- | ---------------------------------------- |
28 | | `date` | `D` | Date of Month |
29 | | `day` | `d` | Day of Week (Sunday as 0, Saturday as 6) |
30 | | `month` | `M` | Month (January as 0, December as 11) |
31 | | `year` | `y` | Year |
32 | | `hour` | `h` | Hour |
33 | | `minute` | `m` | Minute |
34 | | `second` | `s` | Second |
35 | | `millisecond` | `ms` | Millisecond |
36 |
--------------------------------------------------------------------------------
/docs/get-set/hour.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: hour
3 | title: Hour
4 | ---
5 |
6 | Gets or sets the hour.
7 |
8 | Accepts numbers from 0 to 23. If the range is exceeded, it will bubble up to the day.
9 |
10 | ```js
11 | dayjs().hour() // gets current hour
12 | newDate = dayjs().hour(12) // returns new dayjs object
13 | ```
14 |
--------------------------------------------------------------------------------
/docs/get-set/iso-week-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: iso-week-year
3 | title: Week Year (ISO)
4 | ---
5 |
6 | Gets the [ISO week-year](https://en.wikipedia.org/wiki/ISO_week_date).
7 |
8 | @>IsoWeek
9 |
10 | ```javascript
11 | dayjs.extend(isoWeek)
12 |
13 | dayjs().isoWeekYear()
14 | ```
15 |
--------------------------------------------------------------------------------
/docs/get-set/iso-week.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: iso-week
3 | title: Week of Year (ISO)
4 | ---
5 |
6 | Gets or sets the [ISO week of the year](https://en.wikipedia.org/wiki/ISO_week_date).
7 |
8 | @>IsoWeek
9 |
10 | ```javascript
11 | dayjs.extend(isoWeek)
12 |
13 | dayjs().isoWeek() // gets the current ISO week of the year
14 | newDate = dayjs().isoWeek(2) // returns new dayjs object
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/get-set/iso-weekday.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: iso-weekday
3 | title: ISO Day of Week
4 | ---
5 |
6 | Gets or sets the [ISO day of the week](https://en.wikipedia.org/wiki/ISO_week_date) with 1 being Monday and 7 being Sunday.
7 |
8 | @>IsoWeek
9 |
10 | ```javascript
11 | dayjs.extend(isoWeek)
12 |
13 | dayjs().isoWeekday() // gets the current ISO day of the week
14 | dayjs().isoWeekday(1); // Monday
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/get-set/iso-weeks-in-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: iso-weeks-in-year
3 | title: Weeks In Year (ISO)
4 | ---
5 | Gets the number of weeks in the current year, according to [ISO weeks](https://en.wikipedia.org/wiki/ISO_week_date).
6 |
7 | @>IsoWeeksInYear
8 |
9 | ```js
10 | dayjs.extend(isoWeeksInYear)
11 | dayjs.extend(isLeapYear)
12 |
13 | dayjs('2004-01-01').isoWeeksInYear() // 53
14 | dayjs('2005-01-01').isoWeeksInYear() // 52
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/get-set/max.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: max
3 | title: Maximum
4 | ---
5 | Returns the maximum (most distant future) of the given Day.js instances. This accepts both multiple arguments and array that contains Day.js instance.
6 |
7 | @>MinMax
8 |
9 | ```js
10 | dayjs.extend(minMax)
11 |
12 | dayjs.max(dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01'))
13 | dayjs.max([dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01')])
14 | ```
15 |
--------------------------------------------------------------------------------
/docs/get-set/millisecond.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: millisecond
3 | title: Millisecond
4 | ---
5 |
6 | Gets or sets the millisecond.
7 |
8 | Accepts numbers from 0 to 999. If the range is exceeded, it will bubble up to the seconds.
9 |
10 | ```js
11 | dayjs().millisecond() // gets current millisecond
12 | dayjs().millisecond(1) // returns new dayjs object
13 | ```
14 |
--------------------------------------------------------------------------------
/docs/get-set/min.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: min
3 | title: Minimum
4 | ---
5 | Returns the minimum (most distant past) of the given Day.js instances.
6 | This accepts both multiple arguments and array that contains Day.js instance.
7 |
8 | @>MinMax
9 |
10 | ```js
11 | dayjs.extend(minMax)
12 |
13 | dayjs.min(dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01'))
14 | dayjs.min([dayjs(), dayjs('2018-01-01'), dayjs('2019-01-01')])
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/get-set/minute.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: minute
3 | title: Minute
4 | ---
5 |
6 | Gets or sets the minutes.
7 |
8 | Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the hour.
9 |
10 | ```js
11 | dayjs().minute() // gets current minute
12 | dayjs().minute(59) // returns new dayjs object
13 | ```
14 |
--------------------------------------------------------------------------------
/docs/get-set/month.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: month
3 | title: Month
4 | ---
5 |
6 | Gets or sets the month.
7 |
8 | Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the year.
9 |
10 | ```js
11 | dayjs().month() // gets current month
12 | dayjs().month(0) // returns new dayjs object
13 | ```
14 |
15 | >Months are zero indexed, so January is month 0.
16 |
--------------------------------------------------------------------------------
/docs/get-set/quarter.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: quarter
3 | title: Quarter
4 | ---
5 | Gets or sets the quarter.
6 |
7 | @>QuarterOfYear
8 |
9 | ```js
10 | dayjs.extend(quarterOfYear)
11 |
12 | dayjs('2010-04-01').quarter() // 2
13 | dayjs('2010-04-01').quarter(2) // returns new dayjs object
14 | ```
15 |
--------------------------------------------------------------------------------
/docs/get-set/second.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: second
3 | title: Second
4 | ---
5 |
6 | Gets or sets the second.
7 |
8 | Accepts numbers from 0 to 59. If the range is exceeded, it will bubble up to the minutes.
9 |
10 | ```js
11 | dayjs().second() // gets current second
12 | dayjs().second(1) // returns new dayjs object
13 | ```
14 |
--------------------------------------------------------------------------------
/docs/get-set/set.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: set
3 | title: Set
4 | ---
5 |
6 | Generic setter, accepting unit as first argument, and value as second, returns a new instance with the applied changes.
7 |
8 | In general:
9 | ```js
10 | dayjs().set(unit, value) === dayjs()[unit](value)
11 | ```
12 |
13 | ```js
14 | dayjs().set('date', 1)
15 | dayjs().set('month', 3) // April
16 | dayjs().set('second', 30)
17 | ```
18 | For multiple set:
19 | ```
20 | dayjs().set('hour', 5).set('minute', 55).set('second', 15)
21 | ```
22 | Units are case insensitive, and support plural and short forms.
23 |
24 | [List of all available units](./get#list-of-all-available-units)
25 |
--------------------------------------------------------------------------------
/docs/get-set/week-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: week-year
3 | title: Week Year
4 | ---
5 | Gets the week-year according to the locale.
6 |
7 | @>WeekYear
8 |
9 | ```javascript
10 | dayjs.extend(weekYear)
11 | dayjs.extend(weekOfYear)
12 |
13 | dayjs().weekYear()
14 | ```
15 |
--------------------------------------------------------------------------------
/docs/get-set/week.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: week
3 | title: Week of Year
4 | ---
5 | Gets or sets the week of the year.
6 |
7 | @>WeekOfYear
8 |
9 | ```js
10 | dayjs.extend(weekOfYear)
11 |
12 | dayjs('2018-06-27').week() // 26
13 | dayjs('2018-06-27').week(5) // returns new dayjs object
14 | ```
15 |
--------------------------------------------------------------------------------
/docs/get-set/weekday.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: weekday
3 | title: Day of Week (Locale Aware)
4 | ---
5 | Gets or sets the day of the week according to the locale.
6 |
7 | @>Weekday
8 |
9 | If the locale assigns Sunday as the first day of the week, `dayjs().weekday(0)` will be Sunday. If Monday is the first day of the week, `dayjs().weekday(0)` will be Monday.
10 |
11 | ```js
12 | dayjs.extend(weekday)
13 |
14 | // when Sunday is the first day of the week
15 | dayjs().weekday(-7); // last Sunday
16 | dayjs().weekday(7); // next Sunday
17 |
18 | // when Monday is the first day of the week
19 | dayjs().weekday(-7) // last Monday
20 | dayjs().weekday(7) // next Monday
21 |
22 |
23 | // when Sunday is the first day of the week
24 | dayjs().weekday(-5) // last Tuesday (5th day before Sunday)
25 | dayjs().weekday(5) // next Friday (5th day after Sunday)
26 | ```
27 |
--------------------------------------------------------------------------------
/docs/get-set/year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: year
3 | title: Year
4 | ---
5 |
6 | Gets or sets the year.
7 |
8 | ```js
9 | dayjs().year() // gets current year
10 | dayjs().year(2000) // returns new dayjs object
11 | ```
12 |
--------------------------------------------------------------------------------
/docs/i18n/changing-locale.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: changing-locale
3 | title: Changing locale globally
4 | ---
5 | By default, Day.js comes with English locale **only**.
6 |
7 | If you need other locales, you can load them on demand.
8 |
9 | ```js
10 | require('dayjs/locale/de')
11 | ```
12 |
13 | Once you load a locale, it becomes the active locale. To change active locales, simply call `dayjs.locale` with the key of a loaded locale to change global locale.
14 |
15 | Changing the global locale doesn't affect existing instances.
16 |
17 | ```js
18 | dayjs.locale('de') // use loaded locale globally
19 | dayjs.locale('en') // switch back to default English locale globally
20 | ```
21 |
--------------------------------------------------------------------------------
/docs/i18n/getting-locale.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: getting-locale
3 | title: Checking the current Day.js locale
4 | ---
5 |
6 | This returns the locale of current instance.
7 |
8 | ```js
9 | dayjs.locale() // 'en'
10 | ```
11 |
--------------------------------------------------------------------------------
/docs/i18n/i18n.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: i18n
3 | title: i18n
4 | ---
5 |
6 | Day.js has great support for internationalization.
7 |
8 | But none of them will be included in your build unless you use that.
9 |
10 | You can load multiple locales and switch between them easily.
11 |
12 | [List of supported locales](https://github.com/iamkun/dayjs/tree/dev/src/locale)
13 |
14 | There's also a [locale.json](https://cdn.jsdelivr.net/npm/dayjs@1/locale.json) file contains the list of all supported locales in the root of each release.
15 |
16 | More details on each of the parts of the locale bundle and the way to update or customize locale can be found in the [customization](../../customization/customization) section.
17 |
18 | You are **super welcome** to add a locale by opening a pull request.
19 |
20 |
--------------------------------------------------------------------------------
/docs/i18n/instance-locale.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: instance-locale
3 | title: Changing locales locally
4 | ---
5 |
6 | A global locale configuration can be problematic when passing around date-times that may need to be formatted into different locale.
7 |
8 | Exactly the same as `dayjs#locale`, but only use locale in a specific instance and returns a new instance by switching to new locale.
9 |
10 | ```js
11 | require('dayjs/locale/de')
12 | dayjs().locale('de').format() // use loaded locale locally
13 | ```
14 |
--------------------------------------------------------------------------------
/docs/i18n/listing-months-weekdays.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: listing-months-weekdays
3 | title: Listing the months and weekdays of the current locale
4 | ---
5 |
6 | To get the list of months or weekdays in a locale.
7 |
8 | @>LocaleData
9 |
10 | ```js
11 | dayjs.extend(localeData)
12 |
13 | dayjs.weekdays()
14 | dayjs.weekdaysShort()
15 | dayjs.weekdaysMin()
16 | dayjs.monthsShort()
17 | dayjs.months() // e.g. return [ 'January','February','March','April','May',
18 | // 'June','July','August','September','October','November','December' ]
19 | ```
20 |
--------------------------------------------------------------------------------
/docs/i18n/loading-into-browser.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: loading-into-browser
3 | title: Loading locale in the browser
4 | ---
5 | Loading locale on demand.
6 |
7 | ```html
8 |
9 |
13 | ```
14 |
15 | Get the locale object for further use.
16 |
17 | ```html
18 |
19 |
20 |
23 | ```
24 |
25 | Day.js is available on [CDN](../installation/browser#cdn-resource).
26 |
27 | ```html
28 |
29 |
30 |
31 |
32 | ```
33 |
--------------------------------------------------------------------------------
/docs/i18n/loading-into-nodejs.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: loading-into-nodejs
3 | title: Loading locale in NodeJS
4 | ---
5 | Loading locale on demand.
6 |
7 | ```javascript
8 | require('dayjs/locale/de')
9 | // import 'dayjs/locale/de' // ES 2015
10 |
11 | dayjs.locale('de') // use locale globally
12 | dayjs().locale('de').format() // use locale in a specific instance
13 | ```
14 |
15 | You can also load and get the locale object for further use.
16 | ```javascript
17 | var locale_de = require('dayjs/locale/de')
18 | // import locale_de from 'dayjs/locale/de' // ES 2015
19 | ```
20 |
--------------------------------------------------------------------------------
/docs/i18n/locale-data.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: locale-data
3 | title: Accessing locale specific functionality
4 | ---
5 |
6 | You can access the properties of the currently loaded locale through the `dayjs.localeData()` function, or `dayjs().localeData()` for current Day.js object.
7 |
8 | @>LocaleData
9 |
10 | ```js
11 | dayjs.extend(localeData)
12 |
13 | globalLocaleData = dayjs.localeData()
14 | globalLocaleData.firstDayOfWeek()
15 | globalLocaleData.months()
16 | globalLocaleData.monthsShort()
17 | globalLocaleData.weekdays()
18 | globalLocaleData.weekdaysShort()
19 | globalLocaleData.weekdaysMin()
20 |
21 | globalLocaleData.months(dayjs())
22 | globalLocaleData.monthsShort(dayjs())
23 | globalLocaleData.weekdays(dayjs())
24 | globalLocaleData.weekdaysShort(dayjs())
25 | globalLocaleData.weekdaysMin(dayjs())
26 |
27 | instanceLocaleData = dayjs().localeData()
28 | instanceLocaleData.firstDayOfWeek()
29 | instanceLocaleData.months()
30 | instanceLocaleData.monthsShort()
31 | instanceLocaleData.weekdays()
32 | instanceLocaleData.weekdaysShort()
33 | instanceLocaleData.weekdaysMin()
34 | ```
35 |
--------------------------------------------------------------------------------
/docs/installation/browser.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: browser
3 | title: Browser
4 | ---
5 |
6 | ```html
7 |
8 |
11 | ```
12 |
13 | #### CDN resource
14 |
15 | >Day.js can be included by way of a CDN provider like [cdnjs.com](https://cdnjs.com/libraries/dayjs), [unpkg](https://unpkg.com/dayjs/) and [jsDelivr](https://www.jsdelivr.com/package/npm/dayjs) ...
16 |
17 | ```html
18 |
19 |
20 |
21 | ```
22 |
23 | Check here for more information about loading [locale](../i18n/loading-into-browser) and [plugin](../plugin/loading-into-browser).
24 |
--------------------------------------------------------------------------------
/docs/installation/download.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: download
3 | title: Download
4 | ---
5 |
6 | Download the latest version of Day.js at [https://www.jsdelivr.com/package/npm/dayjs](https://www.jsdelivr.com/package/npm/dayjs)
7 |
8 | Check the releases and source code of Day.js at [https://github.com/iamkun/dayjs/releases](https://github.com/iamkun/dayjs/releases)
9 |
--------------------------------------------------------------------------------
/docs/installation/installation.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: installation
3 | title: Installation
4 | ---
5 |
6 | Day.js was designed to work both in the browser and in Node.js.
7 |
8 | All code should work in both of these environments, and all unit tests are run in both of these environments.
9 |
10 | Currently the following browsers are used for the ci system: Chrome on Windows XP, IE 8, 9, and 10 on Windows 7, IE 11 on Windows 10, latest Firefox on Linux, and latest Safari on OSX 10.8 and 10.11.
11 |
12 | If you want to try the sample codes, just open your browser's console and enter them.
13 |
--------------------------------------------------------------------------------
/docs/installation/node-js.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: node-js
3 | title: Node.js
4 | ---
5 |
6 | To get started with Day.js in your Node.js project, simply add the dependency to your Node.js package manager.
7 |
8 | ```bash
9 | npm install dayjs
10 | # or
11 | yarn add dayjs
12 | # or
13 | pnpm add dayjs
14 | ```
15 |
16 | Then include it in your script:
17 |
18 | ```js
19 | const dayjs = require('dayjs')
20 | //import dayjs from 'dayjs' // ES 2015
21 | dayjs().format()
22 | ```
23 |
24 | Check here for more information about loading [locale](../i18n/loading-into-nodejs) and [plugin](../plugin/loading-into-nodejs).
25 |
--------------------------------------------------------------------------------
/docs/installation/typescript.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: typescript
3 | title: TypeScript
4 | ---
5 | Day.js ships with official type declarations for TypeScript in NPM package out of the box.
6 |
7 | Install via NPM
8 | ```console
9 | npm install dayjs
10 | ```
11 | Import and use in your TypeScript file
12 | ```js
13 | import * as dayjs from 'dayjs'
14 | dayjs().format()
15 | ```
16 |
17 | #### Have trouble importing Day.js?
18 |
19 | If your `tsconfig.json` contains the following config, you must do the default import workflow `import dayjs from 'dayjs'`:
20 |
21 | ```json
22 | { //tsconfig.json
23 | "compilerOptions": {
24 | "esModuleInterop": true,
25 | "allowSyntheticDefaultImports": true,
26 | }
27 | }
28 | ```
29 | If you don't have these config above, the default import won't work, and you'll continue to have to use `import * as dayjs from 'dayjs'`
30 |
31 | ### Locale and plugin import
32 |
33 | To use locale and plugin, you first need to import the targeting language and plugin.
34 |
35 | ```js
36 | import * as dayjs from 'dayjs'
37 | import * as isLeapYear from 'dayjs/plugin/isLeapYear' // import plugin
38 | import 'dayjs/locale/zh-cn' // import locale
39 |
40 | dayjs.extend(isLeapYear) // use plugin
41 | dayjs.locale('zh-cn') // use locale
42 | ```
43 |
--------------------------------------------------------------------------------
/docs/manipulate/add.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: add
3 | title: Add
4 | ---
5 |
6 | Returns a cloned Day.js object with a specified amount of time added.
7 |
8 | ```js
9 | const a = dayjs()
10 | const b = a.add(7, 'day')
11 |
12 | // a -> the original value and will not change
13 | // b -> the manipulation result
14 | ```
15 |
16 | Units are case insensitive, and support plural and short forms. Note, short forms are case sensitive.
17 |
18 | #### List of all available units
19 |
20 | | Unit | Shorthand | Description |
21 | | ------------- | --------- | ---------------------------------------- |
22 | | `day` | `d` | Day |
23 | | `week` | `w` | Week |
24 | | `month` | `M` | Month |
25 | | `quarter` | `Q` | Quarter @>>QuarterOfYear |
26 | | `year` | `y` | Year |
27 | | `hour` | `h` | Hour |
28 | | `minute` | `m` | Minute |
29 | | `second` | `s` | Second |
30 | | `millisecond` | `ms` | Millisecond |
31 |
32 | Alternatively, you can use [durations](../durations/durations) to add to Day.js object.
33 |
34 | ```js
35 | result = dayjs().add(dayjs.duration({'days' : 1}))
36 | ```
37 |
38 | When decimal values are passed for **days** and **weeks**, they are rounded to the nearest integer before adding.
39 |
--------------------------------------------------------------------------------
/docs/manipulate/end-of.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: end-of
3 | title: End of Time
4 | ---
5 | Returns a cloned Day.js object and set it to the end of a unit of time.
6 |
7 | ```js
8 | dayjs().endOf('month')
9 | ```
10 |
11 | Units are case insensitive, and support plural and short forms.
12 |
13 | [List of all available units](../manipulate/start-of#list-of-all-available-units)
14 |
--------------------------------------------------------------------------------
/docs/manipulate/local.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: local
3 | title: Local
4 | ---
5 |
6 | This returns a Day.js object with a flag to use local time.
7 |
8 | @>UTC
9 |
10 | ```js
11 | dayjs.extend(utc)
12 |
13 | var a = dayjs.utc()
14 | a.format() // 2019-03-06T00:00:00Z
15 | a.local().format() //2019-03-06T08:00:00+08:00
16 | ```
17 |
18 | Check more information about [UTC mode](../parse/utc).
19 |
20 |
--------------------------------------------------------------------------------
/docs/manipulate/manipulate.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: manipulate
3 | title: Manipulate
4 | ---
5 |
6 | Once you have a Day.js object, you may want to manipulate it in some way.
7 |
8 | Day.js supports method chaining like this:
9 |
10 | ```js
11 | dayjs('2019-01-25').add(1, 'day').subtract(1, 'year').year(2009).toString()
12 | ```
13 |
--------------------------------------------------------------------------------
/docs/manipulate/start-of.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: start-of
3 | title: Start of Time
4 | ---
5 |
6 | Returns a cloned Day.js object and set it to the start of a unit of time.
7 |
8 | ```js
9 | dayjs().startOf('year')
10 | ```
11 |
12 | Units are case insensitive, and support plural and short forms.
13 |
14 | #### List of all available units
15 |
16 | | Unit | Shorthand | Description |
17 | | ------------- | --------- | ----------------------------------------- |
18 | | `year` | `y` | January 1st, 00:00 this year |
19 | | `quarter` | `Q` | beginning of the current quarter, 1st day of months, 00:00 @>>QuarterOfYear|
20 | | `month` | `M` | the first day of this month, 00:00 |
21 | | `week` | `w` | the first day of this week, 00:00 (locale aware) |
22 | | `isoWeek` || the first day of this week according to ISO 8601, 00:00 @>>IsoWeek|
23 | | `date` | `D` | 00:00 today |
24 | | `day` | `d` | 00:00 today |
25 | | `hour` | `h` | now, but with 0 mins, 0 secs, and 0 ms |
26 | | `minute` | `m` | now, but with 0 seconds and 0 milliseconds|
27 | | `second` | `s` | now, but with 0 milliseconds |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/docs/manipulate/subtract.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: subtract
3 | title: Subtract
4 | ---
5 | Returns a cloned Day.js object with a specified amount of time subtracted.
6 |
7 | ```js
8 | dayjs().subtract(7, 'year')
9 | ```
10 |
11 | Units are case insensitive, and support plural and short forms.
12 |
13 | [List of all available units](../manipulate/add#list-of-all-available-units)
14 |
15 | When decimal values are passed for **days** and **weeks**, they are rounded to the nearest integer before subtracting.
16 |
--------------------------------------------------------------------------------
/docs/manipulate/utc-offset.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: utc-offset
3 | title: UTC offset
4 | ---
5 | Get the UTC offset in minutes.
6 |
7 | ```js
8 | dayjs().utcOffset()
9 | ```
10 |
11 | Setting the UTC offset by supplying minutes and returns a new instance.
12 | Note that once you set an offset, it's fixed and won't change on its own (i.e there are no DST rules).
13 |
14 | @>UTC
15 |
16 | ```js
17 | dayjs.extend(utc)
18 |
19 | dayjs().utcOffset(120)
20 | ```
21 |
22 | If the input is less than 16 and greater than -16, it will interpret your input as hours instead.
23 | ```
24 | // these are equivalent
25 | dayjs().utcOffset(8) // set hours offset
26 | dayjs().utcOffset(480) // set minutes offset (8 * 60)
27 | ```
28 |
29 | Passing true to the second parameter will keep the same local time.
30 | ```
31 | dayjs.utc('2000-01-01T06:01:02Z').utcOffset(1, true).format()
32 | // 2000-01-01T06:01:02+01:00
33 | ```
34 |
--------------------------------------------------------------------------------
/docs/manipulate/utc.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: utc
3 | title: UTC
4 | ---
5 |
6 | This returns a Day.js object with a flag to use UTC time.
7 |
8 | @>UTC
9 |
10 | ```js
11 | dayjs.extend(utc)
12 |
13 | var a = dayjs()
14 | a.format() //2019-03-06T08:00:00+08:00
15 | a.utc().format() // 2019-03-06T00:00:00Z
16 | ```
17 |
18 | Passing true will change the time zone without changing the current time.
19 |
20 | ```
21 | dayjs('2016-05-03 22:15:01').utc(true).format()
22 | // 2016-05-03T22:15:01Z
23 | ```
24 |
25 | Check more information about [UTC mode](../parse/utc).
26 |
27 |
--------------------------------------------------------------------------------
/docs/parse/array.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: array
3 | title: Array
4 | ---
5 | You can create a Dayjs object with an array of numbers that mirror the parameters passed to `new Date()`
6 |
7 | @>ArraySupport
8 |
9 | ```js
10 | dayjs.extend(arraySupport)
11 | dayjs([2010, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM
12 | dayjs.utc([2010, 1, 14, 15, 25, 50, 125]);
13 | dayjs([2010]); // January 1st
14 | dayjs([2010, 6]); // July 1st
15 | dayjs([2010, 6, 10]); // July 10th
16 | ```
17 |
18 | `dayjs([])` returns the current time.
19 |
20 | Note that like `new Date(year, month, date)`, months are 0 indexed.
21 |
22 |
--------------------------------------------------------------------------------
/docs/parse/date.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: date
3 | title: Date
4 | ---
5 |
6 | Create a Day.js object with a pre-existing native Javascript `Date` object.
7 |
8 | ```js
9 | var d = new Date(2018, 8, 18)
10 | var day = dayjs(d)
11 | ```
12 |
13 | This clones the `Date` object. Further changes to the `Date` won't affect the Day.js object, and vice-versa.
14 |
--------------------------------------------------------------------------------
/docs/parse/dayjs-clone.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: dayjs-clone
3 | title: Dayjs Clone
4 | ---
5 |
6 | All Day.js objects are **immutable**. Still, `dayjs#clone` can create a clone of the current object if you need one.
7 |
8 | ```js
9 | var a = dayjs()
10 | var b = a.clone()
11 | // a and b are two separate Day.js object
12 | ```
13 |
14 | Calling `dayjs()` on a Day.js object will clone it as well.
15 | ```js
16 | var a = dayjs()
17 | var b = dayjs(a)
18 | ```
19 |
--------------------------------------------------------------------------------
/docs/parse/is-valid.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-valid
3 | title: Validation
4 | ---
5 |
6 | Returns a `boolean` indicating whether the `Dayjs`'s date is valid.
7 | - Non-strict check.
8 |
9 | Only checks if the value could be parsed to a Date time.
10 |
11 | ```js
12 | dayjs('2022-01-33').isValid();
13 | // true, parsed to 2022-02-02
14 | dayjs('some invalid string').isValid();
15 | // false
16 | ```
17 | - Strict check.
18 |
19 | Checks if the value could be parsed to a Date time and it is a valid date. The last two arguments `format` and `strict` must be provided.
20 | @>CustomParseFormat
21 | ```js
22 | dayjs('2022-02-31', 'YYYY-MM-DD', true).isValid();
23 | // false
24 | ```
25 |
--------------------------------------------------------------------------------
/docs/parse/now.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: now
3 | title: Now
4 | ---
5 |
6 | Calling `dayjs()` without parameters returns a fresh Day.js object with the current date and time.
7 |
8 | ```js
9 | var now = dayjs()
10 | ```
11 |
12 | This is essentially the same as calling `dayjs(new Date())`.
13 |
14 | Day.js treats `dayjs(undefined)` as `dayjs()` due to that function parameters default to undefined when not passed in.
15 |
16 | Day.js treats `dayjs(null)` as an invalid input.
17 |
18 |
19 |
--------------------------------------------------------------------------------
/docs/parse/object.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: object
3 | title: Object
4 | ---
5 | You can create a Dayjs object by specifying some of the units in an object.
6 |
7 | @>ObjectSupport
8 |
9 | ```js
10 | dayjs.extend(objectSupport)
11 | dayjs({ hour:15, minute:10 });
12 | dayjs.utc({ y:2010, M:3, d:5, h:15, m:10, s:3, ms: 123});
13 | dayjs({ year :2010, month :3, day :5, hour :15, minute :10, second :3, millisecond :123});
14 | dayjs({ years:2010, months:3, date:5, hours:15, minutes:10, seconds:3, milliseconds:123});
15 | ```
16 |
17 | `day` and `date` key both mean day of the month.
18 |
19 | `dayjs({})` returns the current time.
20 |
21 | Note that like `new Date(year, month, date)`, months are 0 indexed.
22 |
23 |
--------------------------------------------------------------------------------
/docs/parse/parse.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: parse
3 | title: Parse
4 | ---
5 |
6 | Instead of modifying the native `Date.prototype`, Day.js creates a wrapper for the `Date` object. To get this wrapper object, simply call `dayjs()` with one of the supported input types.
7 |
8 | The Day.js object is immutable, that is, all API operations that change the Day.js object in some way will return a new instance of it.
9 |
--------------------------------------------------------------------------------
/docs/parse/string-format.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: string-format
3 | title: String + Format
4 | ---
5 | If you know the format of an input string, you can use that to parse a date.
6 |
7 | @>CustomParseFormat
8 |
9 | ```js
10 | dayjs.extend(customParseFormat)
11 | dayjs("12-25-1995", "MM-DD-YYYY")
12 | ```
13 |
14 | Pass the locale key as the third parameter to parse locale-aware date time string.
15 | ```js
16 | require('dayjs/locale/es')
17 | dayjs('2018 Enero 15', 'YYYY MMMM DD', 'es')
18 | ```
19 |
20 | You may specify a boolean for the last argument to use strict parsing. Strict parsing requires that the format and input match exactly, including delimiters.
21 |
22 | ```js
23 | dayjs('1970-00-00', 'YYYY-MM-DD').isValid() // true
24 | dayjs('1970-00-00', 'YYYY-MM-DD', true).isValid() // false
25 | dayjs('1970-00-00', 'YYYY-MM-DD', 'es', true).isValid() // false
26 | ```
27 |
28 | If you don't know the exact format of an input string, but know it could be one of many, you can use an array of formats.
29 | ```js
30 | dayjs("12-25-2001", ["YYYY", "YYYY-MM-DD"], 'es', true);
31 | ```
32 |
33 | ### List of all available parsing tokens
34 |
35 | | Input | Example | Description |
36 | | ------ | ---------------- | --------------------------------- |
37 | | `YY` | 01 | Two-digit year |
38 | | `YYYY` | 2001 | Four-digit year |
39 | | `M` | 1-12 | Month, beginning at 1 |
40 | | `MM` | 01-12 | Month, 2-digits |
41 | | `MMM` | Jan-Dec | The abbreviated month name |
42 | | `MMMM` | January-December | The full month name |
43 | | `D` | 1-31 | Day of month |
44 | | `DD` | 01-31 | Day of month, 2-digits |
45 | | `H` | 0-23 | Hours |
46 | | `HH` | 00-23 | Hours, 2-digits |
47 | | `h` | 1-12 | Hours, 12-hour clock |
48 | | `hh` | 01-12 | Hours, 12-hour clock, 2-digits |
49 | | `m` | 0-59 | Minutes |
50 | | `mm` | 00-59 | Minutes, 2-digits |
51 | | `s` | 0-59 | Seconds |
52 | | `ss` | 00-59 | Seconds, 2-digits |
53 | | `S` | 0-9 | Hundreds of milliseconds, 1-digit |
54 | | `SS` | 00-99 | Tens of milliseconds, 2-digits |
55 | | `SSS` | 000-999 | Milliseconds, 3-digits |
56 | | `Z` | -05:00 | Offset from UTC |
57 | | `ZZ` | -0500 | Compact offset from UTC, 2-digits |
58 | | `A` | AM PM | Post or ante meridiem, upper-case |
59 | | `a` | am pm | Post or ante meridiem, lower-case |
60 | | `Do` | 1st... 31st | Day of Month with ordinal |
61 | | `X` | 1410715640.579 | Unix timestamp |
62 | | `x` | 1410715640579 | Unix ms timestamp |
63 |
64 |
65 | ### Differences to moment
66 |
67 | | title | parameters | dayjs | moment |
68 | |-------|------------|-------|--------|
69 | | invalid date with overflow | ('35/22/2010 99:88:77', 'DD-MM-YYYY HH:mm:ss') | '08-11-2011 04:29:17' | 'Invalid date' |
70 | | invalid date with overflow, strict | ('35/22/2010 99:88:77', 'DD-MM-YYYY HH:mm:ss', true) | 'Invalid Date' | 'Invalid date' |
71 | | '0' day or month (using default values) | ('1970-00-00', 'YYYY-MM-DD') | '1970-01-01' | 'Invalid date' |
72 | | '0' day or month (using default values), strict | ('1970-00-00', 'YYYY-MM-DD', true) | 'Invalid Date' | 'Invalid date' |
73 | | date not matching format | ('10/12/2014', 'YYYY-MM-DD') | '01-01-2014' | '12-20-2010' |
74 | | date not matching format, strict | ('10/12/2014', 'YYYY-MM-DD', true) | 'Invalid Date' | 'Invalid date' |
75 | | first match vs. longest match | ('2012-05-28 10:21:15', ['YYYY', 'YYYY-MM-DD', 'YYYY-MM-DD HH:mm:ss']) | '2012-01-01 00:00:00' | '2012-05-28 10:21:15' |
76 | | first match vs. longest match, strict | ('2012-05-28 10:21:15', ['YYYY', 'YYYY-MM-DD', 'YYYY-MM-DD HH:mm:ss'], true) | '2012-05-28 10:21:15' | '2012-05-28 10:21:15' |
77 |
78 |
79 |
80 | ### List of all recognized separator characters:
81 | `-_:.,()/`
82 |
83 |
84 |
--------------------------------------------------------------------------------
/docs/parse/string.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: string
3 | title: String
4 | ---
5 |
6 | Parse the given string in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format (a space instead of the 'T' is allowed) and return a Day.js object instance.
7 |
8 | ```js
9 | dayjs('2018-04-04T16:00:00.000Z')
10 | dayjs('2018-04-13 19:18:17.040+02:00')
11 | dayjs('2018-04-13 19:18')
12 | ```
13 |
14 | >For consistent results parsing anything **other** than ISO 8601 strings, you should use [String + Format](./string-format).
15 |
16 |
17 |
--------------------------------------------------------------------------------
/docs/parse/unix-timestamp-milliseconds.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: unix-timestamp-milliseconds
3 | title: Unix Timestamp (milliseconds)
4 | ---
5 |
6 | Create a Day.js object by passing an integer value representing the number of milliseconds (13 digits, since the Unix Epoch Jan 1 1970 12AM UTC).
7 |
8 | ```js
9 | dayjs(1318781876406)
10 | ```
11 |
12 | > The passing argument must be a **number**
13 |
--------------------------------------------------------------------------------
/docs/parse/unix-timestamp.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: unix-timestamp
3 | title: Unix Timestamp (seconds)
4 | ---
5 |
6 | Create a Day.js object from a Unix timestamp (10 digits, seconds since the Unix Epoch).
7 |
8 | ```js
9 | dayjs.unix(1318781876)
10 | ```
11 |
12 | This is implemented as `dayjs(timestamp * 1000)`, so partial seconds in the input timestamp are included.
13 |
14 | ```js
15 | dayjs.unix(1318781876.721)
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/parse/utc.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: utc
3 | title: UTC
4 | ---
5 |
6 | By default, Day.js parses and displays in local time.
7 |
8 | If you want to parse or display a date-time in UTC, you can use `dayjs.utc()` instead of `dayjs()`.
9 |
10 | While in UTC mode, all display methods will display in UTC time instead of local time.
11 |
12 | @>UTC
13 |
14 | ```js
15 | dayjs.extend(utc)
16 |
17 | // default local time
18 | dayjs().format() //2019-03-06T08:00:00+08:00
19 | // UTC mode
20 | dayjs.utc().format() // 2019-03-06T00:00:00Z
21 | ```
22 |
23 | Additionally, while in UTC mode, all getters and setters will internally use the `Date#getUTC*` and `Date#setUTC*` methods instead of the `Date#get*` and `Date#set*` methods.
24 |
25 | ```js
26 | dayjs.utc().seconds(30).valueOf()// => new Date().setUTCSeconds(30)
27 | dayjs.utc().seconds()// => new Date().getUTCSeconds()
28 | ```
29 | To switch from UTC to local time, you can use [dayjs#utc](../manipulate/utc) or [dayjs#local](../manipulate/local).
30 |
--------------------------------------------------------------------------------
/docs/plugin/advanced-format.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: advanced-format
3 | title: AdvancedFormat
4 | ---
5 |
6 | AdvancedFormat extends `dayjs().format` API to supply more format options.
7 |
8 | ```javascript
9 | var advancedFormat = require("dayjs/plugin/advancedFormat");
10 | // import advancedFormat from 'dayjs/plugin/advancedFormat' // ES 2015
11 |
12 | dayjs.extend(advancedFormat);
13 |
14 | dayjs().format("Q Do k kk X x");
15 | ```
16 |
17 | Note: some of the format options like `z` and `zzz` in the table below require additional plugins.
18 |
19 | List of added formats:
20 |
21 | | Format | Output | Description |
22 | | ------ | --------------------- | --------------------------------------- |
23 | | `Q` | 1-4 | Quarter |
24 | | `Do` | 1st 2nd ... 31st | Day of Month with ordinal |
25 | | `k` | 1-24 | The hour, beginning at 1 |
26 | | `kk` | 01-24 | The hour, 2-digits, beginning at 1 |
27 | | `X` | 1360013296 | Unix Timestamp in second |
28 | | `x` | 1360013296123 | Unix Timestamp in millisecond |
29 | | `w` | 1 2 ... 52 53 | Week of year @>>WeekOfYear |
30 | | `ww` | 01 02 ... 52 53 | Week of year, 2-digits @>>WeekOfYear |
31 | | `W` | 1 2 ... 52 53 | ISO Week of year @>>IsoWeek |
32 | | `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits @>>IsoWeek |
33 | | `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal @>>WeekOfYear |
34 | | `gggg` | 2017 | Week Year @>>WeekYear |
35 | | `GGGG` | 2017 | ISO Week Year @>>IsoWeek |
36 | | `z` | EST | Abbreviated named offset @>>Timezone |
37 | | `zzz` | Eastern Standard Time | Unabbreviated named offset @>>Timezone |
38 |
--------------------------------------------------------------------------------
/docs/plugin/array-support.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: array-support
3 | title: ArraySupport
4 | ---
5 |
6 | ArraySupport extends `dayjs()`, `dayjs.utc` APIs to support array argument.
7 |
8 | ```javascript
9 | var arraySupport = require("dayjs/plugin/arraySupport");
10 | // import arraySupport from 'dayjs/plugin/arraySupport' // ES 2015
11 |
12 | dayjs.extend(arraySupport);
13 |
14 | dayjs([2010, 1, 14, 15, 25, 50, 125]);
15 | dayjs.utc([2010, 1, 14, 15, 25, 50, 125]);
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/plugin/bad-mutable.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: bad-mutable
3 | title: BadMutable
4 | ---
5 |
6 | Day.js is designed to be immutable, however, in order to make it fully compatible with moment.js in some legacy projects we introduced a plugin 🚨 BadMutable 🚨 to make Day.js mutable.
7 |
8 | > This is **NOT** good and **NOT** recommended for most projects.
9 |
10 | With this plugin enabled, all setters will update the instance itself.
11 |
12 | ```javascript
13 | var badMutable = require("dayjs/plugin/badMutable");
14 | // import badMutable from 'dayjs/plugin/badMutable' // ES 2015
15 |
16 | dayjs.extend(badMutable);
17 | // with 🚨 BadMutable 🚨 plugin
18 | const today = dayjs();
19 | today.add(1, "day");
20 | console.log(today); // update itself, value will be tomorrow
21 | ```
22 |
--------------------------------------------------------------------------------
/docs/plugin/bigint-support.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: bigint-support
3 | title: BigIntSupport
4 | ---
5 |
6 | BigIntSupport extends `dayjs()`, `dayjs.unix` APIs to support [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) argument.
7 |
8 | ```javascript
9 | var bigIntSupport = require("dayjs/plugin/bigIntSupport");
10 | // import bigIntSupport from 'dayjs/plugin/bigIntSupport' // ES 2015
11 |
12 | dayjs.extend(bigIntSupport);
13 |
14 | dayjs(BigInt(1666310421101));
15 | dayjs.unix(BigInt(1666311003));
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/plugin/buddhist-era.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: buddhist-era
3 | title: BuddhistEra
4 | ---
5 |
6 | BuddhistEra extends `dayjs().format` API to supply Buddhist Era (B.E.) format options.
7 |
8 | Buddhist Era is a year numbering system that primarily used in mainland Southeast Asian countries of Cambodia, Laos, Myanmar and Thailand as well as in Sri Lanka and Chinese populations of Malaysia and Singapore for religious or official occasions ([Wikipedia](https://en.wikipedia.org/wiki/Buddhist_calendar)).
9 |
10 | To calculate BE year manually, just add 543 to year. For example 26 May 1977 AD/CE should display as 26 May 2520 BE (1977 + 543).
11 |
12 | ```javascript
13 | var buddhistEra = require("dayjs/plugin/buddhistEra");
14 | // import buddhistEra from 'dayjs/plugin/buddhistEra' // ES 2015
15 |
16 | dayjs.extend(buddhistEra);
17 |
18 | dayjs().format("BBBB BB");
19 | ```
20 |
21 | List of added formats:
22 |
23 | | Format | Output | Description |
24 | | ------ | ------ | ------------------------- |
25 | | `BBBB` | 2561 | Full BE Year (Year + 543) |
26 | | `BB` | 61 | 2-digit of BE Year |
27 |
--------------------------------------------------------------------------------
/docs/plugin/calendar.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: calendar
3 | title: Calendar
4 | ---
5 |
6 | Calendar adds `.calendar` API to return a `string` to display calendar time
7 |
8 | ```javascript
9 | var calendar = require("dayjs/plugin/calendar");
10 | // import calendar from 'dayjs/plugin/calendar' // ES 2015
11 |
12 | dayjs.extend(calendar);
13 |
14 | dayjs().calendar(dayjs("2008-01-01"));
15 | dayjs().calendar(null, {
16 | sameDay: "[Today at] h:mm A", // The same day ( Today at 2:30 AM )
17 | nextDay: "[Tomorrow at] h:mm A", // The next day ( Tomorrow at 2:30 AM )
18 | nextWeek: "dddd [at] h:mm A", // The next week ( Sunday at 2:30 AM )
19 | lastDay: "[Yesterday at] h:mm A", // The day before ( Yesterday at 2:30 AM )
20 | lastWeek: "[Last] dddd [at] h:mm A", // Last week ( Last Monday at 2:30 AM )
21 | sameElse: "DD/MM/YYYY", // Everything else ( 17/10/2011 )
22 | });
23 | ```
24 |
--------------------------------------------------------------------------------
/docs/plugin/custom-parse-format.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: custom-parse-format
3 | title: CustomParseFormat
4 | ---
5 |
6 | CustomParseFormat extends `dayjs()` constructor to support custom formats of input strings.
7 |
8 | ```javascript
9 | var customParseFormat = require("dayjs/plugin/customParseFormat");
10 | // import customParseFormat from 'dayjs/plugin/customParseFormat' // ES 2015
11 |
12 | dayjs.extend(customParseFormat);
13 |
14 | dayjs("05/02/69 1:02:03 PM -05:00", "MM/DD/YY H:mm:ss A Z");
15 | // Returns an instance containing '1969-05-02T18:02:03.000Z'
16 |
17 | dayjs("2018 Enero 15", "YYYY MMMM DD", "es");
18 | // Returns an instance containing '2018-01-15T00:00:00.000Z'
19 |
20 | dayjs("1970-00-00", "YYYY-MM-DD", true); // strict parsing
21 | ```
22 |
23 | [List of all available parsing tokens](../parse/string-format#list-of-all-available-parsing-tokens)
24 |
--------------------------------------------------------------------------------
/docs/plugin/day-of-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: day-of-year
3 | title: DayOfYear
4 | ---
5 |
6 | DayOfYear adds `.dayOfYear()` API to returns a `number` indicating the `Dayjs`'s day of the year, or to set the day of the year.
7 |
8 | ```javascript
9 | var dayOfYear = require("dayjs/plugin/dayOfYear");
10 | // import dayOfYear from 'dayjs/plugin/dayOfYear' // ES 2015
11 |
12 | dayjs.extend(dayOfYear);
13 |
14 | dayjs("2010-01-01").dayOfYear(); // 1
15 | dayjs("2010-01-01").dayOfYear(365); // 2010-12-31
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/plugin/dev-helper.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: dev-helper
3 | title: DevHelper
4 | ---
5 |
6 | DevHelper adds some helper function to give you more hints and warnings while using Day.js.
7 |
8 | Note, you can set `process.env.NODE_ENV` to `production` to disable the DevHelper in your production environment. And if you have enabled a JavaScript minifier like UglifyJS, it can remove the plugin from your production bundle automatically to save some bytes.
9 |
10 | ```js
11 | var devHelper = require("dayjs/plugin/devHelper");
12 | // import devHelper from 'dayjs/plugin/devHelper' // ES 2015
13 |
14 | dayjs.extend(devHelper);
15 | ```
16 |
17 | You can also load this plugin on demand yourself.
18 |
19 | ```js
20 | if (isInDevelopment) {
21 | // load DevHelper plugin like above
22 | }
23 | ```
24 |
--------------------------------------------------------------------------------
/docs/plugin/duration.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: duration
3 | title: Duration
4 | ---
5 |
6 | Duration adds `.duration` `.isDuration` APIs to support duration.
7 |
8 | ```javascript
9 | var duration = require("dayjs/plugin/duration");
10 | // import duration from 'dayjs/plugin/duration' // ES 2015
11 |
12 | dayjs.extend(duration);
13 |
14 | dayjs.duration(100);
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/plugin/is-between.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-between
3 | title: IsBetween
4 | ---
5 |
6 | IsBetween adds `.isBetween()` API to returns a `boolean` indicating if a date is between two other dates.
7 |
8 | ```javascript
9 | var isBetween = require("dayjs/plugin/isBetween");
10 | // import isBetween from 'dayjs/plugin/isBetween' // ES 2015
11 |
12 | dayjs.extend(isBetween);
13 |
14 | // To use `year` granularity pass the third parameter
15 | dayjs("2010-10-20").isBetween("2010-10-19", dayjs("2010-10-25"), "year");
16 |
17 | // Parameter 4 is a string with two characters; '[' means inclusive, '(' exclusive
18 | // '()' excludes start and end date (default)
19 | // '[]' includes start and end date
20 | // '[)' includes the start date but excludes the stop
21 | // Granuality offers the precision on start and end inclusive checks.
22 | // For example including the start date on day precision you should use 'day' as 3rd parameter.
23 | dayjs("2016-10-30").isBetween("2016-01-01", "2016-10-30", "day", "[)");
24 | ```
25 |
--------------------------------------------------------------------------------
/docs/plugin/is-leap-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-leap-year
3 | title: IsLeapYear
4 | ---
5 |
6 | IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.
7 |
8 | ```javascript
9 | var isLeapYear = require("dayjs/plugin/isLeapYear");
10 | // import isLeapYear from 'dayjs/plugin/isLeapYear' // ES 2015
11 |
12 | dayjs.extend(isLeapYear);
13 |
14 | dayjs("2000-01-01").isLeapYear(); // true
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/plugin/is-same-or-after.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-same-or-after
3 | title: IsSameOrAfter
4 | ---
5 |
6 | IsSameOrAfter adds `.isSameOrAfter()` API to return a `boolean` indicating if a date is the same or after another date.
7 |
8 | ```javascript
9 | var isSameOrAfter = require("dayjs/plugin/isSameOrAfter");
10 | // import isSameOrAfter from 'dayjs/plugin/isSameOrAfter' // ES 2015
11 |
12 | dayjs.extend(isSameOrAfter);
13 |
14 | dayjs("2010-10-20").isSameOrAfter("2010-10-19", "year");
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/plugin/is-same-or-before.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-same-or-before
3 | title: IsSameOrBefore
4 | ---
5 |
6 | IsSameOrBefore adds `.isSameOrBefore()` API to returns a `boolean` indicating if a date is same or before another date.
7 |
8 | ```javascript
9 | var isSameOrBefore = require("dayjs/plugin/isSameOrBefore");
10 | // import isSameOrBefore from 'dayjs/plugin/isSameOrBefore' // ES 2015
11 |
12 | dayjs.extend(isSameOrBefore);
13 |
14 | dayjs("2010-10-20").isSameOrBefore("2010-10-19", "year");
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/plugin/is-today.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-today
3 | title: IsToday
4 | ---
5 |
6 | IsToday adds `.isToday()` API to indicates whether the Day.js object is today or not.
7 |
8 | ```js
9 | var isToday = require("dayjs/plugin/isToday");
10 | // import isToday from 'dayjs/plugin/isToday' // ES 2015
11 |
12 | dayjs.extend(isToday);
13 |
14 | dayjs().isToday(); // true
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/plugin/is-tomorrow.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-tomorrow
3 | title: IsTomorrow
4 | ---
5 |
6 | IsTomorrow adds `.isTomorrow()` API to indicates whether the Day.js object is tomorrow or not.
7 |
8 | ```js
9 | var isTomorrow = require("dayjs/plugin/isTomorrow");
10 | // import isTomorrow from 'dayjs/plugin/isTomorrow' // ES 2015
11 |
12 | dayjs.extend(isTomorrow);
13 |
14 | dayjs().add(1, "day").isTomorrow(); // true
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/plugin/is-yesterday.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-yesterday
3 | title: IsYesterday
4 | ---
5 |
6 | IsYesterday adds `.isYesterday()` API to indicates whether the Day.js object is yesterday or not.
7 |
8 | ```js
9 | var isYesterday = require("dayjs/plugin/isYesterday");
10 | // import isYesterday from 'dayjs/plugin/isYesterday' // ES 2015
11 |
12 | dayjs.extend(isYesterday);
13 |
14 | dayjs().add(-1, "day").isYesterday(); // true
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/plugin/iso-week.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: iso-week
3 | title: IsoWeek
4 | ---
5 |
6 | IsoWeek adds `.isoWeek()` API to get or set the ISO week of the year. And adds `.isoWeekday()` to get or set ISO day of the week and `.isoWeekYear()` to get ISO week-year, and extends `.startOf` `.endOf` APIs to support unit `isoWeek`.
7 |
8 | ```javascript
9 | var isoWeek = require("dayjs/plugin/isoWeek");
10 | // import isoWeek from 'dayjs/plugin/isoWeek' // ES 2015
11 |
12 | dayjs.extend(isoWeek);
13 |
14 | dayjs().isoWeek();
15 | dayjs().isoWeekday();
16 | dayjs().isoWeekYear();
17 | ```
18 |
--------------------------------------------------------------------------------
/docs/plugin/iso-weeks-in-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: iso-weeks-in-year
3 | title: IsoWeeksInYear
4 | ---
5 |
6 | IsoWeeksInYear adds `.isoWeeksInYear()` API to return a `number` to get the number of weeks in year, according to ISO weeks.
7 |
8 | @>IsLeapYear
9 |
10 | ```javascript
11 | var isoWeeksInYear = require("dayjs/plugin/isoWeeksInYear");
12 | // import isoWeeksInYear from 'dayjs/plugin/isoWeeksInYear' // ES 2015
13 |
14 | var isLeapYear = require("dayjs/plugin/isLeapYear"); // dependent on isLeapYear plugin
15 | // import isLeapYear from 'dayjs/plugin/isLeapYear' // ES 2015
16 |
17 | dayjs.extend(isoWeeksInYear);
18 | dayjs.extend(isLeapYear);
19 |
20 | dayjs("2004-01-01").isoWeeksInYear(); // 53
21 | dayjs("2005-01-01").isoWeeksInYear(); // 52
22 | ```
23 |
--------------------------------------------------------------------------------
/docs/plugin/loading-into-browser.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: loading-into-browser
3 | title: Loading plugin in the browser
4 | ---
5 | Loading plugin on demand.
6 |
7 | ```html
8 |
9 |
10 |
13 | ```
14 |
15 | Day.js is available on [CDN](../installation/browser#cdn-resource).
16 |
17 | ```html
18 |
19 |
20 |
21 |
22 | ```
23 |
--------------------------------------------------------------------------------
/docs/plugin/loading-into-nodejs.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: loading-into-nodejs
3 | title: Loading plugin in NodeJS
4 | ---
5 | Loading plugin on demand.
6 |
7 | ```javascript
8 | var AdvancedFormat = require('dayjs/plugin/advancedFormat')
9 | // import AdvancedFormat from 'dayjs/plugin/advancedFormat' // ES 2015
10 |
11 | dayjs.extend(AdvancedFormat) // use plugin
12 | ```
13 |
--------------------------------------------------------------------------------
/docs/plugin/locale-data.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: locale-data
3 | title: LocaleData
4 | ---
5 |
6 | LocaleData extends `dayjs().localeData` API to supply locale data.
7 |
8 | ```javascript
9 | var localeData = require("dayjs/plugin/localeData");
10 | // import localeData from 'dayjs/plugin/localeData' // ES 2015
11 |
12 | dayjs.extend(localeData);
13 |
14 | dayjs().localeData();
15 | ```
16 |
17 | Available methods:
18 |
19 | ```js
20 | dayjs.months();
21 | dayjs.monthsShort();
22 | dayjs.weekdays();
23 | dayjs.weekdaysShort();
24 | dayjs.weekdaysMin();
25 | dayjs.longDateFormat("L");
26 |
27 | globalLocaleData = dayjs.localeData();
28 | globalLocaleData.firstDayOfWeek();
29 | globalLocaleData.months();
30 | globalLocaleData.monthsShort();
31 | globalLocaleData.weekdays();
32 | globalLocaleData.weekdaysShort();
33 | globalLocaleData.weekdaysMin();
34 | globalLocaleData.longDateFormat("L");
35 |
36 | globalLocaleData.months(dayjs());
37 | globalLocaleData.monthsShort(dayjs());
38 | globalLocaleData.weekdays(dayjs());
39 | globalLocaleData.weekdaysShort(dayjs());
40 | globalLocaleData.weekdaysMin(dayjs());
41 | globalLocaleData.meridiem();
42 | globalLocaleData.ordinal();
43 |
44 | instanceLocaleData = dayjs().localeData();
45 | instanceLocaleData.firstDayOfWeek();
46 | instanceLocaleData.months();
47 | instanceLocaleData.monthsShort();
48 | instanceLocaleData.weekdays();
49 | instanceLocaleData.weekdaysShort();
50 | instanceLocaleData.weekdaysMin();
51 | instanceLocaleData.longDateFormat("L");
52 | instanceLocaleData.meridiem();
53 | instanceLocaleData.ordinal();
54 | ```
55 |
56 | Note: when you want use `longDateFormat('L')`, remember extend `localizedFormat`
57 |
58 | ```js
59 | import localizedFormat from "dayjs/plugin/localizedFormat";
60 | dayjs.extend(localizedFormat);
61 | ```
62 |
--------------------------------------------------------------------------------
/docs/plugin/localized-format.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: localized-format
3 | title: LocalizedFormat
4 | ---
5 |
6 | LocalizedFormat extends `dayjs().format` API to supply localized format options.
7 |
8 | ```javascript
9 | var localizedFormat = require("dayjs/plugin/localizedFormat");
10 | // import localizedFormat from 'dayjs/plugin/localizedFormat' // ES 2015
11 |
12 | dayjs.extend(localizedFormat);
13 |
14 | dayjs().format("L LT");
15 | ```
16 |
17 | [List of localized formats](../display/format#list-of-localized-formats)
18 |
--------------------------------------------------------------------------------
/docs/plugin/min-max.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: min-max
3 | title: MinMax
4 | ---
5 |
6 | MinMax adds `.min` `.max` APIs to return a `dayjs` to compare given dayjs instances.
7 | This accepts both multiple arguments and array that contains Day.js instance.
8 |
9 | ```javascript
10 | var minMax = require("dayjs/plugin/minMax");
11 | // import minMax from 'dayjs/plugin/minMax' // ES 2015
12 |
13 | dayjs.extend(minMax);
14 |
15 | dayjs.max(dayjs(), dayjs("2018-01-01"), dayjs("2019-01-01"));
16 | dayjs.min([dayjs(), dayjs("2018-01-01"), dayjs("2019-01-01")]);
17 | ```
18 |
--------------------------------------------------------------------------------
/docs/plugin/object-support.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: object-support
3 | title: ObjectSupport
4 | ---
5 |
6 | ObjectSupport extends `dayjs()`, `dayjs.utc`, `dayjs().set`, `dayjs().add`, `dayjs().subtract` APIs to support object argument.
7 |
8 | ```javascript
9 | var objectSupport = require("dayjs/plugin/objectSupport");
10 | // import objectSupport from 'dayjs/plugin/objectSupport' // ES 2015
11 |
12 | dayjs.extend(objectSupport);
13 |
14 | dayjs({
15 | year: 2010,
16 | month: 1,
17 | day: 12,
18 | });
19 | dayjs.utc({
20 | year: 2010,
21 | month: 1,
22 | day: 12,
23 | });
24 | dayjs().set({ year: 2010, month: 1, day: 12 });
25 | dayjs().add({ M: 1 });
26 | dayjs().subtract({ month: 1 });
27 | ```
28 |
--------------------------------------------------------------------------------
/docs/plugin/plugin.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: plugin
3 | title: Plugin
4 | ---
5 |
6 | A plugin is an independent module that can be added to Day.js to extend functionality or add new features.
7 |
8 | By default, Day.js comes with core code only and no installed plugin.
9 |
10 | You can load multiple plugins based on your need.
11 |
12 |
13 | ## Customize
14 |
15 | You could build your own Day.js plugin to meet different needs.
16 |
17 | Feel free to open a pull request to share your plugin.
18 |
19 | Template of a Day.js plugin.
20 |
21 | ```javascript
22 | export default (option, dayjsClass, dayjsFactory) => {
23 | // extend dayjs()
24 | // e.g. add dayjs().isSameOrBefore()
25 | dayjsClass.prototype.isSameOrBefore = function(arguments) {}
26 |
27 | // extend dayjs
28 | // e.g. add dayjs.utc()
29 | dayjsFactory.utc = arguments => {}
30 |
31 | // overriding existing API
32 | // e.g. extend dayjs().format()
33 | const oldFormat = dayjsClass.prototype.format
34 | dayjsClass.prototype.format = function(arguments) {
35 | // original format result
36 | const result = oldFormat.bind(this)(arguments)
37 | // return modified result
38 | }
39 | }
40 | ```
41 |
--------------------------------------------------------------------------------
/docs/plugin/plural-get-set.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: plural-get-set
3 | title: PluralGetSet
4 | ---
5 |
6 | PluralGetSet adds plural getter & setter APIs `.milliseconds()`, `.seconds()`, `.minutes()`, `.hours()`, `.days()`, `.weeks()`, `.isoWeeks()`, `.months()`, `.quarters()`, `.years()`, `.dates()`.
7 |
8 | ```javascript
9 | var pluralGetSet = require("dayjs/plugin/pluralGetSet");
10 | // import pluralGetSet from 'dayjs/plugin/pluralGetSet' // ES 2015
11 |
12 | dayjs.extend(pluralGetSet);
13 |
14 | dayjs().millisecond();
15 | dayjs().milliseconds();
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/plugin/preparse-postformat.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: preparse-postformat
3 | title: PreParsePostFormat
4 | ---
5 | Pre-parse / Post-format lets you process the input before the parser and process the string output after the formatter. [Based on similar behavior for locales in moment.js](https://momentjs.com/docs/#/i18n/locale-data/).
6 |
7 | NOTE: this plugin requires the localeData plugin to be imported before it (as it depends on it's functionality).
8 |
9 | NOTE: this plugin also affects the relative time plugin, also by design (mimics the moment.js implementaiton behavior).
10 |
11 | ## Sample usage
12 | e.g. [In the AR locale specifically, it is used to support Arabic numerals](https://github.com/iamkun/dayjs/pull/1255/commits/e26e802d767eec89aae02c8cecf87f517600a698).
13 |
14 | ```javascript
15 | // Arabic [ar]
16 | import dayjs from 'dayjs'
17 | import preParsePostFormat from 'dayjs/plugin/preParsePostFormat'
18 | dayjs.extend(preParsePostFormat)
19 |
20 | const months = 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_')
21 | const symbolMap = {
22 | 1: '١',
23 | 2: '٢',
24 | 3: '٣',
25 | 4: '٤',
26 | 5: '٥',
27 | 6: '٦',
28 | 7: '٧',
29 | 8: '٨',
30 | 9: '٩',
31 | 0: '٠'
32 | }
33 |
34 | const numberMap = {
35 | '١': '1',
36 | '٢': '2',
37 | '٣': '3',
38 | '٤': '4',
39 | '٥': '5',
40 | '٦': '6',
41 | '٧': '7',
42 | '٨': '8',
43 | '٩': '9',
44 | '٠': '0'
45 | }
46 |
47 | const locale = {
48 | name: 'ar',
49 | // ...
50 | preparse(string) {
51 | return string
52 | .replace(
53 | /[١٢٣٤٥٦٧٨٩٠]/g,
54 | match => numberMap[match]
55 | )
56 | .replace(/،/g, ',')
57 | },
58 | postformat(string) {
59 | return string
60 | .replace(/\d/g, match => symbolMap[match])
61 | .replace(/,/g, '،')
62 | },
63 | // ...
64 | }
65 | // ...
66 | ```
67 |
68 | [The tests](https://github.com/iamkun/dayjs/blob/dev/test/plugin/preParsePostFormat.test.js) should also give you a good idea on how to use the plugin if this isn't clear enough ;).
69 |
--------------------------------------------------------------------------------
/docs/plugin/quarter-of-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: quarter-of-year
3 | title: QuarterOfYear
4 | ---
5 |
6 | QuarterOfYear adds `.quarter()` API to return to which quarter of the year belongs a date, and extends `.add` `.subtract` `.startOf` `.endOf` APIs to support unit `quarter`.
7 |
8 | ```javascript
9 | var quarterOfYear = require("dayjs/plugin/quarterOfYear");
10 | // import quarterOfYear from 'dayjs/plugin/quarterOfYear' // ES 2015
11 |
12 | dayjs.extend(quarterOfYear);
13 |
14 | dayjs("2010-04-01").quarter(); // 2
15 | dayjs("2010-04-01").quarter(2);
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/plugin/relative-time.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: relative-time
3 | title: RelativeTime
4 | ---
5 |
6 | RelativeTime adds `.from` `.to` `.fromNow` `.toNow` APIs to formats date to relative time strings (e.g. 3 hours ago).
7 |
8 | ```javascript
9 | var relativeTime = require("dayjs/plugin/relativeTime");
10 | // import relativeTime from 'dayjs/plugin/relativeTime' // ES 2015
11 |
12 | dayjs.extend(relativeTime);
13 |
14 | dayjs().from(dayjs("1990-01-01")); // in 31 years
15 | dayjs().from(dayjs("1990-01-01"), true); // 31 years
16 | dayjs().fromNow();
17 |
18 | dayjs().to(dayjs("1990-01-01")); // "31 years ago"
19 | dayjs().toNow();
20 | ```
21 |
22 | ### Time from now `.fromNow(withoutSuffix?: boolean)`
23 |
24 | Returns the `string` of relative time from now.
25 |
26 | ### Time from X `.from(compared: Dayjs, withoutSuffix?: boolean)`
27 |
28 | Returns the `string` of relative time from X.
29 |
30 | ### Time to now `.toNow(withoutSuffix?: boolean)`
31 |
32 | Returns the `string` of relative time to now.
33 |
34 | ### Time to X `.to(compared: Dayjs, withoutSuffix?: boolean)`
35 |
36 | Returns the `string` of relative time to X.
37 |
38 | [List of breakdown range](../display/from-now#list-of-breakdown-range)
39 |
--------------------------------------------------------------------------------
/docs/plugin/timezone.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: timezone
3 | title: Timezone
4 | ---
5 |
6 | Timezone adds `dayjs.tz` `.tz` `.tz.guess` `.tz.setDefault` APIs to parse or display between time zones.
7 |
8 | ```javascript
9 | var utc = require("dayjs/plugin/utc");
10 | // import utc from 'dayjs/plugin/utc' // ES 2015
11 |
12 | var timezone = require("dayjs/plugin/timezone"); // dependent on utc plugin
13 | // import timezone from 'dayjs/plugin/timezone' // ES 2015
14 |
15 | dayjs.extend(utc);
16 | dayjs.extend(timezone);
17 |
18 | const timestamp = "2014-06-01 12:00";
19 | const tz = "America/New_York";
20 |
21 | const dayjsLocal = dayjs(timestamp); //assumes UTC
22 | //dayjsLocal.toISOString() -> 2014-06-01T12:00:00.000Z
23 | //dayjsLocal.format('YYYY-MM-DDTHH:mm:ss') -> 2014-06-01T12:00:00
24 |
25 | const dayjsAmerica = dayjsLocal.tz(tz); //existing time treated as UTC
26 | //dayjsAmerica.toISOString() -> 2014-06-01T12:00:00.000Z
27 | //dayjsAmerica.format('YYYY-MM-DDTHH:mm:ss') -> 2014-06-01T08:00:00
28 |
29 | const dayjsAmericaKeep = dayjsLocal.tz(tz, true); //existing time treated as local time
30 | //dayjsAmericaKeep.toISOString() -> 2014-06-01T16:00:00.000Z
31 | //dayjsAmericaKeep.format('YYYY-MM-DDTHH:mm:ss') -> 2014-06-01T12:00:00
32 | ```
33 |
34 | Guessing the user timezone
35 |
36 | ```javascript
37 | dayjs.tz.guess();
38 | ```
39 |
40 | Parsing in a timezone
41 |
42 | ```javascript
43 | const d1 = dayjs.tz("2013-11-18 11:55", "Asia/Taipei");
44 | d1.format(); // => 2013-11-18T11:55:00+08:00
45 | d1.toISOString(); // => 2013-11-18T03:55:00.000Z
46 | ```
47 |
48 | Converting to a timezone
49 |
50 | ```javascript
51 | const d2 = dayjs.utc("2013-11-18 11:55").tz("Asia/Taipei");
52 | d2.format(); // => 2013-11-18T19:55:00+08:00
53 | d2.toISOString(); // => 2013-11-18T11:55:00.000Z
54 | ```
55 |
56 | Set / reset the default timezone (used by 'tz')
57 |
58 | ```javascript
59 | // Setting the default timezone
60 | dayjs.tz.setDefault("America/New_York");
61 |
62 | // Resetting the default timezone to the system timezone
63 | dayjs.tz.setDefault();
64 | ```
65 |
66 | ### Differences to moment
67 |
68 | Using the default timezone makes `moment(dateValue)` use this timezone (but `moment.tz(dateValue, timezone)` still requires the second parameter).
69 |
70 | But `dayjs(dateValue)` always uses the local timezone, even if `dayjs.tz.setDefault` is used; only `dayjs.tz(dateValue)` (without second parameter) uses the default timezone.
71 |
--------------------------------------------------------------------------------
/docs/plugin/to-array.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: to-array
3 | title: ToArray
4 | ---
5 |
6 | ToArray adds `.toArray()` API to return an `array` that mirrors the parameters
7 |
8 | ```javascript
9 | var toArray = require("dayjs/plugin/toArray");
10 | // import toArray from 'dayjs/plugin/toArray' // ES 2015
11 |
12 | dayjs.extend(toArray);
13 |
14 | dayjs("2019-01-25").toArray(); // [ 2019, 0, 25, 0, 0, 0, 0 ]
15 | ```
16 |
--------------------------------------------------------------------------------
/docs/plugin/to-object.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: to-object
3 | title: ToObject
4 | ---
5 |
6 | ToObject adds `.toObject()` API to return an `object` with the date's properties.
7 |
8 | ```javascript
9 | var toObject = require("dayjs/plugin/toObject");
10 | // import toObject from 'dayjs/plugin/toObject' // ES 2015
11 |
12 | dayjs.extend(toObject);
13 |
14 | dayjs("2019-01-25").toObject();
15 | /* { years: 2019,
16 | months: 0,
17 | date: 25,
18 | hours: 0,
19 | minutes: 0,
20 | seconds: 0,
21 | milliseconds: 0 } */
22 | ```
23 |
--------------------------------------------------------------------------------
/docs/plugin/update-locale.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: update-locale
3 | title: UpdateLocale
4 | ---
5 |
6 | UpdateLocale adds `.updateLocale` API to update a locale's properties.
7 |
8 | ```javascript
9 | var updateLocale = require('dayjs/plugin/updateLocale')
10 | // import updateLocale from 'dayjs/plugin/updateLocale' // ES 2015
11 |
12 | dayjs.extend(updateLocale)
13 |
14 | dayjs.updateLocale('en', {
15 | months : String[]
16 | })
17 | ```
18 |
--------------------------------------------------------------------------------
/docs/plugin/utc.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: utc
3 | title: UTC
4 | ---
5 |
6 | UTC adds `.utc` `.local` `.isUTC` APIs to parse or display in UTC.
7 |
8 | ```javascript
9 | var utc = require("dayjs/plugin/utc");
10 | // import utc from 'dayjs/plugin/utc' // ES 2015
11 |
12 | dayjs.extend(utc);
13 |
14 | // default local time
15 | dayjs().format(); //2019-03-06T17:11:55+08:00
16 |
17 | // UTC mode
18 | dayjs.utc().format(); // 2019-03-06T09:11:55Z
19 |
20 | // convert local time to UTC time
21 | dayjs().utc().format(); // 2019-03-06T09:11:55Z
22 |
23 | // While in UTC mode, all display methods will display in UTC time instead of local time.
24 | // And all getters and setters will internally use the Date#getUTC* and Date#setUTC* methods instead of the Date#get* and Date#set* methods.
25 | dayjs.utc().isUTC(); // true
26 | dayjs.utc().local().format(); //2019-03-06T17:11:55+08:00
27 | dayjs.utc("2018-01-01", "YYYY-MM-DD"); // with CustomParseFormat plugin
28 | ```
29 |
30 | By default, Day.js parses and displays in local time.
31 |
32 | If you want to parse or display in UTC, you can use `dayjs.utc()` instead of `dayjs()`.
33 |
34 | #### dayjs.utc `dayjs.utc(dateType?: string | number | Date | Dayjs, format? string)`
35 |
36 | Returns a `Dayjs` object in UTC mode.
37 |
38 | #### Use UTC time `.utc()`
39 |
40 | Returns a cloned `Dayjs` object with a flag to use UTC time.
41 |
42 | #### Use local time `.local()`
43 |
44 | Returns a cloned `Dayjs` object with a flag to use local time.
45 |
46 | #### Set UTC offset `.utcOffset()`
47 |
48 | Returns a cloned `Dayjs` object with a new UTC offset.
49 |
50 | #### isUTC mode `.isUTC()`
51 |
52 | Returns a `boolean` indicating current `Dayjs` object is in UTC mode or not.
53 |
--------------------------------------------------------------------------------
/docs/plugin/week-of-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: week-of-year
3 | title: weekOfYear
4 | ---
5 |
6 | WeekOfYear adds `.week()` API to returns a `number` indicating the `Dayjs`'s week of the year.
7 |
8 | ```javascript
9 | var weekOfYear = require("dayjs/plugin/weekOfYear");
10 | // import weekOfYear from 'dayjs/plugin/weekOfYear' // ES 2015
11 |
12 | dayjs.extend(weekOfYear);
13 |
14 | dayjs("2018-06-27").week(); // 26
15 | dayjs("2018-06-27").week(5); // set week
16 | ```
17 |
--------------------------------------------------------------------------------
/docs/plugin/week-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: week-year
3 | title: WeekYear
4 | ---
5 |
6 | WeekYear adds `.weekYear()` API to get locale aware week of the year.
7 |
8 | ```javascript
9 | var weekYear = require("dayjs/plugin/weekYear"); // dependent on weekOfYear plugin
10 | // import weekYear from 'dayjs/plugin/weekYear' // ES 2015
11 |
12 | var weekOfYear = require("dayjs/plugin/weekOfYear");
13 | // import weekOfYear from 'dayjs/plugin/weekOfYear' // ES 2015
14 |
15 | dayjs.extend(weekOfYear);
16 | dayjs.extend(weekYear);
17 |
18 | dayjs().weekYear();
19 | ```
20 |
--------------------------------------------------------------------------------
/docs/plugin/weekday.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: weekday
3 | title: Weekday
4 | ---
5 |
6 | Weekday adds `.weekday()` API to get or set locale aware day of the week.
7 |
8 | ```javascript
9 | var weekday = require("dayjs/plugin/weekday");
10 | // import weekday from 'dayjs/plugin/weekday' // ES 2015
11 |
12 | dayjs.extend(weekday);
13 |
14 | // when Sunday is the first day of the week
15 | dayjs().weekday(-7); // last Sunday
16 | dayjs().weekday(7); // next Sunday
17 |
18 | // when Monday is the first day of the week
19 | dayjs().weekday(-7); // last Monday
20 | dayjs().weekday(7); // next Monday
21 | ```
22 |
--------------------------------------------------------------------------------
/docs/query/is-a-dayjs.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-a-dayjs
3 | title: Is a Dayjs
4 | ---
5 |
6 | This indicates whether a variable is a Day.js object or not.
7 |
8 | ```js
9 | dayjs.isDayjs(dayjs()) // true
10 | dayjs.isDayjs(new Date()) // false
11 | ```
12 |
13 | The operator `instanceof` works equally well:
14 |
15 | ```js
16 | dayjs() instanceof dayjs // true
17 | ```
18 |
--------------------------------------------------------------------------------
/docs/query/is-after.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-after
3 | title: Is After
4 | ---
5 | This indicates whether the Day.js object is after the other supplied date-time.
6 |
7 | ```js
8 | dayjs().isAfter(dayjs('2011-01-01')) // default milliseconds
9 | ```
10 | If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter. In that case the comparision respects the given unit and the units above.
11 |
12 | ```js
13 | dayjs().isAfter('2011-01-01', 'month') // compares month and year
14 | ```
15 |
16 | Units are case insensitive, and support plural and short forms.
17 |
18 | [List of all available units](../manipulate/start-of#list-of-all-available-units)
19 |
--------------------------------------------------------------------------------
/docs/query/is-before.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-before
3 | title: Is Before
4 | ---
5 |
6 | This indicates whether the Day.js object is before the other supplied date-time.
7 |
8 | ```js
9 | dayjs().isBefore(dayjs('2011-01-01')) // default milliseconds
10 | ```
11 | If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter. In that case, the comparison respects the given unit and the units above.
12 |
13 | ```js
14 | dayjs().isBefore('2011-01-01', 'month') // compares month and year
15 | ```
16 |
17 | Units are case insensitive, and support plural and short forms.
18 |
19 | [List of all available units](../manipulate/start-of#list-of-all-available-units)
20 |
--------------------------------------------------------------------------------
/docs/query/is-between.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-between
3 | title: Is Between
4 | ---
5 |
6 | This indicates whether the Day.js object is between two other supplied date-time.
7 |
8 | @>IsBetween
9 |
10 | ```js
11 | dayjs.extend(isBetween)
12 | dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'))
13 | // default milliseconds
14 | ```
15 | If you want to limit the granularity to a unit other than milliseconds, pass it as the third parameter. In that case the comparision respects the given unit and the units above.
16 |
17 | ```js
18 | dayjs().isBetween('2010-10-19', '2010-10-25', 'month') // compares month and year
19 | ```
20 |
21 | Units are case insensitive, and support plural and short forms.
22 |
23 | [List of all available units](../manipulate/start-of#list-of-all-available-units)
24 |
25 | The fourth parameter is about inclusivity. A `[` indicates inclusion of a value. A `(` indicates exclusion.
26 |
27 | If the inclusivity parameter is used, both indicators must be passed.
28 |
29 | ```js
30 | dayjs('2016-10-30').isBetween('2016-01-01', '2016-10-30', null, '[)')
31 | ```
32 |
--------------------------------------------------------------------------------
/docs/query/is-leap-year.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-leap-year
3 | title: Is Leap Year
4 | ---
5 |
6 | This indicates whether the Day.js object's year is a leap year or not.
7 |
8 | @>IsLeapYear
9 |
10 | ```js
11 | dayjs.extend(isLeapYear)
12 |
13 | dayjs('2000-01-01').isLeapYear() // true
14 | ```
15 |
--------------------------------------------------------------------------------
/docs/query/is-same-or-after.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-same-or-after
3 | title: Is Same or After
4 | ---
5 |
6 | This indicates whether the Day.js object is the same or after another supplied date-time.
7 |
8 | @>IsSameOrAfter
9 |
10 | ```js
11 | dayjs.extend(isSameOrAfter)
12 | dayjs().isSameOrAfter(dayjs('2011-01-01')) // default milliseconds
13 | ```
14 | If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
15 |
16 | ```js
17 | dayjs().isSameOrAfter('2011-01-01', 'year')
18 | ```
19 |
20 | Units are case insensitive, and support plural and short forms.
21 |
22 | [List of all available units](../manipulate/start-of#list-of-all-available-units)
23 |
--------------------------------------------------------------------------------
/docs/query/is-same-or-before.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-same-or-before
3 | title: Is Same or Before
4 | ---
5 |
6 | This indicates whether the Day.js object is the same or before another supplied date-time.
7 |
8 | @>IsSameOrBefore
9 |
10 | ```js
11 | dayjs.extend(isSameOrBefore)
12 | dayjs().isSameOrBefore(dayjs('2011-01-01')) // default milliseconds
13 | ```
14 | If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
15 |
16 | ```js
17 | dayjs().isSameOrBefore('2011-01-01', 'year')
18 | ```
19 |
20 | Units are case insensitive, and support plural and short forms.
21 |
22 | [List of all available units](../manipulate/start-of#list-of-all-available-units)
23 |
--------------------------------------------------------------------------------
/docs/query/is-same.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: is-same
3 | title: Is Same
4 | ---
5 |
6 | This indicates whether the Day.js object is the same as the other supplied date-time.
7 |
8 | ```js
9 | dayjs().isSame(dayjs('2011-01-01')) // default milliseconds
10 | ```
11 | If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.
12 |
13 | When including a second parameter, it will match all units equal or larger. Passing in `month` will check `month` and `year`. Passing in `day` will check `day`, `month`, and `year`.
14 |
15 | ```js
16 | dayjs().isSame('2011-01-01', 'year')
17 | ```
18 |
19 | Units are case insensitive, and support plural and short forms.
20 |
21 | [List of all available units](../manipulate/start-of#list-of-all-available-units)
22 |
--------------------------------------------------------------------------------
/docs/query/query.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: query
3 | title: Query
4 | ---
5 |
6 | There are several methods to query a Day.js object.
7 |
--------------------------------------------------------------------------------
/docs/timezone/converting-to-zone.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: converting-to-zone
3 | title: Converting to Zone
4 | ---
5 |
6 | Change the time zone and update the offset and return a Day.js object instance.
7 |
8 | @>Timezone
9 | ```javascript
10 | dayjs.extend(utc)
11 | dayjs.extend(timezone)
12 |
13 | // this example runs in time zone 'Europe/Berlin' (offset +01:00)
14 | dayjs("2013-11-18T11:55:20") // '2013-11-18T11:55:20+01:00'
15 | dayjs("2013-11-18T11:55:20").tz("America/Toronto") // '2013-11-18T05:55:20-05:00'
16 | dayjs("2013-11-18T11:55:20").tz("America/Toronto", true) // '2013-11-18T11:55:20-05:00'
17 | ```
18 |
19 | On passing a second parameter as true, only the timezone (and offset) is updated, keeping the local time same.
20 |
--------------------------------------------------------------------------------
/docs/timezone/guessing-user-timezone.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: guessing-user-timezone
3 | title: Guessing user zone
4 | ---
5 |
6 | Return the user's time zone.
7 |
8 | @>Timezone
9 | ```javascript
10 | dayjs.extend(utc)
11 | dayjs.extend(timezone)
12 | dayjs.tz.guess() // America/Chicago
13 | ```
14 |
--------------------------------------------------------------------------------
/docs/timezone/parsing-in-zone.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: parsing-in-zone
3 | title: Parsing in Zone
4 | ---
5 |
6 | Parse date-time string in the given timezone and return a Day.js object instance.
7 |
8 | @>Timezone
9 | ```javascript
10 | dayjs.extend(utc)
11 | dayjs.extend(timezone)
12 | dayjs.tz("2013-11-18T11:55:20", "America/Toronto") // '2013-11-18T11:55:20-05:00'
13 | ```
14 |
15 | If you know the format of an input string, you can use that to parse a date, the arguments are the same as [String + Format](../parse/string-format).
16 |
17 | @>CustomParseFormat
18 | ```javascript
19 | dayjs.extend(customParseFormat)
20 | dayjs.tz("12-25-1995", "MM-DD-YYYY", "America/Toronto")
21 | ```
22 |
--------------------------------------------------------------------------------
/docs/timezone/set-default-timezone.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: set-default-timezone
3 | title: Set Default Timezone
4 | ---
5 |
6 | Change default timezone from local time zone to your custom timezone.
7 |
8 | You can still custom a different timezone in a specific `dayjs` object.
9 |
10 | @>Timezone
11 | ```javascript
12 | dayjs.extend(utc)
13 | dayjs.extend(timezone)
14 |
15 | dayjs.tz.setDefault("America/New_York")
16 |
17 | // The same behavior with dayjs.tz("2014-06-01 12:00", "America/New_York")
18 | dayjs.tz("2014-06-01 12:00") // 2014-06-01T12:00:00-04:00
19 |
20 | // use another timezone
21 | dayjs.tz("2014-06-01 12:00", "Asia/Tokyo") // 2014-06-01T12:00:00+09:00
22 |
23 | // reset timezone
24 | dayjs.tz.setDefault()
25 | ```
26 |
27 | Notice: `dayjs.tz.setDefault` will not affect existing `dayjs` objects.
28 |
--------------------------------------------------------------------------------
/docs/timezone/timezone.md:
--------------------------------------------------------------------------------
1 | ---
2 | id: timezone
3 | title: Time Zone
4 | ---
5 |
6 | Day.js supports time zone via the [Internationalization API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) in [supported environments](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#Browser_compatibility). By using the native API, no extra bytes of timezone data need to be included in code bundle.
7 | The list of all time zone names can be found in the [IANA database](https://www.iana.org/time-zones).
8 |
9 | For legacy or unsupported environments, please use a proper [polyfill](https://github.com/formatjs/date-time-format-timezone).
10 |
11 | @>Timezone
12 |
13 | ```javascript
14 | dayjs.extend(utc)
15 | dayjs.extend(timezone)
16 |
17 | // current time zone is 'Europe/Berlin' (offset +01:00)
18 | // Parsing
19 | dayjs.tz("2013-11-18 11:55:20", "America/Toronto") // '2013-11-18T11:55:20-05:00'
20 |
21 | // Converting (from time zone 'Europe/Berlin'!)
22 | dayjs("2013-11-18 11:55:20").tz("America/Toronto") // '2013-11-18T05:55:20-05:00'
23 | ```
24 |
--------------------------------------------------------------------------------
/website/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "semi": false,
3 | "singleQuote": true,
4 | "printWidth": 120
5 | }
6 |
--------------------------------------------------------------------------------
/website/README.md:
--------------------------------------------------------------------------------
1 | # DAYJS Website
2 |
--------------------------------------------------------------------------------
/website/core/Footer.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-present, Facebook, Inc.
3 | *
4 | * This source code is licensed under the MIT license found in the
5 | * LICENSE file in the root directory of this source tree.
6 | */
7 |
8 | const React = require('react')
9 |
10 | class Footer extends React.Component {
11 | docUrl(doc, language) {
12 | const baseUrl = this.props.config.baseUrl
13 | const docsUrl = this.props.config.docsUrl
14 | const docsPart = `${docsUrl ? `${docsUrl}/` : ''}`
15 | const langPart = `${language ? `${language}/` : ''}`
16 | return `${baseUrl}${docsPart}${langPart}${doc}`
17 | }
18 |
19 | pageUrl(doc, language) {
20 | const baseUrl = this.props.config.baseUrl
21 | return baseUrl + (language ? `${language}/` : '') + doc
22 | }
23 |
24 | render() {
25 | return (
26 |
156 | )
157 | }
158 | }
159 |
160 | module.exports = Footer
161 |
--------------------------------------------------------------------------------
/website/languages.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-present, Facebook, Inc.
3 | *
4 | * This source code is licensed under the MIT license found in the
5 | * LICENSE file in the root directory of this source tree.
6 | */
7 |
8 | const languages = [
9 | {
10 | enabled: true,
11 | name: 'English',
12 | tag: 'en'
13 | },
14 | {
15 | enabled: true,
16 | name: '中文',
17 | tag: 'zh-CN'
18 | },
19 | {
20 | enabled: true,
21 | name: 'Русский',
22 | tag: 'ru-RU',
23 | },
24 | {
25 | enabled: true,
26 | name: 'Español',
27 | tag: 'es-ES'
28 | },
29 |
30 | ]
31 |
32 | module.exports = languages
33 |
--------------------------------------------------------------------------------
/website/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "dayjs-website",
3 | "version": "0.1.0",
4 | "scripts": {
5 | "examples": "docusaurus-examples",
6 | "start": "docusaurus-start",
7 | "build": "docusaurus-build",
8 | "publish-gh-pages": "docusaurus-publish",
9 | "write-translations": "docusaurus-write-translations",
10 | "crowdin-upload": "crowdin upload sources --config ../crowdin.yaml --auto-update -b master",
11 | "crowdin-download": "crowdin download --config ../crowdin.yaml -b master",
12 | "version": "docusaurus-version",
13 | "rename-version": "docusaurus-rename-version"
14 | },
15 | "devDependencies": {
16 | "docusaurus": "1.13.0",
17 | "@crowdin/cli": "^3.19.4"
18 | },
19 | "license": "MIT",
20 | "private": true
21 | }
22 |
--------------------------------------------------------------------------------
/website/pages/en/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-present, Facebook, Inc.
3 | *
4 | * This source code is licensed under the MIT license found in the
5 | * LICENSE file in the root directory of this source tree.
6 | */
7 |
8 | const React = require('react')
9 |
10 | const translate = require('../../server/translate.js').translate
11 |
12 | class Banner extends React.Component {
13 | render() {
14 | const { siteConfig, language = '' } = this.props
15 | const { baseUrl, docsUrl } = siteConfig
16 | const docsPart = `${docsUrl ? `${docsUrl}/` : ''}`
17 | const langPart = `${language ? `${language}/` : ''}`
18 | const docUrl = doc => `${baseUrl}${docsPart}${langPart}${doc}`
19 |
20 | return (
21 |
22 |
23 |
{siteConfig.title}
24 |
25 | Fast 2kB alternative to Moment.js with the same modern API
26 |
27 |
32 |
33 |
34 | )
35 | }
36 | }
37 |
38 | function Intro() {
39 | return (
40 |
41 |
42 |
43 |
44 |
45 | Why Day.js?
46 |
47 |
48 |
49 | 2kB
50 |
51 |
52 | Less JavaScript to download, parse and execute, leaving more time for your code.
53 |
54 |
55 |
56 |
57 | Simple
58 |
59 |
60 |
61 | Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and
62 | times for modern browsers with a largely Moment.js-compatible API.
63 |
64 |
65 |
66 | If you use Moment.js, you already know how to use Day.js.
67 |
68 |
69 |
70 |
71 | Immutable
72 |
73 |
74 |
75 | All API operations that change the Day.js object will return a new instance instead.
76 |
77 |
78 |
79 | This helps prevent bugs and avoid long debugging sessions.
80 |
81 |
82 |
83 |
84 | I18n
85 |
86 |
87 | Day.js has great support for internationalization. But none of them will be included in your build unless
88 | you use them.
89 |
90 |
91 |
92 |
93 |
94 |
Special Sponsors
95 |
124 |
125 | Balancing coding projects and academics? Discover a professional
essay service that can help streamline your workload.
126 |
127 |
130 |
135 |
136 | Pay-as-you-go
online class help with weekly
137 | expert support, no upfront payment.
138 |
139 |
140 |
141 |
142 | This requires the
143 |
144 |
145 | plugin to work
146 |
147 |
148 | dependent
149 |
150 |
151 | plugin
152 |
153 |
154 |
155 | )
156 | }
157 |
158 | function DayJS(props) {
159 | const { config: siteConfig, language = 'en' } = props
160 |
161 | return (
162 |
163 |
164 |
165 |
166 | )
167 | }
168 |
169 | module.exports = DayJS
170 |
--------------------------------------------------------------------------------
/website/plugin.js:
--------------------------------------------------------------------------------
1 | const translate = require(process.cwd() + '/node_modules/docusaurus/lib/server/translate.js').translate;
2 |
3 | const parse = md => {
4 | if (md.src.charCodeAt(md.pos) !== 64) { // @
5 | return false
6 | }
7 |
8 | const match = /@>>?([\S]+)/.exec(md.src)
9 | if (!match) return false
10 |
11 | md.pos += match[0].length
12 |
13 | const token = {
14 | type: 'pluginNotice',
15 | level: md.level,
16 | content: {
17 | inline: md.src.indexOf('>>') > -1,
18 | plugin: match[1],
19 | match: match,
20 | },
21 | }
22 |
23 | md.push(token)
24 | return true
25 | }
26 |
27 | const render = (tokens, idx) => {
28 | const token = tokens[idx]
29 | const pluginName = token.content.plugin
30 | let pluginSnakeName
31 | if (['UTC'].indexOf(pluginName) === -1) {
32 | pluginSnakeName = pluginName.replace(/([A-Z])/g, "-$1").toLowerCase().slice(1)
33 | } else {
34 | pluginSnakeName = pluginName.toLowerCase()
35 | }
36 | const isInline = token.content.inline
37 | if (isInline) {
38 | return `(
39 | ${translate("dependent|plugin")}
40 |
41 | ${token.content.plugin}
42 |
43 | ${translate("plugin|plugin")}
44 | )`
45 | }
46 | return `
47 | ${translate("This requires the|plugin")}
48 |
49 | ${token.content.plugin}
50 |
51 | ${translate("plugin to work|plugin")}
52 |
`
53 | }
54 |
55 | const pluginNotice = ctx => {
56 | ctx.inline.ruler.push('pluginNotice', parse)
57 | ctx.renderer.rules['pluginNotice'] = render
58 | }
59 |
60 | module.exports = pluginNotice
61 |
--------------------------------------------------------------------------------
/website/sidebars.json:
--------------------------------------------------------------------------------
1 | {
2 | "docs": {
3 | "Installation": [
4 | "installation/installation",
5 | "installation/node-js",
6 | "installation/browser",
7 | "installation/typescript",
8 | "installation/download"
9 | ],
10 | "Parse": [
11 | "parse/parse",
12 | "parse/now",
13 | "parse/string",
14 | "parse/string-format",
15 | "parse/unix-timestamp-milliseconds",
16 | "parse/unix-timestamp",
17 | "parse/date",
18 | "parse/object",
19 | "parse/array",
20 | "parse/utc",
21 | "parse/dayjs-clone",
22 | "parse/is-valid"
23 | ],
24 | "Get + Set": [
25 | "get-set/get-set",
26 | "get-set/millisecond",
27 | "get-set/second",
28 | "get-set/minute",
29 | "get-set/hour",
30 | "get-set/date",
31 | "get-set/day",
32 | "get-set/weekday",
33 | "get-set/iso-weekday",
34 | "get-set/day-of-year",
35 | "get-set/week",
36 | "get-set/iso-week",
37 | "get-set/month",
38 | "get-set/quarter",
39 | "get-set/year",
40 | "get-set/week-year",
41 | "get-set/iso-week-year",
42 | "get-set/iso-weeks-in-year",
43 | "get-set/get",
44 | "get-set/set",
45 | "get-set/max",
46 | "get-set/min"
47 | ],
48 | "Manipulate": [
49 | "manipulate/manipulate",
50 | "manipulate/add",
51 | "manipulate/subtract",
52 | "manipulate/start-of",
53 | "manipulate/end-of",
54 | "manipulate/local",
55 | "manipulate/utc",
56 | "manipulate/utc-offset"
57 | ],
58 | "Display": [
59 | "display/display",
60 | "display/format",
61 | "display/from-now",
62 | "display/from",
63 | "display/to-now",
64 | "display/to",
65 | "display/calendar-time",
66 | "display/difference",
67 | "display/unix-timestamp-milliseconds",
68 | "display/unix-timestamp",
69 | "display/days-in-month",
70 | "display/as-javascript-date",
71 | "display/as-array",
72 | "display/as-json",
73 | "display/as-iso-string",
74 | "display/as-object",
75 | "display/as-string"
76 | ],
77 | "Query": [
78 | "query/query",
79 | "query/is-before",
80 | "query/is-same",
81 | "query/is-after",
82 | "query/is-same-or-before",
83 | "query/is-same-or-after",
84 | "query/is-between",
85 | "query/is-a-dayjs",
86 | "query/is-leap-year"
87 | ],
88 | "i18n": [
89 | "i18n/i18n",
90 | "i18n/loading-into-nodejs",
91 | "i18n/loading-into-browser",
92 | "i18n/changing-locale",
93 | "i18n/instance-locale",
94 | "i18n/getting-locale",
95 | "i18n/listing-months-weekdays",
96 | "i18n/locale-data"
97 | ],
98 | "Plugins": [
99 | "plugin/plugin",
100 | "plugin/loading-into-nodejs",
101 | "plugin/loading-into-browser",
102 | "plugin/advanced-format",
103 | "plugin/array-support",
104 | "plugin/bad-mutable",
105 | "plugin/bigint-support",
106 | "plugin/buddhist-era",
107 | "plugin/calendar",
108 | "plugin/custom-parse-format",
109 | "plugin/day-of-year",
110 | "plugin/dev-helper",
111 | "plugin/duration",
112 | "plugin/is-between",
113 | "plugin/is-leap-year",
114 | "plugin/is-same-or-after",
115 | "plugin/is-same-or-before",
116 | "plugin/is-today",
117 | "plugin/is-tomorrow",
118 | "plugin/is-yesterday",
119 | "plugin/iso-week",
120 | "plugin/iso-weeks-in-year",
121 | "plugin/locale-data",
122 | "plugin/localized-format",
123 | "plugin/min-max",
124 | "plugin/object-support",
125 | "plugin/plural-get-set",
126 | "plugin/preparse-postformat",
127 | "plugin/quarter-of-year",
128 | "plugin/relative-time",
129 | "plugin/timezone",
130 | "plugin/to-array",
131 | "plugin/to-object",
132 | "plugin/update-locale",
133 | "plugin/utc",
134 | "plugin/week-of-year",
135 | "plugin/week-year",
136 | "plugin/weekday"
137 | ],
138 | "Customize": [
139 | "customization/customization",
140 | "customization/month-names",
141 | "customization/month-abbreviations",
142 | "customization/weekday-names",
143 | "customization/weekday-abbreviations",
144 | "customization/weekday-min",
145 | "customization/relative-time",
146 | "customization/calendar"
147 | ],
148 | "Durations": [
149 | "durations/durations",
150 | "durations/creating",
151 | "durations/clone",
152 | "durations/humanize",
153 | "durations/format",
154 | "durations/milliseconds",
155 | "durations/seconds",
156 | "durations/minutes",
157 | "durations/hours",
158 | "durations/days",
159 | "durations/weeks",
160 | "durations/months",
161 | "durations/years",
162 | "durations/add",
163 | "durations/subtract",
164 | "durations/diffing",
165 | "durations/as",
166 | "durations/get",
167 | "durations/as-json",
168 | "durations/is-a-duration",
169 | "durations/as-iso-string",
170 | "durations/locale"
171 | ],
172 | "Time Zone": [
173 | "timezone/timezone",
174 | "timezone/parsing-in-zone",
175 | "timezone/converting-to-zone",
176 | "timezone/guessing-user-timezone",
177 | "timezone/set-default-timezone"
178 | ]
179 | },
180 | "docs-other": {
181 | "Other": []
182 | }
183 | }
184 |
--------------------------------------------------------------------------------
/website/siteConfig.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017-present, Facebook, Inc.
3 | *
4 | * This source code is licensed under the MIT license found in the
5 | * LICENSE file in the root directory of this source tree.
6 | */
7 |
8 | // See https://docusaurus.io/docs/site-config for all the possible
9 | // site configuration options.
10 |
11 | // List of projects/orgs using your project for the users page.
12 | const users = []
13 | const markdownPlugins = require('./plugin')
14 | const siteConfig = {
15 | markdownPlugins: [markdownPlugins],
16 | title: 'Day.js', // Title for your website.
17 | tagline: '2kB JavaScript date utility library',
18 | url: 'https://day.js.org', // Your website URL
19 | baseUrl: '/', // Base URL for your project */
20 | // For github.io type URLs, you would set the url and baseUrl like:
21 | // url: 'https://facebook.github.io',
22 | // baseUrl: '/test-site/',
23 |
24 | // Used for publishing and more
25 | projectName: 'website',
26 | organizationName: 'dayjs',
27 | // For top-level user or org sites, the organization is still the same.
28 | // e.g., for the https://JoelMarcey.github.io site, it would be set like...
29 | // organizationName: 'JoelMarcey'
30 |
31 | // For no header links in the top nav bar -> headerLinks: [],
32 | headerLinks: [
33 | { search: true },
34 | { doc: 'installation/installation', label: 'Docs' },
35 | // { blog: true, label: 'Blog' },
36 | { href: 'https://github.com/iamkun/dayjs/', label: 'GitHub' },
37 | { languages: true },
38 | ],
39 |
40 | translationRecruitingLink: 'https://crowdin.com/project/dayjs',
41 |
42 | // If you have users set above, you add it here:
43 | // users,
44 |
45 | /* path to images for header/footer */
46 | headerIcon: 'img/logo.png',
47 | footerIcon: 'img/logo.png',
48 | favicon: 'img/favicon.ico',
49 |
50 | /* Colors for website */
51 | colors: {
52 | primaryColor: '#fb6052',
53 | secondaryColor: '#eb6052',
54 | },
55 |
56 | /* Custom fonts for website */
57 | /*
58 | fonts: {
59 | myFont: [
60 | "Times New Roman",
61 | "Serif"
62 | ],
63 | myOtherFont: [
64 | "-apple-system",
65 | "system-ui"
66 | ]
67 | },
68 | */
69 |
70 | // This copyright info is used in /core/Footer.js and blog RSS/Atom feeds.
71 | copyright: `Copyright © ${new Date().getFullYear()} Day.js`,
72 |
73 | docsSideNavCollapsible: true,
74 |
75 | editUrl: 'https://github.com/dayjs/website/blob/master/docs/',
76 |
77 | scrollToTop: false,
78 |
79 | highlight: {
80 | // Highlight.js theme to use for syntax highlighting in code blocks.
81 | // theme: 'github'
82 | themeUrl: 'https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/default.min.css',
83 | },
84 |
85 | // Add custom scripts here that would be placed in