All Projects → guisturdy → dayjs-plugin-utc

guisturdy / dayjs-plugin-utc

Licence: other
a plugin for dayjs gives dayjs the ability to operate UTC timezone

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to dayjs-plugin-utc

moment-dayjs-codemod
A Codemod to migrate from moment.js to day.js
Stars: ✭ 20 (+25%)
Mutual labels:  dayjs
hifitime
A high fidelity time management library in Rust
Stars: ✭ 67 (+318.75%)
Mutual labels:  utc
ember-dayjs-helpers
Ember Template helpers based on day.js
Stars: ✭ 19 (+18.75%)
Mutual labels:  dayjs
time-formater
在javascript中显示日期。
Stars: ✭ 44 (+175%)
Mutual labels:  utc
svelte-time
Format a timestamp using day.js
Stars: ✭ 70 (+337.5%)
Mutual labels:  dayjs
sqlalchemy-utc
SQLAlchemy type to store aware datetime values
Stars: ✭ 87 (+443.75%)
Mutual labels:  utc
react-modern-datepicker
A modern date picker for react library
Stars: ✭ 19 (+18.75%)
Mutual labels:  dayjs
Dayjs
⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Stars: ✭ 37,373 (+233481.25%)
Mutual labels:  dayjs
dayjs-business-days
Day.js plugin to add support for calculating dates only accounting for Business days
Stars: ✭ 21 (+31.25%)
Mutual labels:  dayjs
dayjs
Extended fork of Day.js - 2KB immutable date library alternative to Moment.js
Stars: ✭ 36 (+125%)
Mutual labels:  dayjs

English | 简体中文

this is a plugin for dayjs, that gives dayjs the ability to operate UTC timezone


Usage

  • Via NPM:
npm i dayjs-plugin-utc --save
import dayjsPluginUTC from 'dayjs-plugin-utc'

dayjs.extend(dayjsPluginUTC)
  • Via CDN:
<!-- Latest compiled and minified JavaScript -->
<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() // 2018-05-18T05:04:05+08:00

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() // 2018-05-18T03:04:05+06:00

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() // 2018-05-18T05:04:05+08:00

API

Parse

get an instance in UTC

  dayjs.utc() 
  dayjs.utc('2018-05-18T03:04:05+06:00') 
  /* (string | number | Date | Dayjs) support like dayjs() */

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() // (-480, -120, 0, 120, 480, etc.)
  dayjs().isLocal()   // true
  dayjs().isUTC()     // false

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()           // 2018-05-17T21:04:05+00:00

  day.local().format()         // 2018-05-18T05:04:05+08:00

  day.utcOffset(240).format()  // 2018-05-18T01:04:05+04:00
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].