2

I am looking for javascript/jquery library which can provide me the UI and functionality to select ISO 8601 formatted duration.

UI should contain multiple cascaded select box where user can select Month, Hours, Days, Week... defined in ISO_8601.

Upon selection library should convert it into standard ISO_8601 duration formatted code and vice versa.

For example, "P3Y6M4DT12H30M5S" represents a duration of "three years, six months, four days, twelve hours, thirty minutes, and five seconds".

bitto
  • 101
  • 1
  • 4

2 Answers2

3

momentjs is a popular time library for js, and there is a plugin available on github called moment-interval that supports ISO 8601 durations

From the github readme

moment.duration(3, 'weeks').toISOString(); // "P3W"
moment.duration({days: 9, hours: 18}).toISOString(); // "P9DT18H"

See the moment-interval github page for more examples

ujeenator
  • 26,384
  • 2
  • 23
  • 28
chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • great. This is what I am looking for. please also suggest me for UI library which provides UI so that it will reduce my work – bitto Feb 10 '17 at 07:33
  • I have referred UI and Functionality in both in the same question. – bitto Feb 10 '17 at 10:14
2

Wrapped up a small library to help with this: https://www.npmjs.com/package/tinyduration

Example usage:

import { parse, serialize } from 'tinyduration';
 
// Basic parsing
const durationObj = parse('P1Y2M3DT4H5M6S');
assert(durationObj, {
    years: 1,
    months: 2,
    days: 3,
    hours: 4,
    minutes: 5,
    seconds: 6
});
 
// Serialization
assert(serialize(durationObj), 'P1Y2M3DT4H5M6S');

You can install it using npm install --save tinyduration or yarn add tinyduration

HTH

Melle
  • 7,639
  • 1
  • 30
  • 31