GithubHelp home page GithubHelp logo

iso-duration's Introduction

Parse & sum ISO 8601 duration to date

Example

import { duration } from 'iso-duration'

const dayBefore = duration('-P1D')

const { negative, year, month, week, day, hour, minute, second } = dayBefore
console.log({ negative, year, month, week, day, hour, minute, second })
// output parsed values: { negative: true, year: 0, month: 0, week: 0, day: -1, hour: 0, minute: 0, second: 0 }

const yesterday = dayBefore(new Date())
// today's Date less 1 day

API

duration

Higher order function duration argument must be a valid ISO 8601 duration string formated as PnYnMnDTnHnMnS or PnW (n being integer).

It returns a function to sum parsed duration to given date (accorging duration sign), decorated with parsed values.

Throws an Error when it cannot parse the value.

  • date & time durations
const inTreeDaysAndTwelveHours = duration('P3DT12H')(new Date())
  • substract & negative durations
const yesterday = duration('-P1D')(new Date())
const alsoYesterday = duration('P1D')(new Date(), { substract: true })
const stillYesterday = duration('-P1D')(new Date(), { substract: true })
  • week durations
const inTwoWeeks = duration('P2W')(new Date())
const twoWeeksAgo = duration('-P2W')(new Date())
  • strict option set as false (default behavior)
const tomorrow = duration('P1D')(new Date())

Time in the day won't be affected by daylight saving time (DST)

Time interval between the date given and returned may be affected by DST and not equal to duration

  • strict option set as true
const tomorrow = duration('P1D')(new Date(), { strict: true })

Time in the day may be affected by daylight saving time (DST)

Time interval between the date given and returned will be stricly equal to duration

How to report bugs or questions?

How to make contributions?

Please read and follow the steps in the Contributing guide.

License

Released under The MIT License.

Code of conduct

This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to [email protected].

Oodrive

If you use or like iso-duration, send kind words @oodriveofficiel.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.