A plugin is an independent module that can be added to Day.js to extend functionality or add new features.
By default, Day.js comes with core code only and no installed plugin.
You can load multiple plugins based on your need.
- Returns dayjs
Use a plugin.
import plugin
dayjs.extend(plugin)
dayjs.extend(plugin, options) // with plugin options
- Via NPM:
import dayjs from 'dayjs'
import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' // load on demand
dayjs.extend(AdvancedFormat) // use plugin
- Via CDN:
<script src="https://unpkg.com/dayjs"></script>
<!-- Load plugin as window.dayjs_plugin_NAME -->
<script src="https://unpkg.com/dayjs/plugin/advancedFormat"></script>
<script>
dayjs.extend(dayjs_plugin_advancedFormat);
</script>
- AdvancedFormat extends
dayjs().format
API to supply more format options.
import advancedFormat from 'dayjs/plugin/advancedFormat'
dayjs.extend(advancedFormat)
dayjs().format('Q Do k kk X x')
List of added formats:
Format | Output | Description |
---|---|---|
Q |
1-4 | Quarter |
Do |
1st 2nd ... 31st | Day of Month with ordinal |
k |
1-23 | The hour, beginning at 1 |
kk |
01-23 | The hour, 2-digits, beginning at 1 |
X |
1360013296 | Unix Timestamp in second |
x |
1360013296123 | Unix Timestamp in millisecond |
- RelativeTime adds
.from
.to
.fromNow
.toNow
APIs to formats date to relative time strings (e.g. 3 hours ago).
import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)
dayjs().from(dayjs('1990')) // 2 years ago
dayjs().from(dayjs(), true) // 2 years
dayjs().fromNow()
dayjs().to(dayjs())
dayjs().toNow()
Returns the string
of relative time from now.
Returns the string
of relative time from X.
Returns the string
of relative time to now.
Returns the string
of relative time to X.
Range | Key | Sample Output |
---|---|---|
0 to 44 seconds | s | a few seconds ago |
45 to 89 seconds | m | a minute ago |
90 seconds to 44 minutes | mm | 2 minutes ago ... 44 minutes ago |
45 to 89 minutes | h | an hour ago |
90 minutes to 21 hours | hh | 2 hours ago ... 21 hours ago |
22 to 35 hours | d | a day ago |
36 hours to 25 days | dd | 2 days ago ... 25 days ago |
26 to 45 days | M | a month ago |
46 days to 10 months | MM | 2 months ago ... 10 months ago |
11 months to 17months | y | a year ago |
18 months+ | yy | 2 years ago ... 20 years ago |
You could build your own Day.js plugin to meet different needs.
Feel free to open a pull request to share your plugin.
Template of a Day.js plugin.
export default (option, dayjsClass, dayjsFactory) => {
// extend dayjs()
// e.g. add dayjs().isSameOrBefore()
dayjsClass.prototype.isSameOrBefore = function (arguments) {}
// extend dayjs
// e.g. add dayjs.utc()
dayjsFactory.utc = (arguments) => {}
// overriding existing API
// e.g. extend dayjs().format()
const oldFormat = dayjsClass.prototype.format
dayjsClass.prototype.format = function (arguments) {
// original format result
const result = oldFormat(arguments)
// return modified result
}
}