time模块
一、time
python中时间日期格式化符号:
- %y 两位数的年份表示(00-99)
- %Y 四位数的年份表示(000-9999)
- %m 月份(01-12)
- %d 月内中的一天(0-31)
- %H 24小时制小时数(0-23)
- %I 12小时制小时数(01-12)
- %M 分钟数(00-59)
- %S 秒(00-59)
- %a 本地简化星期名称
- %A 本地完整星期名称
- %b 本地简化的月份名称
- %B 本地完整的月份名称
- %c 本地相应的日期表示和时间表示
- %j 年内的一天(001-366)
- %p 本地A.M.或P.M.的等价符
- %U 一年中的星期数(00-53)星期天为星期的开始
- %w 星期(0-6),星期天为星期的开始
- %W 一年中的星期数(00-53)星期一为星期的开始
- %x 本地相应的日期表示
- %X 本地相应的时间表示
- %Z 当前时区的名称
- %% %号本身
# -*- coding:utf-8 -*- import time # 时间戳(timestamp):1584536385.2879126 print(time.time()) # 格式化的时间(format_string time):2020-03-18 20:59:45 print(time.strftime('%Y-%m-%d %X')) # 结构化的时间(struct_time):time.struct_time(tm_year=2020, tm_mon=3, tm_mday=18, tm_hour=20, tm_min=59, tm_sec=45, tm_wday=2, tm_yday=78, tm_isdst=0) # 把时间戳转换为当前时区的结构化的时间(struct_time),默认是当前时间 print(time.localtime()) # 时间戳(timestamp)----->结构化时间(struct_time) print(time.localtime())#转换为当前时区,,默认是当前时间 print(time.localtime(1584536272.0676434)) print(time.gmtime())#转换为UTC时区 # 结构化时间(struct_time)----->时间戳(timestamp) print(time.mktime(time.localtime())) # 结构化时间(struct_time)----->格式化的字符串时间(format_string time) print(time.strftime('%Y-%m-%d %X', time.localtime())) print(time.strftime('%Y-%m-%d %X', time.gmtime())) # 格式化的字符串时间(format_string time)----->结构化时间(struct_time) print(time.strptime('2021-01-28 08:57:22', '%Y-%m-%d %X')) # 结构化时间(struct_time)------> Wed Mar 18 21:10:29 2020 print(time.asctime()) print(time.asctime(time.localtime())) # 时间戳(timestamp)-------> Wed Mar 18 21:10:29 2020 print(time.ctime()) print(time.ctime(time.time())) # def __init__(self, form): # self.id = None # self.title = form.get('title', '') # self.content = form.get('content', '') # self.author = form.get('author', '') # self.create_time = int(time.time()) # # # def ct(self): # format = '%H:%M:%S' # value = time.localtime(self.create_time)##得到struct_time结构化时间 # dt = time.strftime(format, value)#转为格式化的时间 # return dt
二、datetime
# datatime模块相当于重新封装了time模块,提供更多接口,提供的类有:date,time,datetime,timedelta,tzinfo # datetime模块用于是date和time模块的合集 # 1.datetime.date:表示日期的类 from datetime import date print(date.today()) #2021-02-03 print(date.today().year) print(date.today().month) print(date.today().day) print(date.today().weekday()) print(date.fromtimestamp(time.time())) #根据给定的时间戮,返回一个date对象 2021-02-03 # 2.datetime.time:表示时间的类 #time类由hour小时、minute分钟、second秒、microsecond毫秒和tzinfo五部分组成 from datetime import time dt = time.fromisoformat('04:23:01') #New in version 3.7. dt = time(hour=12, minute=34, second=56, microsecond=0) print(dt) #12:34:56 # 3.datetime.datetime:表示日期时间的类 from datetime import datetime # 返回一个表示当前本地时间的datetime对象 print(datetime.today())#2020-04-12 16:37:43.092388 # 返回一个表示当前本地时间的datetime对象 print(datetime.now())#2020-04-12 16:37:43.092388 # 返回一个当前utc(格林威治时间)时间的datetime对象 print(datetime.utcnow())#2020-04-12 08:37:43.092388 # strptime(string, format),根据返回一个格式化的datetime对象 print(datetime.strftime(datetime.now(),'%Y-%m %H-%M'))#2020-04 16-37 print(datetime.strftime(datetime.utcnow(),'%Y-%m-%d'))#2020-04-12 dt = datetime.now()# 返回一个表示当前本地时间的datetime对象 print('datetime.now',datetime.now())#2020-04-12 16:34:06.829745 print('dt.date()', dt.date())#返回datetime对象的日期部分:2020-04-12 print('dt.time()', dt.time())#返回datetime对象的时间部分:16:34:06.829745 print('dt.year', dt.year)#dt.year 2020 print('dt.month',dt.month)#dt.month 4 print('dt.day', dt.day)#dt.day 12
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】