前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 时间模块备忘

python 时间模块备忘

作者头像
py3study
发布2020-01-13 12:37:34
2870
发布2020-01-13 12:37:34
举报
文章被收录于专栏:python3

  由于要用到时间模块,为了下次不用去翻文档,这里也简单记录一下:

直接一个脚本看输出:

代码语言:javascript
复制
import time
print time.time()
print time.localtime(time.time())
print time.strftime('%Y-%m-%d', time.localtime())
print time.strftime('%y-%m-%d', time.localtime())
print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print time.strftime('%Y-%m-%d %I:%M:%S', time.localtime())
print time.strftime('%Y-%m-%d %H:%M:%S --%A--%c', time.localtime())
print time.strftime('%Y-%m-%d %H:%M:%S --%x--%X', time.localtime())

查看结果:

代码语言:javascript
复制
[root@www python]# python time1.py 
1425623399.84
time.struct_time(tm_year=2015, tm_mon=3, tm_mday=6, tm_hour=14, tm_min=29, tm_sec=59, tm_wday=4, tm_yday=65, tm_isdst=0)
2015-03-06
15-03-06
2015-03-06 14:29:59
2015-03-06 02:29:59
2015-03-06 14:29:59 --Friday--Fri Mar  6 14:29:59 2015
2015-03-06 14:29:59 --03/06/15--14:29:59


datetime模块定义了下面这几个类:
datetime.date:表示日期的类。常用的属性有year, month, day;
datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;
datetime.datetime:表示日期时间。
datetime.timedelta:表示时间间隔,即两个时间点之间的长度。


>>> from datetime import *
>>> print datetime.today()
2015-03-06 14:43:46.870936
>>> print datetime.now()  
2015-03-06 14:43:51.313098

需求:查看100天前是几月几号:

代码语言:javascript
复制
import datetime
(datetime.datetime.now() - datetime.timedelta(days = 100)).strftime("%Y-%m-%d")
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档