[英]how to convert datetime to numeric format in Python
我在 Python 中有以下日期时间,并希望将其转换为数字
2020-04-01 12:30:01
当我将其转换为 excel 中的数字时,它返回43922.5208449074
。 如何在 Python 中获得相同的数字?
我尝试了以下
datetime.fromisoformat('2020-04-01 12:30:01').timestamp()
但它不会返回相同的数字。 我们如何在 Python 中做到这一点
如果我理解您的问题,您可以使用它
# Importing datetime.
from datetime import datetime
# Creating a datetime object so we can test.
a = datetime.now()
# Converting a to string in the desired format (YYYYMMDD) using strftime
# and then to int.
a = int(a.strftime('%Y%m%d'))
print (a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.