博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python将字符串转换为日期时间
阅读量:2505 次
发布时间:2019-05-11

本文共 3635 字,大约阅读时间需要 12 分钟。

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

还有许多其他指令可用于将不同类型的字符串转换为日期。 要查看有关可用指令的更多详细信息,请访问

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

在这里,我们不需要编写指令。 有关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

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

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 dateparserdate_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有关的查询。

翻译自:

转载地址:http://yoggb.baihongyu.com/

你可能感兴趣的文章
Swift - 点击箭头旋转
查看>>
git配置
查看>>
【hexo】01安装
查看>>
CI框架源码学习笔记2——Common.php
查看>>
005---书籍添加和编辑的提交数据
查看>>
使用case语句给字体改变颜色
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
JSP九大内置对象及四个作用域
查看>>
ConnectionString 属性尚未初始化
查看>>
数据结构-栈 C和C++的实现
查看>>
MySQL基本命令和常用数据库对象
查看>>
poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
查看>>
进程和线程概念及原理
查看>>
Lucene、ES好文章
查看>>
android 生命周期
查看>>
jquery--this
查看>>
MySQL 5.1参考手册
查看>>
TensorFlow安装流程(GPU加速)
查看>>
OpenStack的容器服务体验
查看>>