utc-date
Generate JavaScript's UTC dates with various offsets
!tippin.metippin-me-badgetippin-me-url !Follow mefollow-me-badgefollow-me-url
!Versionversion-badgeversion-url !Node versionnode-version-badgenode-version-url !MIT Licensemit-license-badgemit-license-url
!Downloadsdownloads-badgedownloads-url !Total downloadstotal-downloads-badgedownloads-url !Packagephobiapackagephobia-badgepackagephobia-url !Bundlephobiabundlephobia-badgebundlephobia-url
!CircleCIcircleci-badgecircleci-url !Dependency Statusdaviddm-badgedaviddm-url !codecovcodecov-badgecodecov-url !Coverage Statuscoveralls-badgecoveralls-url
!codebeat badgecodebeat-badgecodebeat-url !Codacy Badgecodacy-badgecodacy-url !Code of Conductcoc-badgecoc-url
Returns a JavaScript date objectdate-mdn-url using the UTCutc-url timezone with optional offsets to adjust theyear
,month
, ordate
.
Table of contents
- TypeScript or ES Modules - Node.js - Browser- [ES Modules](#es-modules)
- [IIFE](#iife)
- UTCDateParams
- utcDate(UTCDateParams)
- utcDateSync(UTCDateParams)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-date
Usage
TypeScript or ES Modules
import {
utcDate,
// utcDateSync,
} from '@motss/utc-date';
void async function main() {
/** NOTE: Assuming today's date is '2020-02-02', */
const defaultUTCDate = await utcDate();
const defaultUTCDateWithOffsets = await utcDate({
offset: {
year: 2,
month: 1,
day: 0,
},
});
const specifiedUTCDate = await utcDate({
startDate: '2030-03-02',
});
assert(defaultUTCDate, new Date('2020-02-02T00:00:00.000Z')); // OK
assert(defaultUTCDateWithOffsets, new Date('2022-03-02T00:00:00.000Z')); // OK
assert(specifiedUTCDate, new Date('2030-03-02T00:00:00.000Z')); // OK
}();
Node.js
const {
utcDate,
// utcDateSync,
} = require('@motss/utc-date');
void async function main() {
/** NOTE: Assuming today's date is '2020-02-02', */
const defaultUTCDate = await utcDate(); // utcDateSync();
const defaultUTCDateWithOffsets = await utcDate({
offset: {
year: 2,
month: 1,
day: 0,
},
});
const specifiedUTCDate = await utcDate({
startDate: '2030-03-02',
});
assert(defaultUTCDate, new Date('2020-02-02T00:00:00.000Z')); // OK
assert(defaultUTCDateWithOffsets, new Date('2022-03-02T00:00:00.000Z')); // OK
assert(specifiedUTCDate, new Date('2030-03-02T00:00:00.000Z')); // OK
}();
Browser
ES Modules
<script type="module">
import { utcDate } from 'https://unpkg.com/@motss/utc-date@latest/dist/utc-date.js';
utcDate()
.then(console.log)
.catch(console.error);
</script>
IIFE
<script src="https://unpkg.com/@motss/utc-date@latest/dist/utc-date.iife.js"></script>
<script>
const { utcDate } = window.UTCDate;
utcDate()
.then(console.log)
.catch(console.error);
</script>
API Reference
UTCDateParams
offsets
<?Objectobject-mdn-url> Optional offset values when returning a JavaScript Date objectdate-mdn-url using the UTCutc-url timezone.
year
<?numbernumber-mdn-url> Optional offset to adjust the year
.
- month
<?numbernumber-mdn-url> Optional offset to adjust the month
.
- day
<?numbernumber-mdn-url> Optional offset to adjust the day
.startDate
<?stringstring-mdn-url|?numbernumber-mdn-url|?Datedate-mdn-url> Optional starting date. Defaults to today's date if it is not given.
utcDate(UTCDateParams)
UTCDateParams
<?UTCDateParamsutc-date-opts-url> Optional configuration when returning a JavaScript Date objectdate-mdn-url using the UTCutc-url timezone.- returns: <Promisepromise-mdn-url<Datedate-mdn-url>> Promise which resolves with a JavaScript Date objectdate-mdn-url using the UTCutc-url timezone.
utcDateSync(UTCDateParams)
This methods works the same asutcDate([UTCDateParams])
except that this is the synchronous version.Related works
- utc-timeutc-time-url - Manipulate UTC Time with ease in JavaScript