项目作者: motss

项目描述 :
Generate JavaScript's UTC dates with various offsets
高级语言: TypeScript
项目地址: git://github.com/motss/utc-date.git
创建时间: 2018-02-11T13:34:51Z
项目社区:https://github.com/motss/utc-date

开源协议:MIT License

下载


🚨 No longer maintained. Moved to @reallyland/node_mod. 🚨


utc-date



Generate JavaScript’s UTC dates with various offsets



Buy Me A Coffee
@igarshmyb"">tippin.me
Follow me

Version
Node version
MIT License

@motss/utc-date"">Downloads
@motss/utc-date"">Total downloads
Packagephobia
@motss/utc-date"">Bundlephobia

CircleCI
Dependency Status
codecov
Coverage Status

codebeat badge
Codacy Badge
Code of Conduct

Returns a JavaScript date object using the UTC timezone with optional offsets to adjust the year, month, or date.

Table of contents

Pre-requisites

Setup

Install

  1. # Install via NPM
  2. $ npm install --save @motss/utc-date

Usage

TypeScript or ES Modules

  1. import {
  2. utcDate,
  3. // utcDateSync,
  4. } from '@motss/utc-date';
  5. void async function main() {
  6. /** NOTE: Assuming today's date is '2020-02-02', */
  7. const defaultUTCDate = await utcDate();
  8. const defaultUTCDateWithOffsets = await utcDate({
  9. offset: {
  10. year: 2,
  11. month: 1,
  12. day: 0,
  13. },
  14. });
  15. const specifiedUTCDate = await utcDate({
  16. startDate: '2030-03-02',
  17. });
  18. assert(defaultUTCDate, new Date('2020-02-02T00:00:00.000Z')); // OK
  19. assert(defaultUTCDateWithOffsets, new Date('2022-03-02T00:00:00.000Z')); // OK
  20. assert(specifiedUTCDate, new Date('2030-03-02T00:00:00.000Z')); // OK
  21. }();

Node.js

  1. const {
  2. utcDate,
  3. // utcDateSync,
  4. } = require('@motss/utc-date');
  5. void async function main() {
  6. /** NOTE: Assuming today's date is '2020-02-02', */
  7. const defaultUTCDate = await utcDate(); // utcDateSync();
  8. const defaultUTCDateWithOffsets = await utcDate({
  9. offset: {
  10. year: 2,
  11. month: 1,
  12. day: 0,
  13. },
  14. });
  15. const specifiedUTCDate = await utcDate({
  16. startDate: '2030-03-02',
  17. });
  18. assert(defaultUTCDate, new Date('2020-02-02T00:00:00.000Z')); // OK
  19. assert(defaultUTCDateWithOffsets, new Date('2022-03-02T00:00:00.000Z')); // OK
  20. assert(specifiedUTCDate, new Date('2030-03-02T00:00:00.000Z')); // OK
  21. }();

Browser

ES Modules

  1. <script type="module">
  2. import { utcDate } from 'https://unpkg.com/@motss/utc-date@latest/dist/utc-date.js';
  3. utcDate()
  4. .then(console.log)
  5. .catch(console.error);
  6. </script>

IIFE

  1. <script src="https://unpkg.com/@motss/utc-date@latest/dist/utc-date.iife.js"></script>
  2. <script>
  3. const { utcDate } = window.UTCDate;
  4. utcDate()
  5. .then(console.log)
  6. .catch(console.error);
  7. </script>

API Reference

UTCDateParams

  • offsets <?Object> Optional offset values when returning a JavaScript Date object using the UTC timezone.
    • year <?number> Optional offset to adjust the year.
    • month <?number> Optional offset to adjust the month.
    • day <?number> Optional offset to adjust the day.
  • startDate <?string|?number|?Date> Optional starting date. Defaults to today’s date if it is not given.

utcDate([UTCDateParams])

utcDateSync([UTCDateParams])

This methods works the same as utcDate([UTCDateParams]) except that this is the synchronous version.

  • utc-time - Manipulate UTC Time with ease in JavaScript

License

MIT License © Rong Sen Ng