python3自学之路-笔记14_时间日历的常用操作
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : 时间日历处理.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date : 2019/3/15
#时间模块 有 time,calendar,datetime 模块
import time
'''
1.time.time() 获取当前时间戳
2.time.localtime([seconds]) 获取时间元组seconds是时间戳,默认为当前时间戳 时间元组里面的可以用[下标]读取元素 ,也可以用.对象来看
3.time.ctime(seconds) 将时间戳转化为格式化可读时间
4.time.asctime(P_tuple) 将时间元组转为格式化可读时间
5.time.strftime(格式化字符串,时间元组) 字符串自定义格式化日期
6.time.strptime(日期字符串,格式化字符串) 自定义的时间转回时间元组
7.time.mktime(时间元组) 将时间元组转为时间戳
8.time.sleep(xx) 休眠(延迟)几秒
9.time.clock() time.process_time() ime.perf_counter() 获取cpu的时间点
re=time.time() #获取从1970 年后的时间戳
years=re/(24*60*60*365)+1970
print(years)
print(time.asctime(time.localtime()))
re1=time.localtime()
print(re1.tm_year)
res=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
res=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) 前面的Y表示4为的年 y为2位的年
print(res)
res=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
res=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) #前面的Y表示4为的年 y为2位的年
res1=time.strptime(res,'%Y-%m-%d %H:%M:%S')
res2=time.mktime(res1)
print(res1)
print(res)
print(res2)
start=time.perf_counter()
for i in range(1000):
print(i)
end=time.perf_counter()
print(end-start)
time.process_time()
------------------------常用时间格式符-------------------------------
%y 两位数年份表示
%Y 四位数年份表示
%m 月份
%d 月中的一天
%H 24小时制时间
%I 12小时制小时
%M 分钟数
%S 秒数
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化月份名称
%B 本地完整月份名称
calenda 日历模块-------------------------------------------
calenda.month(年份,月份)
import calendar
a=calendar.month(2017,2)
print(a)
datatime类
1.datetime.datetime.now() 当前时间
2.datetime.timedelta(xxx=) 时间间隔
print(datetime.datetime.now())
print(datetime.datetime.today())
datetime.now() 是一个datetime.datetime类型 他里面有各种对象年月日等等
import datetime
t=datetime.datetime.now()
print(datetime.datetime.now())
print(datetime.datetime.today())
print(t.month)
import datetime
t=datetime.datetime.now()
res=t+datetime.timedelta(days=7)
print(res)
first=datetime.datetime(2019,3,15,12,00,00)
second=datetime.datetime(2019,3,22,12,00,00)
delta=second-first
print(delta.total_seconds())
'''