Socket
Socket
Sign inDemoInstall

date-duration

Package Overview
Dependencies
1
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    date-duration

Manipulate Date objects with ISO 8601-formatted durations


Version published
Maintainers
1
Install size
58.7 kB
Created

Readme

Source

date-duration ci

Manipulate Date objects with ISO 8601-formatted durations.

Installation

$ npm install date-duration

Usage

import Duration from 'date-duration';

let duration = Duration('PT1H');
// or
let duration = Duration({P: {T: {H: 1}}});

Note: keep in mind that working with time-only durations (hours, minutes, seconds) is generally a better idea.

A duration of PT36H is not the same as P1DT12H. The former will add/subtract exactly 36 hours while the latter will add 12 hours + whatever 1 day means. e.g. if jumping across DST (Daylight saving time) and the difference is 1 hour, this totals to plus/minus 35 hours or 37 hours.

This is generally what you want as long as you take into account this happens within the context of the timezone of your environment. This can become tricky when used in browsers (where you don't have control over this).

API

Duration(ISOString)

Pass a string in ISO 8601 duration format to create a duration.

duration.addTo(Date)

Add the duration to a Date object and returns the new date. Objects with a .toDate() (like Moment.js) method are converted to a regular Date object.

duration.subtractFrom(Date)

Subtract the duration from a Date object and returns the new date. Objects with a .toDate() method are converted to a regular Date object.

duration.add(Duration)

Add durations together returning a new duration which is the sum of both.

duration.multiply(number)

Multiply individual parts of duration returning a new duration as the result.

duration.toString()

Convert a duration to a string in ISO 8601 format.

Keywords

FAQs

Last updated on 22 Sep 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc