Python将字符串(从int64)转换为日期时间

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

Python convert string (from int64) to datetime

问题

You can convert the lastLogon value from LDAP computer logon timestamp to datetime in Python using the following code:

import datetime

ldap_timestamp = 133322323232233542
# Convert LDAP timestamp to datetime
timestamp_datetime = datetime.datetime(1601, 1, 1) + datetime.timedelta(microseconds=ldap_timestamp / 10)
print(timestamp_datetime)

这是将LDAP计算机登录时间戳转换为Python中的日期时间的代码。

英文:

Got this value from LDAP computer logon timestamp:
lastLogon: 133322323232233542

How to convert it to datetime in python ? (it looks like it's string for int64 representation ?)

Thanks,

答案1

得分: 1

以下是翻译好的代码部分:

似乎是字符串格式的Unix时间戳。为了在Python中将其转换为`datetime`对象,您可以使用`datetime`模块。

import datetime

timestamp = int("133322323232233542")
datetime_obj = datetime.datetime.fromtimestamp(timestamp / 10000000 - 11644473600)

print(datetime_obj)
英文:

It appears to be a Unix timestamp in a string format. In order to convert it to a datetime object in Python, you can use the datetime module.

import datetime

timestamp = int("133322323232233542")
datetime_obj = datetime.datetime.fromtimestamp(timestamp / 10000000 
- 11644473600)

print(datetime_obj)

huangapple
  • 本文由 发表于 2023年6月26日 14:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554119.html
匿名

发表评论

匿名网友

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

确定