Comments (5)
I can see why that would make sense but I think it's out of scope for this project since this is aimed at consuming and not constructing iso8601 durations.
The reason being that (without much thought):
- given the example, it seems simple to just do that in your own code
- given to construct from 2 dates, codebase would grow and there are certainly lots of corner cases
- this is the first interest expressed in such a feature
from iso8601-duration.
we'll keep this issue open and see if others chime in 👍
Thanks for creating it!
from iso8601-duration.
I'm looking for this function as well. Use case is to create structured data for a video object, needed for the duration of the video. https://schema.org/VideoObject
from iso8601-duration.
This should be it I think.
export function secondsToISOString(seconds) {
let Y = Math.floor(seconds / 60 / 60 / 24 / 365);
let M = Math.floor(seconds / 60 / 60 / 24 / 30.42 % 12);
let D = Math.floor(seconds / 60 / 60 / 24 % 30.42);
let h = Math.floor(seconds / 60 / 60 % 24);
let m = Math.floor(seconds / 60 % 60);
let s = Math.floor(seconds % 60);
if (!seconds) {
return 'P0D';
}
return (
(seconds < 0 ? '-' : '') +
'P' +
(Y ? Y + 'Y' : '') +
(M ? M + 'M' : '') +
(D ? D + 'D' : '') +
(h || m || s ? 'T' : '') +
(h ? h + 'H' : '') +
(m ? m + 'M' : '') +
(s ? s + 'S' : '')
);
}
Tests:
import test from 'tape';
import { secondsToISOString } from '../src/utils.js';
test('secondsToISOString', (t) => {
t.equal(secondsToISOString(0), 'P0D');
t.equal(secondsToISOString(120), 'PT2M');
t.equal(secondsToISOString(3601), 'PT1H1S');
t.equal(secondsToISOString(1), 'PT1S');
t.equal(secondsToISOString(5410), 'PT1H30M10S');
t.equal(secondsToISOString(37226652), 'P1Y2M4DT20H44M12S');
t.equal(secondsToISOString(37313052), 'P1Y2M5DT20H44M12S');
t.end();
});
from iso8601-duration.
A better solution is to use this then:
https://www.npmjs.com/package/temporal-polyfill
new Temporal.Duration(0, 0, 0, 40); // => P40D
I've decided not to add more logic to this project since the Temporal proposal is around the corner (hopefully 🤞 )
from iso8601-duration.
Related Issues (20)
- P1111111111 is valid and shouldn't be HOT 2
- Negative offset doesn't work HOT 1
- TypeError when passing invalid string to parse HOT 2
- improve readme with examples of valid formats
- It should be easier to convert to milliseconds as durations in JS/TypeScript are all in MS. HOT 1
- Durations with significant millisecond accuracy are parsed incorrectly HOT 1
- Mixing weeks and days produces incorrect results. HOT 1
- Pattern matches P or PT without numbers HOT 3
- Better TypeScript typing for ISO8601-duration strings HOT 3
- Use of commas HOT 1
- Exporing a private type in a public function HOT 1
- Fix auto-publish step for travis
- Incorrect toSeconds value HOT 2
- Feature request: fromSeconds HOT 1
- Error while parsing fraction HOT 2
- Add toString method HOT 4
- toSeconds may return incorrect values when daylight savings time changes HOT 3
- Publish typescript typings
- Fractional Time Doesn't Seem Correct in all Cases HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iso8601-duration.