首先导入模块:
from datetime import datetimeimport time
# 默认是今天data = {'time': '','thing': ''}def add_memo(item): """默认添加今天日期""" now = datetime.now().strftime('%Y-%m-%d %H:%M') print(now) data['time'] = now data['thing'] = item return data
add_memo("饿了")
2019-04-25 23:57{'time': '2019-04-25 23:57', 'thing': '饿了'}
python字典打印信息再提取日期内容(字符串形式)
a = data["time"]print(type(a))
从字符串转datetime,并输出年, 月, 日信息
dt = datetime.strptime(str(a), "%Y-%m-%d %H:%M")print(dt.year)print(dt.month)print(dt.day)
2019425
datetime转时间戳
time_time = time.mktime(dt.timetuple())print(time_time)
1556207820.0
时间戳转str
time.strftime('%Y-%m-%d', time.localtime(time_time))
'2019-04-25'