二十三、python中的time和datetime模块
A、time模块
1. sleep():强制等待
import time
import datetime print("start to sleep.....")
time.sleep(5)
print("wake up ...........")
'''
2.time.time():当前系统的时间戳
'''
print(time.time())
---------------------------
1533021214.0050707
---------------------------
'''
3.time.ctime():当前系统时间
'''
print (time.ctime())
print(time.ctime(time.time()-86400))#昨天的此刻
---------------------------
Tue Jul 31 15:13:34 2018
Mon Jul 30 15:13:34 2018
---------------------------
'''
4.time.gmtime():将时间转化成struct_time格式
'''
print (time.gmtime())
time_obj=time.gmtime(time.time()-86400) #昨天的此刻(将时间戳转化成struct_time格式)
print (time_obj)
print (str(time_obj.tm_year)+"-"+str(time_obj.tm_mon)+"-"+str(time_obj.tm_mday))
print("%s-%s-%s %s:%s:%s" %(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday,time_obj.tm_hour,time_obj.tm_min,time_obj.tm_sec))
---------------------------
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=31, tm_hour=7, tm_min=32, tm_sec=9, tm_wday=1, tm_yday=212, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=30, tm_hour=7, tm_min=32, tm_sec=9, tm_wday=0, tm_yday=211, tm_isdst=0)
2018-7-30
2018-7-30 7:32:9
---------------------------
'''
4.1.字符串格式化(扩展)
%s:字符串
%d:整型
%f:浮点型
'''
time_obj=time.gmtime(time.time()-86400) #昨天的此刻
print("%s-%s-%s %s:%s:%s" %(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday,time_obj.tm_hour,time_obj.tm_min,time_obj.tm_sec))
---------------------------
2018-7-30 7:32:9
---------------------------
'''
5.localtime():将时间戳转化成struct_time格式,返回本地的时间
'''
print (time.localtime(time.time()-86400))
---------------------------
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=31, tm_hour=14, tm_min=51, tm_sec=20, tm_wday=1, tm_yday=212, tm_isdst=0)
---------------------------
'''
6.time.mktime(time.localtime()):将struct_time转化成时间戳格式
'''
print (time.mktime(time.localtime()))
---------------------------
1533106558.0
---------------------------
'''
7.time.strptime():将字符串格式日期转化成struct_time格式
''' print (time.strptime("2018-08-01 15:02","%Y-%m-%d %H:%M"))
---------------------------
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=15, tm_min=2, tm_sec=0, tm_wday=2, tm_yday=213, tm_isdst=-1)
---------------------------
'''
8.time.strftime():将时间对象转成指定的字符串格式
'''
print (time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
print (time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
---------------------------
2018-08-01 07:05:10
2018-08-01 15:05:10
---------------------------
'''
9.time.asctime():返回时间格式"Fri Aug 19 11:14:16 2016
'''
print(time.asctime())
---------------------------
Wed Aug 1 15:06:53 2018
---------------------------
B、datatime
'''
1.datetime.date.today():打印当天的日期,格式:2018-08-01
'''
print (datetime.date.today())
---------------------------
2018-08-01
---------------------------
'''
2.datetime.date.fromtimestamp(time.time()):将时间戳转化成日期格式
'''
print (datetime.date.fromtimestamp(time.time()))
---------------------------
2018-08-01
---------------------------
'''
3.datetime.datetime.now():返回当前时间,时分秒
datetime.datetime.now().timetuple():将当前时间转化成struct_time格式
'''
current_time=datetime.datetime.now()
print(current_time)
print(current_time.timetuple())
---------------------------
2018-08-01 15:20:18.638428
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=15, tm_min=20, tm_sec=18, tm_wday=2, tm_yday=213, tm_isdst=-1)
---------------------------
'''
4.datetime.datetime.now().replace(yyyy,mm,dd):将当前时间替换成指定时间
datetime.datetime.now().replace():指定格式为空的话返回当前日期
' '
current_time=datetime.datetime.now()
print(current_time.replace())
print(current_time.replace(2066,12,12))
---------------------------
2018-08-01 15:27:29.508072
2066-12-12 15:27:29.508072
---------------------------
'''
5.时间的加减
'''
print (datetime.datetime.now()+datetime.timedelta(days=10)) #比现在加10天
print (datetime.datetime.now()-datetime.timedelta(days=10)) #比现在减10天
print (datetime.datetime.now()+datetime.timedelta(hours=2)) #比现在加10小时
print (datetime.datetime.now()-datetime.timedelta(seconds=10)) #比现在减10秒
print (datetime.datetime.now()-datetime.timedelta(minutes=10)) #比现在减10分钟
print (datetime.datetime.now()+datetime.timedelta(weeks=1)) #比现在加一周
---------------------------
2018-08-11 15:56:19.873044
2018-07-22 15:56:19.873044
2018-08-01 17:56:19.873044
2018-08-01 15:56:09.873044
2018-08-01 15:46:19.873044
2018-08-08 15:56:19.873044
---------------------------
最新文章
- Java里能用session吗?
- EasyUI加载树控件自动展开所有目录
- jQuery Mobile的基本使用
- LESS-Middleware:Node.js 和 LESS 的完美搭配
- Java Class文件详解
- SVN常见图标的含义
- 简单验证码识别(matlab)
- javascript中&;&;和||的区别
- HDU 4865 Peter's Hobby --概率DP
- 使用hadoop multipleOutputs对输出结果进行不一样的组织
- rational rose 2003安装及破解
- Eclipse启动后一直Initializing Java Tooling (1%)
- 使用R进行相关性分析
- DW1000 用户手册中文版 附录3:双向测距(Two-Way Ranging)
- .Net C#域账号登陆验证
- 05 Docker集群/基础设施 - DevOps之路
- win10下Redis安装
- 转://oracle 11G+ASM for Linux手工建库
- Linux Shell完成Qt程序的自动部署
- luogu P4688 [Ynoi2016]掉进兔子洞 bitset 莫队