简体   繁体   中英

JavaScript converted FILETIME is one day off

I have been reading about the JavaScript 'off-by-one' for dates, but I cannot figure out how to implement it in this situation. I'm converting some values from an Internet Explorer cookie into a Microsoft FILETIME, but it's always ahead by one day, the code is:

function ConvertToFiletime(high, low) {
    var seconds = 1e-7 * (high * Math.pow(2, 32) + low) - 11644473600;
    var date = new Date.UTC(1970,1,1);
    date.setSeconds(date.getSeconds() + seconds);
    return date;
}

This does the conversion perfectly well, but I can't work out the best way to deal with the extra day, which I believe is due to a lack of a timezone. Do I need to subtract a day's worth of seconds? Or is there a better way.

Your error is in this line:

var date = new Date.UTC(1970,1,1);

Date.UTC(1970, 1, 1) is actually February 1st because month indexing starts at 0. You most likely want Date.UTC(1970, 0, 1) which is January 1st.

Also, new Date.UTC() throws an error for me on the latest version of Chrome and IE 11. To properly create a date object use:

new Date(Date.UTC(1970, 0, 1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM