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

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

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:

  1. import datetime
  2. ldap_timestamp = 133322323232233542
  3. # Convert LDAP timestamp to datetime
  4. timestamp_datetime = datetime.datetime(1601, 1, 1) + datetime.timedelta(microseconds=ldap_timestamp / 10)
  5. 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

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

  1. 似乎是字符串格式的Unix时间戳。为了在Python中将其转换为`datetime`对象,您可以使用`datetime`模块。
  2. import datetime
  3. timestamp = int("133322323232233542")
  4. datetime_obj = datetime.datetime.fromtimestamp(timestamp / 10000000 - 11644473600)
  5. 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.

  1. import datetime
  2. timestamp = int("133322323232233542")
  3. datetime_obj = datetime.datetime.fromtimestamp(timestamp / 10000000
  4. - 11644473600)
  5. 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:

确定