0
{"version":0,"weights":[{"date":1528038188489,"weight":106.6},{"date":1528499328455,"weight":104.9},{"date":1528641180585,"weight":106.0},{"date":1528799825943,"weight":104.8},{"date":1531126307411,"weight":107.6},{"date":1531601049953,"weight":106.8},{"date":1531885441817,"weight":108.4},{"date":1536024680696,"weight":105.5},{"date":1539578290537,"weight":103.5},{"date":1539829328962,"weight":102.9},{"date":1540010784415,"weight":102.5},{"date":1540096742826,"weight":102.3},{"date":1540181346367,"weight":102.7},{"date":1541925087295,"weight":106.3},{"date":1542281158478,"weight":106.3},{"date":1542996044848,"weight":108.6},{"date":1543028073487,"weight":108.3},{"date":1543216891819,"weight":107.8},{"date":1543679231916,"weight":108.8},{"date":1543805932731,"weight":108.1},{"date":1543921648545,"weight":107.4},{"date":1544000057821,"weight":107.9},{"date":1544595979799,"weight":109.5},{"date":1544768651138,"weight":109.1},{"date":1544951465287,"weight":108.3},{"date":1545124493025,"weight":110.1},{"date":1545207092551,"weight":110.8},{"date":1545308566108,"weight":111.3},{"date":1545428287608,"weight":110.3},{"date":1546021373835,"weight":110.8}]}

I have a bodyweight logger app that lets me backup the data as a .json file which looks like this. I know nothing about javascript and json format. Is there a simple way to get the actual dates from these entries mentioned above?

EDIT: The time mentioned in these entries is UNIX time, I found out. No answers needed. Thanks! Although, I would love to read more about the UNIX time, in general.

AmanArora
  • 2,379
  • 6
  • 19
  • 22
  • You may want to start here: https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object – Keno Dec 28 '18 at 22:15
  • What have you tried? `new Date(1528038188489)` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Reactgular Dec 28 '18 at 22:16
  • "*UNIX time*" is jargon, you really shouldn't use it. Until recently, UNIX used time values that were seconds since 1970-01-01T00:00:00Z. Most seem to now use milliseconds, which is what your time value seems to be. ECMAScript uses milliseconds from the same epoch, so you can use `new Date(weights[0].date)`. If you don't need an answer, you should delete the question. – RobG Dec 29 '18 at 12:36

2 Answers2

0

The scope of the question is perhaps beyond the answer here. But basically, you want to require or read in this object and map over the weights array with some kind of date evaluation, eg. new Date(). Here's some contrived JavaScript to get you started. (in NodeJS you can require JSON files).

const myJSON = require('./thisJSON.json');

const myDates = myJSON.weights.map((weight) => new Date(weight.date));

// myDates
//['2018-06-03T15:03:08.489Z', '2018-06-03T15:03:08.489Z']
4m1r
  • 12,234
  • 9
  • 46
  • 58
0

using moment.js : https://momentjs.com/

x = JSON.parse('{"version":0,"weights":[{"date":1528038188489,"weight":106.6},{"date":1528499328455,"weight":104.9},{"date":1528641180585,"weight":106.0},{"date":1528799825943,"weight":104.8},{"date":1531126307411,"weight":107.6},{"date":1531601049953,"weight":106.8},{"date":1531885441817,"weight":108.4},{"date":1536024680696,"weight":105.5},{"date":1539578290537,"weight":103.5},{"date":1539829328962,"weight":102.9},{"date":1540010784415,"weight":102.5},{"date":1540096742826,"weight":102.3},{"date":1540181346367,"weight":102.7},{"date":1541925087295,"weight":106.3},{"date":1542281158478,"weight":106.3},{"date":1542996044848,"weight":108.6},{"date":1543028073487,"weight":108.3},{"date":1543216891819,"weight":107.8},{"date":1543679231916,"weight":108.8},{"date":1543805932731,"weight":108.1},{"date":1543921648545,"weight":107.4},{"date":1544000057821,"weight":107.9},{"date":1544595979799,"weight":109.5},{"date":1544768651138,"weight":109.1},{"date":1544951465287,"weight":108.3},{"date":1545124493025,"weight":110.1},{"date":1545207092551,"weight":110.8},{"date":1545308566108,"weight":111.3},{"date":1545428287608,"weight":110.3},{"date":1546021373835,"weight":110.8}]}');

y = moment(x.weights[0].date)
y.format("dddd, MMMM Do YYYY, h:mm:ss a");
// "Monday, June 4th 2018, 1:03:08 am"
David Bray
  • 566
  • 1
  • 3
  • 15
  • A good answer should not require a library that is neither tagged or mentioned in the OP. You should answer in plain js, with perhaps also a library answer if you think it useful. – RobG Dec 29 '18 at 12:33