简体   繁体   English

如何将日期时间转换为 Python 中的数字格式

[英]how to convert datetime to numeric format in Python

I have following datetime in Python and want to convert it to numeric我在 Python 中有以下日期时间,并希望将其转换为数字

 2020-04-01  12:30:01

When I convert this to number in excel it returns 43922.5208449074 .当我将其转换为 excel 中的数字时,它返回43922.5208449074 How to get the same number in Python?如何在 Python 中获得相同的数字?

I tried with following我尝试了以下

datetime.fromisoformat('2020-04-01 12:30:01').timestamp() 

But it does not return me the same number.但它不会返回相同的数字。 How can we do it in Python我们如何在 Python 中做到这一点

If i am understanding your problem, you can use it如果我理解您的问题,您可以使用它

# 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM