如何把字符串转化成时间


当前第2页 返回上一页

(4)dateTime类型转为str类型

1

2

3

4

import datetime

dateTime_p = datetime.datetime.now()

str_p = datetime.datetime.strftime(dateTime_p,'%Y-%m-%d')

print(dateTime_p) # 2019-01-30 15:36:19.415157

(5)字符串类型str转换为date类型

1

2

3

4

5

#!/usr/bin/env python3

import datetime

str_p = '2019-01-30'

date_p = datetime.datetime.strptime(str_p,'%Y-%m-%d').date()

print(date_p,type(date_p)) # 2019-01-30 <class 'datetime.date'>

另外dateTime类型和date类型可以直接做加1减1这种操作

1

2

3

4

5

6

7

8

9

#!/usr/bin/env python3

import datetime

# today = datetime.datetime.today()

today = datetime.datetime.today().date()

yestoday = today + datetime.timedelta(days=-1)

tomorrow = today + datetime.timedelta(days=1)

print(today) # 2019-01-30

print(yestoday)# 2019-01-29

print(tomorrow)# 2019-01-31

以上就是如何把字符串转化成时间的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python怎么做gif

什么是爬虫?Python网络爬虫中概念的介绍

Python的gil是什么?Python中gil的介绍

Python如何模拟实现生产者消费者模式的代码案例

Python单例模式是什么

Python read lines() 有什么用?能用在什么地方?

mac怎么使用Python

Python学习之代理模式

Python怎么调用pi

11个Python入门的知识点

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...