将Windows时间转换为时间戳。

huangapple go评论81阅读模式
英文:

Convert windows time to timestamp

问题

我正在使用以下代码将Windows时间转换为Unix时间戳,

def convert_windows_time(windows_time):
return datetime.datetime(1601, 1, 1) + datetime.timedelta(microseconds=windows_time / 10)


我该如何在Node.js中执行相同操作?以下方法不起作用,它导致了不同的日期:

let a = parseInt(129436810067618693)
let b = (new Date('1601-01-01').getMilliseconds()) + a / 10
console.log(new Date(b / 10000))


有任何想法是什么问题?
英文:

I'm using the following code to convert windows time to unix timestamp,

def convert_windows_time(windows_time):
	return datetime.datetime(1601, 1, 1) + datetime.timedelta(microseconds=windows_time / 10)

How can I do the same in nodejs? This does not work, it results in a different date:

let a = parseInt(129436810067618693)
let b = (new Date('1601-01-01').getMilliseconds()) + a / 10
console.log(new Date(b / 10000))

Any ideas what's wrong?

答案1

得分: 2

以下是翻译好的内容:

// 尝试以下代码:

const winepoch = new Date('1601-01-01T00:00:00.000Z').getTime();
const unxepoch = new Date('1970-01-01T00:00:00.000Z').getTime();
const difference = unxepoch - winepoch;

const a = 129436810067618693; // 以100纳秒为单位测量
console.log(new Date((a / 10000) - difference));
英文:

Try the following:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const winepoch = new Date(&#39;1601-01-01T00:00:00.000Z&#39;).getTime();
const unxepoch = new Date(&#39;1970-01-01T00:00:00.000Z&#39;).getTime();
const difference = unxepoch - winepoch;

const a = 129436810067618693; // measured in 100 ns
console.log(new Date((a / 10000) - difference));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 18:42:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定