Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good format for Duration in JSON?

I realise that JSON has no real date format and that using ISO 8601 is a good bet given that's what JavaScript uses. What about a duration? JavaScript has no built in format.

  1. I came across ISO 8601 Durations e.g. P3Y6M4DT12H30M5S which I haven't seen used much in the wild. It also looks very verbose and difficult to read. As far as I can see it also does not support milliseconds if you needed that.
  2. I'm using C# whose TimeSpan type outputs 1.02:03:04.0050000 for 1 day, 2 hours, 3 minutes, 4 seconds and 5 milliseconds.
  3. I could use the number of seconds or milliseconds as an integer. This is completely machine readable only and it's not obvious if you are using seconds or milliseconds without labelling the value as such.

I've hardly ever seen the first format in the wild. The second seems more intuitive to me but I'm worried that it's not as well known outside of .NET. The third format is probably the most cross platform friendly but totally not human readable.

like image 794
Muhammad Rehan Saeed Avatar asked May 22 '18 15:05

Muhammad Rehan Saeed


People also ask

What is the correct format for JSON?

Valid JSON data can be in two different formats: A collection of key-value pairs enclosed by a pair of curly braces {...} . You saw this as an example above. A collection of an ordered list of key-value pairs separated by comma (,) and enclosed by a pair of square brackets [...] .

What is the right JSON date format?

To represent dates in JavaScript, JSON uses ISO 8601 string format to encode dates as a string.

What is date time format in JSON?

JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string in ISO 8601 format. Example { "myDateTime": "2018-12-10T13:45:00.000Z" }

How do you specify date format in JSON schema?

date-time : This SHOULD be a date in ISO 8601 format of YYYY-MM- DDThh:mm:ssZ in UTC time. This is the recommended form of date/ timestamp. date : This SHOULD be a date in the format of YYYY-MM-DD. It is recommended that you use the "date-time" format instead of "date" unless you need to transfer only the date part.


1 Answers

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 as P1DT12H.

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.

like image 126
Vlad DX Avatar answered Oct 14 '22 02:10

Vlad DX