python中的日期时间strptime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44596077/
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-20 00:11:53  来源:igfitidea点击:

Datetime strptime in python

pythondatetime

提问by VorX

I've tried the following code :

我试过以下代码:

import datetime
d = datetime.datetime.strptime("01/27/2012", "%m/%d/%Y")
print(d)

and the output is :

输出是:

2012-01-27 00:00:00

I'am using Linux Mint :

我正在使用 Linux Mint:

test@testsrv ~/pythonvault $ date
Fri Jun 16 21:40:57 EEST 2017

So,the question is why the output of python code returns a date in "%Y/%m/%d" ( 2012-01-27 )instead of "%m/%d/%Y"format ?

所以,问题是为什么 python 代码的输出返回日期"%Y/%m/%d" ( 2012-01-27 )而不是"%m/%d/%Y"格式?

Please note that I'am using python 2.7

请注意,我使用的是 python 2.7

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Ravi

You need to make sure you provide input accordingly

您需要确保相应地提供输入

datetime.strptime(date_string,date_string_format).strftime(convert_to_date_string_format)

To print the date in specified format you need to provide format as below.

要以指定格式打印日期,您需要提供如下格式。

import datetime
d =datetime.datetime.strptime("01/27/2012","%m/%d/%Y").strftime('%m/%d/%Y')
print d

Output:

输出:

01/27/2012

>>Demo<<

>>演示<<

回答by taras

datetime.strptime(date_string, format)function returns a datetimeobject corresponding to date_string, parsed according to format. When you print datetimeobject, it is formatted as a string in ISO 8601 format, YYYY-MM-DDTHH:MM:SS

datetime.strptime(date_string, format)函数返回一个datetime对应于的对象date_string,根据 解析format。当您打印datetime对象时,它被格式化为 ISO 8601 格式的字符串,YYYY-MM-DDTHH:MM:SS

References:

参考:

回答by Spencer D

As astutely noted in the comments, you are parsing to a datetime object using the format you specified.

正如评论中敏锐地指出的那样,您正在使用您指定的格式解析日期时间对象。

strptime(...)is String Parse Time. You have specified the format for how the string should be interpreted to initialize a Datetime object, but that format is only utilized for initialization. By default, when you go to print that datetime object, you are getting the representation of str(DatetimeObjectInstance)(in your case, str(d)).

strptime(...)海峡荷兰国际集团P屁股时间。您已经指定了如何解释字符串以初始化 Datetime 对象的格式,但该格式仅用于初始化。默认情况下,当您打印该日期时间对象时,您将获得str(DatetimeObjectInstance)(在您的情况下为str(d)) 的表示。

If you want a different format, you should use String Format Time(strftime(...))

如果你想有一个不同的格式,你应该使用海峡ING ˚FORMAT时间strftime(...)

回答by Mahmudul Hasan

import datetime

str_time= "2018-06-03 08:00:00"
date_date = datetime.datetime.strptime(str_time, "%Y-%m-%d %H:%M:%S")
print date_date

回答by RedKoder

There is a difference between datetime.strp[arse]time()and datetime.strf[ormat]time().

datetime.strp[arse]time()和之间有区别datetime.strf[ormat]time()

The first one, strptime()allows you to create a date object from a string source, provided you can tell it what format to expect:

第一个strptime()允许您从字符串源创建日期对象,前提是您可以告诉它期望的格式:

strDate = "11-Apr-2019_09:15:42"
dateObj = datetime.strptime(strDate, "%d-%b-%Y_%H:%M%S")

print(dateObj)  # shows: 2019-04-11 09:15:42

The second one, strftime()allows you to export your date object to a string in the format of your choosing:

第二个,strftime()允许您以您选择的格式将日期对象导出为字符串:

dateObj = datetime(2019, 4, 11, 9, 19, 25)
strDate = dateObj.strftime("%m/%d/%y %H:%M:%S")

print(strDate)  # shows: 04/11/19 09:19:25

What you're seeing is simply the default string format of a datetime object because you didn't explicitly tell it what format to use.

您看到的只是 datetime 对象的默认字符串格式,因为您没有明确告诉它使用什么格式。

Checkout http://strftime.org/for a list of all the different string format options that are availble.

查看http://strftime.org/以获取可用的所有不同字符串格式选项的列表。