获取昨日日期

为伊消得人憔悴 衣带渐宽终不悔

 

记录下今天用到的知识点,用于加强巩固以及后面查阅。

#!/usr/bin/python
#coding:utf-8

import datetime

def getYesterday():
    today = datetime.date.today()
    oneday = datetime.timedelta(days=1)
    yesterday = today-oneday
    return yesterday

yesterday = getYesterday()
"""
2019-03-24
<type 'datetime.date'>
"""
print(yesterday)
print(type(yesterday))
# 将datetime.date转为str
yesterday = datetime.datetime.strftime(yesterday, "%Y-%m-%d")
"""
2019-03-24
<type 'str'>
"""
print(yesterday)
print(type(yesterday))
# 将str转为datetime.date
yesterday=datetime.datetime.strptime(yesterday, "%Y-%m-%d")
"""
2019-03-24 00:00:00
<type 'datetime.datetime'>
"""
print(yesterday)
print(type(yesterday))

 

代码地址:github

posted @ 2019-03-25 19:53  飞鸟的梦想  阅读(192)  评论(0编辑  收藏  举报