English | 简体中文
this is a plugin for dayjs, that gives dayjs the ability to operate UTC timezone
Usage
npm i dayjs-plugin-utc --save
import dayjsPluginUTC from 'dayjs-plugin-utc'
dayjs.extend(dayjsPluginUTC)
<script src="https://unpkg.com/dayjs"></script>
<script src="https://unpkg.com/dayjs-plugin-utc"></script>
<script>
dayjs.extend(dayjsPluginUTC.default)
</script>
⚠️ NOTICE⚠️
when NOT add this plugin dayjs()
will return an instance that timezone based of you local
dayjs('2018-05-18T03:04:05+06:00').format()
after load this plugin the timezone of instance return by dayjs()
will rely on what you passed
dayjs.extend(dayjsPluginUTC)
dayjs('2018-05-18T03:04:05+06:00').format()
if you always want an local timezone instance would be create or you already use dayjs in you project
you can load this plugin with option parseToLocal: true
dayjs.extend(dayjsPluginUTC, { parseToLocal: true })
dayjs('2018-05-18T03:04:05+06:00').format()
API
Parse
get an instance in UTC
dayjs.utc()
dayjs.utc('2018-05-18T03:04:05+06:00')
Get
with dayjs().utcOffset()
you can get the UTC offset in minutes.
Note: dayjs().utcOffset()
returns the real offset from UTC, not the reverse offset (as returned by Date.prototype.getTimezoneOffset).
also you can check the timezone of an instance is local or UTC by dayjs().isLocal()
and dayjs().isUTC()
dayjs().utcOffset()
dayjs().isLocal()
dayjs().isUTC()
Set
using dayjs().utc()
and dayjs().local()
you can set the timezone to UTC or you local timezone , and dayjs().utcOffset(Number)
you can specify the timezone you want
let day = dayjs('2018-05-18T03:04:05+06:00')
day.utc().format()
day.local().format()
day.utcOffset(240).format()