1

I'm working on API that will pass some Python datetime.timedelta to JavaScript. What duration transport format should I choose to simple parsing and representing it on the JS client? I don't want to use any 3rd party libraries on backend like 'babel' and don't want to force front ends to use any requirements like 'moment'.

1 Answers1

0

Simply use float or long int to representing seconds or nanoseconds UTC time in all db, backend and transport. Only convert it to local time with timezone when it needs to be displayed.

timedelta

90000 seconds = 1 day 1 hour

db

90000.0 #float

python

timedelta = 90000.0

Json

{
"timedelta":90000.0
}

Javascript format

alert(timedelta + "seconds")
alert(Math.floor(timedalta/3600) + "hours")
Jon Stark
  • 91
  • 1
  • 8
  • But it's hard to parse same format in human readable. I don't good in javascript so I thought it may have some good built-in tools, but it don't as I can see. I decided to use [iso 8601](https://stackoverflow.com/questions/27851832/how-do-i-parse-an-iso-8601-formatted-duration-using-moment-js) duration format and probably dublicate it seconds as you show. Thanks all. – Arthur Mullakhmetov May 22 '16 at 18:24
  • your json example is invalid: json requires string (with quotes) keys—don't confuse it with a javascript object literal – jfs May 23 '16 at 15:02
  • the quotes are required only for json **keys**. You could drop the quote around the float **value** (otherwise it is not a number—it is a string). – jfs May 30 '16 at 09:19