What is a good format for Duration in JSON?
Basically, duration is a number of milliseconds or ticks (if you need higher precision).
1 tick is 100 nanoseconds. There are 10,000 ticks in a millisecond.
ISO 8601: the existing standard
The best way is to use common standard that you mentioned ISO 8601. It's human and machine readable.
What is easier to read and change for you?
- 1036800000 ms
- P12D
You can use ISO 8601 duration like P1Y2M10DT2H30M
in different ways:
- Handle by moment.js in JavaScript
- Pass in JSON format as a
string
value - Handle by NodaTime in C#
- Store in DB
Keep in mind that ISO 8601 also resolves the issue when switching from or to Daylight saving time.
PT36H
is not the same asP1DT12H
.
Schema.org
There are different schemas for structured data on the Internet on schema.org. Number of them are using Duration which should be represented by ISO 8601 duration format
.