Python 中的日期时间格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17594298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:37:39  来源:igfitidea点击:

Date Time Formats in Python

pythondatedatetimetimedatetime-format

提问by user1579970

What are these date-time formats? I need to convert them to the same format, to check if they are the same. These are just two coming from separate data source, so I need to find a way to make them the same format. Any ideas?

这些日期时间格式是什么?我需要将它们转换为相同的格式,以检查它们是否相同。这些只是来自不同数据源的两个,因此我需要找到一种方法使它们具有相同的格式。有任何想法吗?

2013-07-12T07:00:00Z

2013-07-12T07:00:00Z

2013-07-10T11:00:00.000Z

2013-07-10T11:00:00.000Z

Thanks in advance

提前致谢

回答by Sudipta

That extra .000is micro seconds.

额外的时间.000是微秒。

This will convert a date string of a format to datetime object.

这会将格式的日期字符串转换为日期时间对象。

import datetime
d1 = datetime.datetime.strptime("2013-07-12T07:00:00Z","%Y-%m-%dT%H:%M:%SZ")
d2 = datetime.datetime.strptime("2013-07-10T11:00:00.000Z","%Y-%m-%dT%H:%M:%S.%fZ")

Then convert them into any format depending on your requirement, by using:

然后根据您的要求将它们转换为任何格式,使用:

new_format = "%Y-%m-%d"
d1.strftime(new_format)

回答by monkut

perhaps use .isoformat()

也许使用 .isoformat()

string in ISO 8601 format, YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM]

ISO 8601 格式的字符串,YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM]

>>> import datetime
>>> datetime.datetime.utcnow().isoformat() + "Z"
'2013-07-11T22:26:51.564000Z'
>>>

Zspecifies "zulu" time or UTC.

Z指定“zulu”时间或UTC。

You can also add the timezone component by making your datetime object timezone awareby applying the appropriate tzinfoobject. With the tzinfoapplied the .isoformat()method will include the appropriate utc offset in the output:

您还可以通过应用适当的tzinfo对象使您的日期时间对象时区感知来添加时区组件。与tzinfo施加的方法将包括适当的UTC的输出偏移:.isoformat()

>>> d = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)
>>> d.isoformat()
'2019-11-11T00:52:43.349356+00:00'

You can remove the microsecondsby change the microsecondsvalue to 0:

您可以删除微秒的变化微秒重视到0

>>> no_ms = d.replace(microsecond=0)
>>> no_ms.isoformat()
'2019-11-11T00:52:43+00:00'

Also, as of python 3.7the .fromisoformat()method is available to load an iso formatted datetime string into a python datetime object:

此外,从python 3.7 开始,.fromisoformat()方法可用于将 iso 格式的日期时间字符串加载到 python 日期时间对象中:

>>> datetime.datetime.fromisoformat('2019-11-11T00:52:43+00:00')
datetime.datetime(2019, 11, 11, 0, 52, 43, tzinfo=datetime.timezone.utc)

http://www.ietf.org/rfc/rfc3339.txt

http://www.ietf.org/rfc/rfc3339.txt

回答by Johan Stiven Hernandez Osorio

you can try to trim the string

你可以尝试修剪字符串

data = "2019-10-22T00:00:00.000-05:00"
result1 = datetime.datetime.strptime(data[0:19],"%Y-%m-%dT%H:%M:%S")
result2 = datetime.datetime.strptime(data[0:23],"%Y-%m-%dT%H:%M:%S.%f")
result3 = datetime.datetime.strptime(data[0:9], "%Y-%m-%d")

回答by Hitman

* Short and best way:

*简短而最好的方法:

str(datetime.datetime.now()).replace(' ','T')
or
str(datetime.datetime.now()).replace(' ','T') + "Z"

回答by Uday Kiran

use datetime module.

使用日期时间模块。

For a variable

对于一个变量

import datetime
def convertDate(d):
     new_date = datetime.datetime.strptime(d,"%Y-%m-%dT%H:%M:%S.%fZ")
     return new_date.date()you can change the ".date()" to ".year", ".month", ".day" etc...

convertDate("2019-12-23T00:00:00.000Z")

Output: # is now a datetime object

输出:#现在是一个日期时间对象

datetime.date(2019, 12, 23)

For a DataFrame column, use apply()

对于 DataFrame 列,使用 apply()

df['new_column'] = df['date_column'].apply(convertDate)