简体   繁体   English

如何使用Python将LabVIEW十进制日期转换为字符串日期时间格式?

[英]How do I convert a LabVIEW decimal date into a string datetime format using Python?

I need to convert a decimal timestamp in a JSON file generated using LabVIEW into a string datetime so that I can POST it to an API I'm using. 我需要将使用LabVIEW生成的JSON文件中的十进制时间戳转换为字符串日期时间,以便我可以将其POST到我正在使用的API中。 For instance, one such decimal timestamp is 3640111724.4817362; 例如,一个这样的十进制时间戳是3640111724.4817362; how can I do this? 我怎样才能做到这一点?

EDIT: This article from NI describes how they format their timestamps. 编辑:NI的这篇文章描述了他们如何格式化他们的时间戳。 They're starting from a nonstandard epoch (01/01/1904 00:00:00.00 UTC), so in other words, Python's interpretation is 66 years ahead. 它们是从非标准时代(01/01/1904 00:00:00.00 UTC)开始的,换句话说,Python的解释是提前66年。

just use datetime.fromtimestamp from datetime and format It with strftime as you want: 只需使用datetime.fromtimestamp的datetime.fromtimestamp,并根据需要使用strftime格式化它:

EDIT: subtracting 66 years to match with datetime timestamp pattern 编辑:减去66年以匹配日期时间戳模式

from dateutil.relativedelta import relativedelta
from datetime import datetime

timestamp = 3640111724.4817362

date = datetime.fromtimestamp(timestamp)
date = date - relativedelta(years=66)
print(date.strftime("%m/%d/%Y, %H:%M:%S"))

Output: 输出:

05/07/2019, 22:08:44

The number of seconds between 1904-01-01 00:00:00 UTC and 1970-01-01 00:00:00 UTC is 2082844800, so you just need to adjust your LabView timestamp before creating your Python datetime object. UTC时间为1904-01-01 00:00:00 UTC和1970-01-01 00:00:00 UTC之间的秒数为2082844800,因此您只需在创建Python日期时间对象之前调整LabView时间戳。

from datetime import datetime

timestamp = 3640111724.4817362
dt = datetime.fromtimestamp(timestamp - 2082844800)
print(dt)
# 2019-05-07 22:08:44.481736

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何使用 DATETIME(python) 将日期转换为特定格式? - How do we convert a date into a particular format using DATETIME(python)? 如何将 python 日期时间转换为具有可读格式日期的字符串? - How do I turn a python datetime into a string, with readable format date? 在python中,如何使用pandas将字符串日期列转换为日期格式? - In python, how do I convert my string date column to date format using pandas? 如何将字符串日期转换为日期时间? - How do I convert string date to datetime? 如何将字符串日期转换为日期时间格式以在python中绘图 - How to convert string date to datetime format for graphing in python 将日期字符串转换为日期时间 Python 格式错误 - Convert date string to Datetime Python Format Error Python将异常日期字符串转换为日期时间格式 - Python Convert unusual date string to datetime format 如何在python中将“上周日”或“下周一”之类的时间字符串转换为日期时间格式 - How do I convert a time string like "last Sunday" or "Next Monday" into datetime format in python Python:如何在不知道格式的情况下将字符串转换为日期时间? - Python: How can I convert string to datetime without knowing the format? python:如何使用 datetime 更改日期格式 - python: how do I use datetime to change the format of date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM