博客
关于我
python-时间模块库
阅读量:503 次
发布时间:2019-03-07

本文共 2868 字,大约阅读时间需要 9 分钟。

目录


time库

import timeprint(time.time())  # 获取当前时间戳print(time.ctime())  # 获取当前时间的并以字符串返回print(time.asctime())  # 同ctime()方法a  = time.gmtime()  # 获取当前格林威治时间,表示计算机可处理的时间输出,可添加时间戳参数b = time.localtime()  # 获取本机时间,表示计算机可处理的时间输出,可添加时间戳参数print(type(a),a)print(type(b),b)# 格式输出时间,strftime(tpl,ts)  tpl:时间格式,ts:计算机内部时间formats = '%Y-%m-%d  %H:%M:%S'print(time.strftime(formats,a))print(time.strftime(formats,b))# 使用时钟信号进行计时,精确到毫秒,返回cpu级别的精确时间值,通产用来做程序计时应用,比time.time()更加精确time1 = time.perf_counter()time2 = time.perf_counter()print(time2-time1)ti1 = time.time()ti2 = time.time()print(ti2-ti1)# 格式化时间与时间戳的转换print(time.mktime(b))  # struct_time时间转为时间戳print(time.strptime('2020-10-29  17:55:03',"%Y-%m-%d %H:%M:%S"))  # 字符串时间转为struct_time时间print(time.localtime(time.time()))  # 时间戳转为struct_time时间# 获取系统重启后到当前的时长print(time.monotonic())

 datetime库

datetime库是基于time库的封装库,time库更适用于时间的提取和简单操作,datetime库是更强大的时间和日期库。

datetime中四个大类

date日期对象,常用属性:year\month\day

time时间对象,常用属性:hour \ minute \ second \ millisecond

datetime日期时间对象

timedelta时间间隔

import datetimeformats = '%Y-%m-%d  %H:%M:%S'# time类,时、分、秒、毫秒print(datetime.time(20,5,38,9898))# date类,年、月、日print(datetime.date(2020,5,20))# datetime类,年、月、日、时、分、秒、毫秒print(datetime.datetime(2020,8,25,20,45,39,888888))# datetime的datetime类方法print(datetime.datetime.now())  # 获取当前时间print(datetime.datetime.now().timestamp())  # 日期时间转化为时间戳:时间日期对象.timestamp()print(datetime.datetime.fromtimestamp(1634231316.756675))  # 时间戳转为日期时间:datetime.fromtimestamp(时间戳)print(datetime.datetime.now().strftime(formats))  # 日期时间对象转字符串:时间日期对象.strftime(format)print(datetime.datetime.strptime('2020-10-29  21:49:40',formats))  # 字符串转日期时间对象:datetime.strptime(data_str,format)# timedelta类,用来做时间运算。通过datetime.timedelta(间隔时间参数)和获的时间进行时间运算# 参数:days=0,econds=0, microseconds=0 milliseconds=0,minutes=0, hours=0, weeks=0print(datetime.datetime.now() + datetime.timedelta(days=20))  # 计算20天后的时间

 计算已生存时间

def count_time():    bri_year = input('请输入你的出生年份:')    bri_mon = input('请输入你的出生年份:')    bri_day = input('请输入你的出生年份:')    bri_time = datetime.datetime.strptime('%s-%s-%s'%(bri_year,bri_mon,bri_day),'%Y-%m-%d')    nowtime = datetime.datetime.now()    count = nowtime - bri_time    print('你活了%s'%count)    count_time()

天干地支纪年

import timefrom datetime import datetimefrom zhdate import ZhDatedef year():        TG = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸']    DZ = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥']    yeartime = int(time.localtime().tm_year)    monthtime = int(time.localtime().tm_mon)    daytime = int(time.localtime().tm_mday)    date_lunar = ZhDate.from_datetime(datetime(yeartime, monthtime, daytime))    yeay_lunar = int(str(date_lunar)[2:6])    TG_idx = (yeay_lunar - 3) % 10    DZ_idx = (yeay_lunar - 3) % 12    output = f'今天是公元{yeartime}年{monthtime}月{daytime}日,({TG[TG_idx-1]}{DZ[DZ_idx-1]}年){str(date_lunar)}'    print(output)    year()

 

转载地址:http://vndjz.baihongyu.com/

你可能感兴趣的文章
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>