Python时间模块的使用

📖 前言
个人博客:https://www.power-blog.cn

优快云:https://blog.youkuaiyun.com/powerbiubiu

👋 简介

Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能,以下介绍下time和datetime常用的方法。

💡 正文

1 time模块

import time
1.1 time.time()

获取当前时间戳

time.time()

# 1701929654.1553001
1.2 time.localtime(seconds=None)

获取struct_time对象,空参时结果为当前时间,入参为时间戳

time.localtime(1701928728.1920094)

# time.struct_time(tm_year=2023, tm_mon=12, tm_mday=7, tm_hour=13, tm_min=58, tm_sec=48, tm_wday=3, tm_yday=341, tm_isdst=0)
struct_time = time.localtime(1701928728.1920094)
print(struct_time.tm_year)  # 2023,获取年份
print(struct_time.tm_mon)	# 12,获取月份
print(struct_time.tm_mday)	# 7,获取日期
print(struct_time.tm_hour)	# 13,获取小时
print(struct_time.tm_min)	# 58,获取分钟
print(struct_time.tm_sec)	# 48,获取秒(0~61,61是闰秒)
print(struct_time.tm_wday)	# 3,获取当周第几天(0~6)
print(struct_time.tm_yday)	# 341,获取当年第几天(1~366)
print(struct_time.tm_isdst)	# 0,是否为夏令时(1:夏令时,0:不是夏令时,-1:未知)
1.3 time.gmtime(seconds=None)

获取UTC时区(0时区)struct_time对象,空参时结果为当前时间,入参为时间戳

time.gmtime(1701928728.1920094)

# time.struct_time(tm_year=2023, tm_mon=12, tm_mday=7, tm_hour=5, tm_min=58, tm_sec=48, tm_wday=3, tm_yday=341, tm_isdst=0)
1.4 time.strftime(format, p_tuple=None)

格式化时间,参数1格式化方式,参数2为时间元组(传默认参数为获取当前时间)

time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1701928728.1920094))
time.strftime("%Y-%m-%d %H:%M:%S", (2023, 12, 7, 13, 58, 48, 3, 341, 0))

# 2023-12-07 13:58:48
1.5 time.strptime(string, format)

根据时间和格式化方式获取struct_time对象

time.strptime("2022-12-12 12:12:12", "%Y-%m-%d %H:%M:%S")

# time.struct_time(tm_year=2023, tm_mon=12, tm_mday=7, tm_hour=13, tm_min=58, tm_sec=48, tm_wday=3, tm_yday=341, tm_isdst=-1)
1.6 time.asctime(p_tuple=None)和time.ctime(seconds=None)

获取标准时间,一个传入时间元组,一个传入时间戳

time.asctime((2023, 12, 7, 13, 58, 48, 3, 341, 0))
time.ctime(1701928728.1920094)

# Thu Dec  7 13:58:48 2023
1.7 time.mktime(p_tuple=None)

将日期转换成时间戳

time.mktime((2023, 12, 7, 13, 58, 48, 3, 341, 0))
time.mktime(time.localtime(1701928728.1920094))

# 1701928728.0
1.8 time.sleep(seconds)

阻塞程序执行,参数为秒

2 datetime模块

datetime模块分为五大类:

  • datetime.date:表示日期的类,主要用于处理年、月、日
  • datetime.time:表示时间的类,主要用于处理时、分、秒
  • datetime.datetime:表示日期时间的类,date类和time类的综合使用,可以处理年、月、日、时、分、秒
  • datetime.timedelta:表示时间间隔,即两个时间点的间隔,主要用于做时间加减的
  • datetime.tzinfo:时区的相关信息
2.1 date类
from datetime import date, time, datetime, timedelta, tzinfo

day = date.today()	
print(day)			
# 2023-12-07,获取今天日期
print(day.year)		
# 2023
print(day.month)	
# 12
print(day.day)		
# 7
print(day.replace(2022, 11))	
# 2022-11-07, 替换年月
print(date(2023, 11, 6))		
# 2023-11-06
2.2 time类
from datetime import date, time, datetime, timedelta, tzinfo

print(time.fromisoformat('04:23:01'))	
# 04:23:01
dt = time(hour=12, minute=34, second=56, microsecond=123456)
print(dt.isoformat(timespec='minutes'))	
# 12:34
print(dt.isoformat(timespec='microseconds'))	
# 12:34:56.123456
2.3 datetime类
from datetime import datetime

print(datetime.now())
print(datetime.today())
# 2023-12-07 15:38:18.536532,获取当前时间
print(datetime.utcnow())
# 2023-12-07 07:38:18.536531,获取UTC0的时间
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# 2023-12-07 15:38:18,格式化
print(datetime.date(datetime.now()))
# 2023-12-07,获取当前日期,年月日
print(datetime.time(datetime.now()))
# 15:38:18.536531,获取当前时间,时分秒
print(datetime.fromisoformat('2023-12-07'))
# 2023-12-07 00:00:00,格式化成年月日时分秒
print(datetime.fromisoformat('2023-12-07T15:05:23'))
# 2023-12-07 15:05:23,格式化成年月日时分秒
print(datetime.fromtimestamp(1701934496.7117853))
# 2023-12-07 15:34:56.711785,将时间戳转换成年月日时分秒
print(datetime.utcfromtimestamp(1701934496.7117853))
# 2023-12-07 07:34:56.711785,将时间戳转换成UTC0的年月日时分秒
2.4 timedelta类
import datetime

day = datetime.date.today()
print(day)
# 2023-12-07,当前日期
print(day + datetime.timedelta(days=7))
# 2023-12-14,增加7天后日期

now = datetime.datetime.now()
print(now)
# 2023-12-07 15:48:14.868991,当前日期时间
print(now + datetime.timedelta(hours=8))
# 2023-12-07 23:48:14.868991,增加8小时
print(now + datetime.timedelta(minutes=30))
# 2023-12-07 16:18:14.868991,增加30分钟
print(now + datetime.timedelta(seconds=30))
# 2023-12-07 15:48:44.868991,增加30秒钟
print(now - datetime.timedelta(weeks=1))
# 2023-11-30 15:48:14.868991,减去一星期

🎉 欢迎我的关注公众号

微信公众号

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值