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 BigInt
s. However, I didn't use that because the difference isn't significant and BigInt
s: a) slow down the calculations, and b) are not portable as plain JS numbers yet.