Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the proper way to express a length of time in TypeScript?

I am currently creating an interface for a personal project and need to include a field that contains a length of time that a task took to complete. What is the proper data type/format for expressing a length of time in TypeScript?

Here is my interface

export interface Task {
    id: number, 
    name: string,
    taskDate: Date,
    taskDuration: {DURATION TYPE HERE}
}

I’ve seen several ways online to calculate a duration from 2 dates, so I could just use a taskStart and taskEnd and then calculate from there, but surely there is a way to simply enter a duration without having to perform a calculation.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

ISO 8601 specifies a format for durations, which is pretty much industry standard.

Some examples:

P1D - one day
PT5H - 5 hours
P2YT2S - 2 years, 2 seconds

https://en.wikipedia.org/wiki/ISO_8601#Durations

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading