-2

This is what I received from an API.

{"$id":"1","currentDateTime":"2021-01-20T22:29-05:00","utcOffset":"-05:00:00","isDayLightSavingsTime":false,"dayOfTheWeek":"Wednesday","timeZoneName":"Eastern Standard Time","currentFileTime":132556553638838375,"ordinalDate":"2021-20","serviceResponse":null}`

It's in JSON format so I did this.

function setTime(data) {
let readDate = JSON.parse(JSON.stringify(data));
console.log(readDate);
let now = new Date(readDate.currentFileTime);
console.log(now);

}

I can't figure out how to convert it into a new Date()?

I assume I take the currentFileTime property of the object and I was hoping a combination of the Date methods would work.

Any help would be appreciated. Thank you.

Dan K
  • 3
  • 4
  • If that didn't work, try using the `currentDateTime` exactly as it is, a string, inside `var now = new Date(currentDateTime);` – Da Mahdi03 Jan 21 '21 at 03:53
  • that didn't work. It says invalid date. Sorry, if I wasn't clear. I want to convert it into a 24 hour clock format. So something like 12:32:30:13 – Dan K Jan 21 '21 at 03:59
  • What's the format of `currentFileTime`? – Chayim Friedman Jan 21 '21 at 15:37
  • Yea so apparently...."The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601." I had no idea. Anybody who is curious http://worldclockapi.com/ is the API – Dan K Jan 22 '21 at 09:13

1 Answers1

0

FILETIME represents time as 100-nanseconds integer since January 1, 1601 (source). The Date constructor represents time as milliseconds since the epoch (January 1, 1970) (source).

Thus, to get a JS Date out of a FILETIME, we need to divide by 10,000 and subtract the difference between January 1, 1970 and January 1, 1601, in milliseconds. The difference is 11,644,474,854,000 ms:

const windowsEpoch = new Date('January 1, 1601');
const unixEpoch = new Date('January 1, 1970');
console.log(unixEpoch - windowsEpoch);

Here's the code:

function filetimeToDate(filetime) {
  const epochsDiff = 11644474854000;
  return new Date((filetime / 10000) - epochsDiff);
}

function setTime(data) {
  let readDate = JSON.parse(JSON.stringify(data));
  let now = filetimeToDate(readDate.currentFileTime);
  console.log(now);
}
setTime({"$id":"1","currentDateTime":"2021-01-20T22:29-05:00","utcOffset":"-05:00:00","isDayLightSavingsTime":false,"dayOfTheWeek":"Wednesday","timeZoneName":"Eastern Standard Time","currentFileTime":132556553638838375,"ordinalDate":"2021-20","serviceResponse":null});

Edit: The above numbers, and calculations, are actually inaccurate, because JS uses double-precision floating point (that can store up to 2^53 precisely), but we're dealing with 64-bit integers. If the precision is important to you (up to seconds or milliseconds), you can use BigInts. However, I didn't use that because the difference isn't significant and BigInts: a) slow down the calculations, and b) are not portable as plain JS numbers yet.

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77