Python将字符串转换为日期时间

In this tutorial, we’ll see how to convert string to datetime in python.

在本教程中,我们将看到如何在python中将字符串转换为datetime。

When we fetch the value from a textbox while working with GUI in python, by default the value have string datatype.

当我们在python中使用GUI时从文本框中获取值时,默认情况下,该值具有字符串数据类型。

So here are some ways to convert a string into DateTime.

因此,这里有一些将字符串转换为DateTime的方法。

Python将字符串转换为日期时间 (Python Convert String to Datetime)

1.使用日期时间库 (1. Using datetime library)

Lets say our string is ’06-02-2018′

可以说我们的字符串是'06 -02-2018'

from datetime import datetime
 
date_string = '06-02-2018'
date_object = datetime.strptime(date_string, '%d-%m-%Y')
print(date_object)

Output:

输出:

2018-02-06 00:00:00

2018-02-06 00:00:00

Where 00:00:00 represents time. In above program, directives

其中00:00:00代表时间。 在以上程序中,指令

%d = day in 2 digit format

%d = 2位格式的日期

%m = month in 2 digit format

%m = 2位数格式的月份

%Y = year in 4 digit format

%Y = 4位数字格式的年份

On other hand if our string is like this  ‘Sep 13 2012 11:00PM’ then we can convert it using this way.

另一方面,如果我们的字符串是这样的'Sep 13 2012 11:00 PM',那么我们可以使用这种方式进行转换。

from datetime import datetime
 
date_string = 'Feb 02 2012 08:00PM'
date_object = datetime.strptime(date_string, '%b %d %Y %I:%M%p')
print(date_object)

Output:

输出:

2012-02-02 20:00:00

2012-02-02 20:00:00

where directives

where指令

%b = short form  of month name

%b =月份名称的缩写

%I = hour in short format (0-12)

%I =小时(短格式(0-12))

%M = Minutes in 2 digit format (0-60)

%M = 2位格式的分钟(0-60)

%p = AM or PM

%p = AM或PM

There are many others directives that can be used to convert different types of strings into date. To see more detail on availble directives go to https://docs.python.org/2/library/datetime.html

还有许多其他指令可用于将不同类型的字符串转换为日期。 要查看有关可用指令的更多详细信息,请访问https://docs.python.org/2/library/datetime.html

2.使用外部库dateutil –解析器 (2. Using external library dateutil – parser)

To use this library first we have to install it. To install it, open Terminal or Command prompt and type

要首先使用此库,我们必须先安装它。 要安装它,请打开终端或命令提示符,然后键入

‘pip install python-dateutil’

'pip安装python-dateutil'

And press enter key. Now we can use this library.

然后按回车键。 现在我们可以使用该库了。

from dateutil import parser
 
date_string = 'feb 06 2018 08:00PM'
date_object = parser.parse(date_string)
print(date_object)

Output:

输出:

2018-02-06 20:00:00

2018-02-06 20:00:00

Here we don’t need to write directives. For more details about dateutil library please got to http://labix.org/python-dateutil

在这里,我们不需要编写指令。 有关dateutil库的更多详细信息,请访问http://labix.org/python-dateutil

3.使用外部库时间字符串 (3. Using external library timestring)

Again to use this library we have to install it. To install it open your terminal or command prompt and type

再次使用该库,我们必须安装它。 要安装它,请打开终端或命令提示符,然后键入

‘pip install timestring’

'pip安装时间字符串'

After installation is done, now we can use this library in our program.

安装完成后,现在我们可以在程序中使用该库了。

import timestring
 
date_string = 'feb 06 2018 08:00PM'
date_object = timestring.Date(date_string)
print(date_object)

Output

输出量

2018-02-06 20:00:00

2018-02-06 20:00:00

Again here we don’t need to write directives. for more detail on timestring library go to https://github.com/stevepeak/timestring

同样,在这里我们不需要编写指令。 有关时间字符串库的更多详细信息,请访问https://github.com/stevepeak/timestring

4.使用外部库dateparser (4. Using external library dateparser)

To install dateparser, open terminal or command prompt and enter following command.

要安装dateparser,请打开终端或命令提示符,然后输入以下命令。

‘pip install dateparser’

'pip install dateparser'

Now we can use it.

现在我们可以使用它了。

import dateparser
date_string = '02/06/2018'
date_object = dateparser.parse(date_string)
print(date_object)

Output

输出量

2018-02-06 00:00:00

2018-02-06 00:00:00

And if our date string is ‘Fri, 06 feb 2018 08:55:00’ then the program will be same as above, just replace the date_string.

如果我们的日期字符串是“星期五,2018年2月6日08:55:00”,则程序将与上面相同,只需替换date_string。

You can ask your queries related to python convert string to datetime.

您可以询问与python将字符串转换为datetime有关的查询。

翻译自: https://www.thecrazyprogrammer.com/2018/04/python-convert-string-to-datetime.html

  • 7
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值