@motss/utc-time
Generate JavaScript's UTC time with various offsets
!NPMnodei-badgenodei-url
!Build Statustravis-badgetravis-url !Versionversion-badgeversion-url !Downloadsdownloads-badgedownloads-url !MIT Licensemit-license-badgemit-license-url !Dependency Statusdaviddm-badgedaviddm-url !NSP Statusnsp-badgensp-url
!Code of Conductcoc-badgecoc-url !Codecovcodecov-badgecodecov-url !Coverage Statuscoveralls-badgecoveralls-url
!codebeat-badgecodebeat-url !codacy-badgecodacy-url !inch-badgeinch-url
Returns a JavaScript date object using the UTC timezone with optional offsets to adjust thehour
,minute
,second
ormillisecond
.
Table of contents
- Pre-requisites - Setup- [Install](#install)
- [Usage](#usage)
- [Node.js](#nodejs)
- [Native ES modules or TypeScript](#native-es-modules-or-typescript)
- API Reference- [UTCTimeOpts](#utctimeopts)
- [utcTime([UTCTimeOps])](#utctimeutctimeops)
- [utcTimeSync([UTCTimeOpts])](#utctimesyncutctimeopts)
- License
Pre-requisites
- Node.jsnode-js-url >= 8.9.0
- NPMnpm-url >= 5.5.1 (NPMnpm-url comes with Node.jsnode-js-url so there is no need to install separately.)
Setup
Install
# Install via NPM
$ npm install --save @motss/utc-time
Usage
Node.js
const {
utcTime,
// utcTimeSync,
} = require('@motss/utc-time');
void async function main() {
/** NOTE: Assuming today's date is '2020-02-02', */
const defaultUTCDatetime = await utcTime(); // utcTimeSync();
const defaultUTCDatetimeWithOffsets = await utcTime({
offset: {
hour: 3,
minute: 2,
second: 1,
millisecond: 0,
},
});
const specifiedUTCDatetime = await utcTime({
startDatetime: '2033-03-03T03:33:33.333Z',
});
assert(defaultUTCDatetime, new Date('2020-02-02T00:00:00.000Z')); // OK
assert(defaultUTCDatetimeWithOffsets, new Date('2020-02-02T03:02:01.000Z')); // OK
assert(specifiedUTCDatetime, new Date('2033-03-03T03:33:33.333Z')); // OK
}();
Native ES modules or TypeScript
// @ts-check
import {
utcTime,
// utcTimeSync,
} from '@motss/utc-time';
void async function main() {
/** NOTE: Assuming today's date is '2020-02-02', */
const defaultUTCDatetime = await utcTime(); // utcTimeSync();
const defaultUTCDatetimeWithOffsets = await utcTime({
offset: {
hour: 3,
minute: 2,
second: 1,
millisecond: 0,
},
});
const specifiedUTCDatetime = await utcTime({
startDatetime: '2033-03-03T03:33:33.333Z',
});
assert(defaultUTCDatetime, new Date('2020-02-02T00:00:00.000Z')); // OK
assert(defaultUTCDatetimeWithOffsets, new Date('2020-02-02T03:02:01.000Z')); // OK
assert(specifiedUTCDatetime, new Date('2033-03-03T03:33:33.333Z')); // OK
}();
API Reference
UTCTimeOpts
offsets
<Objectobject-mdn-url> Optional offset values when returning a JavaScript Date objectdate-mdn-url using UTCutc-url timezone.
hour
<numbernumber-mdn-url> Optional offset to adjust the hour
.
- minute
<numbernumber-mdn-url> Optional offset to adjust the minute
.
- second
<numbernumber-mdn-url> Optional offset to adjust the second
.
- millisecond
<numbernumber-mdn-url> Optional offset to adjust the millisecond
.startDatetime
<stringstring-mdn-url|numbernumber-mdn-url|Datedate-mdn-url> Optional starting datetime. Defaults to today's datetime if it is not provided.
utcTime(UTCTimeOps)
-UTCTimeOpts
<UTCTimeOptsutctimeopts-url> Optional configuration when returning a JavaScript Date objectdate-mdn-url using UTCutc-url timezone.
- returns: <Promisepromise-mdn-url<stringstring-mdn-url>> Promise which resolves with a JavaScript Date objectdate-mdn-url using UTCutc-url timezone.utcTimeSync(UTCTimeOpts)
This methods works the same asutcTime([UTCTimeOpts])
except that this is the synchronous version.