1、python获取秒级时间戳与毫秒级时间戳、微秒级时间戳
import time
import datetime
import calendar
# 获取时间戳
t = time.time()
t_s = int(t) # 秒(s) second
t_ms= int(round(t * 1000)) # 毫秒(ms) millisecond
t_μs= int(round(t * 1000000)) # 微妙(μs) microsecond
# 将秒级时间戳转为日期
ts2dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(t_s))
# 将时间格式转成另一种时间格式
dt_new1 = datetime.datetime.strptime(ts2dt,'%Y-%m-%d %H:%M:%S').strftime('%Y/%m/%d %H:%M:%S')
dt_new2 = datetime.datetime.strptime(ts2dt,'%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y %H:%M')
dt_new3 = datetime.datetime.strptime(dt_new2,'%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S')
# 由字符串格式转化为日期格式的函数为: datetime.datetime.strptime(t_str,"%Y-%m-%d %H:%M:%S")
# 由日期格式转化为字符串格式的函数为: datetime.datetime.strftime(t_date,"%Y-%m-%d %H:%M:%S")
# String转时间格式
t_str = '2022-03-25 11:23:03'
str2time = datetime.datetime.strptime(t_str, "%Y-%m-%d %H:%M:%S")
# 时间格式转String
time2str = str2time.strftime("%Y-%m-%d %H:%M:%S")
# 格式转化
t_str_2 = '20220325'
str2time_2 = datetime.datetime.strptime(t_str_2, "%Y%m%d")
time2str_2 = str2time_2.strftime("%Y%m%d")
t_str_3 = '2022-03-25'
t_date =t_str_3.replace("-","")
# 转结构体时间struct_time
# 日期时间转结构体
dt2st = time.strptime('2022-03-25 11:23:03', '%Y-%m-%d %H:%M:%S')
# 时间戳转结构体
ts2st = time.localtime(1648178583)
print (f'原始时间值 = {t}')
print (f'秒级时间戳(10位) = {t_s}')
print (f'毫秒级时间戳(13位) = {t_ms}')
print (f'微秒级时间戳(16位) = {t_μs}')
print (f'-----------------------------')
print (f'秒级时间戳转为日期 = {ts2dt}')
print (f'时间格式转换1 = {dt_new1}')
print (f'时间格式转换2 = {dt_new2}')
print (f'时间格式转换3 = {dt_new3}')
print (f'-----------------------------')
print (f'str2time = {str2time} , type = {type(str2time)}')
print (f'time2str = {time2str} , type = {type(time2str)}')
print (f'str2time_2 = {str2time_2} , type = {type(str2time_2)}')
print (f'time2str_2 = {time2str_2} , type = {type(time2str_2)}')
print (f't_date = {t_date} , type = {type(t_date)}')
print (f'-----------------------------')
print (f'日期时间转结构体 = {dt2st}')
print (f'时间戳转结构体 = {ts2st}')
# 返回
原始时间值 = 1648189220.4241912
秒级时间戳(10位) = 1648189220
毫秒级时间戳(13位) = 1648189220424
微秒级时间戳(16位) = 1648189220424191
-----------------------------
秒级时间戳转为日期 = 2022-03-25 14:20:20
时间格式转换1 = 2022/03/25 14:20:20
时间格式转换2 = 03/25/2022 14:20
时间格式转换3 = 2022-03-25 14:20:00
-----------------------------
str2time = 2022-03-25 11:23:03 , type = <class 'datetime.datetime'>
time2str = 2022-03-25 11:23:03 , type = <class 'str'>
str2time_2 = 2022-03-25 00:00:00 , type = <class 'datetime.datetime'>
time2str_2 = 20220325 , type = <class 'str'>
t_date = 20220325 , type = <class 'str'>
-----------------------------
日期时间转结构体 = time.struct_time(tm_year=2022, tm_mon=3, tm_mday=25, tm_hour=11, tm_min=23, tm_sec=3, tm_wday=4, tm_yday=84, tm_isdst=-1)
时间戳转结构体 = time.struct_time(tm_year=2022, tm_mon=3, tm_mday=25, tm_hour=11, tm_min=23, tm_sec=3, tm_wday=4, tm_yday=84, tm_isdst=0)
2、获取当前日期时间
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒的日期时间
dt_year = datetime.datetime.now().strftime('%Y')
dt_month = datetime.datetime.now().strftime('%m')
dt_day = datetime.datetime.now().strftime('%d')
dt_yyyymm = datetime.datetime.now().strftime('%Y%m')
dt_yyyymmdd1 = datetime.datetime.now().strftime('%Y%m%d')
dt_yyyymmdd2 = datetime.datetime.now().strftime('%Y-%m-%d')
dt_yyyymmdd3 = datetime.datetime.now().strftime('%Y/%m/%d')
print (f'当前时间 = {dt}')
print (f'当前时间带微秒 = {dt_ms}')
print (f'当前时间年份 = {dt_year}')
print (f'当前时间月份 = {dt_month}')
print (f'当前时间日期 = {dt_day}')
print (f'当前时间年月 = {dt_yyyymm}')
print (f'当前时间年月日 = {dt_yyyymmdd1}')
print (f'当前时间年月日 = {dt_yyyymmdd2}')
print (f'当前时间年月日 = {dt_yyyymmdd3}')
# 返回值
当前时间 = 2022-03-24 17:57:37
当前时间带微秒 = 2022-03-24 17:57:37.403094
当前时间年份 = 2022
当前时间月份 = 03
当前时间日期 = 24
当前时间年月 = 202203
当前时间年月日 = 20220324
当前时间年月日 = 2022-03-24
当前时间年月日 = 2022/03/24
3、获取今日零点时间
# 日期
currentDate = datetime.date.today()
yesterdayDate = datetime.date.today() + datetime.timedelta(days=-1)
daybeforeyesDate= datetime.date.today() + datetime.timedelta(days=-2)
tomorrowDate = datetime.date.today() + datetime.timedelta(days=1)
dayaftertomDate = datetime.date.today() + datetime.timedelta(days=2)
# 日期时间
currentDatetime = datetime.datetime.now()
yesterdayDatetime = currentDatetime + datetime.timedelta(days=-1)
tomorrowDatetime = currentDatetime + datetime.timedelta(days=1)
# 日期零点 (三种方法)
currentDate_0 = datetime.date.today().strftime("%Y-%m-%d %H:%M:%S")
currentTime_0 = time.strftime("%Y-%m-%d 00:00:00")
currentNow_0 = datetime.datetime.now().strftime('%Y-%m-%d 00:00:00')
print (f'今天日期 = {currentDate}')
print (f'昨天日期 = {yesterdayDate}')
print (f'前天日期 = {daybeforeyesDate}')
print (f'明天日期 = {tomorrowDate}')
print (f'后天日期 = {dayaftertomDate}')
print (f'当前时间 = {currentDatetime}')
print (f'当前时间往前24小时 = {yesterdayDatetime}')
print (f'当前时间往后24小时 = {tomorrowDatetime}')
print("datetime.date - 今日0点 = %s" % currentDate_0)
print("time - 今日0点 = %s" % currentTime_0)
print("now - 今日0点 = %s" % currentNow_0)
# 返回
今天日期 = 2022-03-24
昨天日期 = 2022-03-23
前天日期 = 2022-03-22
明天日期 = 2022-03-25
后天日期 = 2022-03-26
当前时间 = 2022-03-24 18:24:03.454874
当前时间往前24小时 = 2022-03-23 18:24:03.454874
当前时间往后24小时 = 2022-03-25 18:24:03.454874
datetime.date - 今日0点 = 2022-03-24 00:00:00
time - 今日0点 = 2022-03-24 00:00:00
now - 今日0点 = 2022-03-24 00:00:00
4、获取昨上周一,下周一,上个月初,月末日期时间等
# 获取本周第一天和本周最后一天日期
today = datetime.date.today()
weekday = today.weekday() # 今天实际周几 = today.weekday() + 1
currentWeekday1 = today + datetime.timedelta(days=-weekday)
currentWeekday7 = today + datetime.timedelta(days=-weekday + 6)
# 获取上周第一天和上周最后一天日期
lastWeekday1 = today + datetime.timedelta(days=-weekday - 7)
lastWeekday7 = today + datetime.timedelta(days=-weekday - 1)
# 获取下周第一天和下周最后一天日期
nextWeekday1 = today + datetime.timedelta(days=-weekday + 7)
nextWeekday7 = today + datetime.timedelta(days=-weekday + 13)
# 获取本月第一天和最后一天日期
now = datetime.datetime.now()
currentMonthBegin = datetime.datetime(now.year, now.month, 1)
currentMonthEnd = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1])
# 获取上个月第一天和最后一天日期
lastMonthEnd = currentMonthBegin + datetime.timedelta(days=-1)
lastMonthBegin = datetime.datetime(lastMonthEnd.year, lastMonthEnd.month, 1)
# 获取下个月第一天和最后一天日期
nextMonthBegin = currentMonthEnd + datetime.timedelta(days=1)
nextMonthEnd = datetime.datetime(nextMonthBegin.year, nextMonthBegin.month, calendar.monthrange(nextMonthBegin.year, nextMonthBegin.month)[1])
# 获取本季第一天和最后一天
month = (now.month - 1) - (now.month - 1) % 3 + 1
this_quarter_start = datetime.datetime(now.year, month, 1)
this_quarter_end = datetime.datetime(now.year, month+2, calendar.monthrange(now.year, now.month)[1])
# 获取上季第一天和最后一天
last_quarter_end = this_quarter_start + datetime.timedelta(days=-1)
last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1)
# 获取下季季第一天和最后一天
next_quarter_start = this_quarter_end + datetime.timedelta(days=1)
next_quarter_end = datetime.datetime(next_quarter_start.year, next_quarter_start.month + 2, calendar.monthrange(next_quarter_start.year, next_quarter_start.month+2)[1])
# 获取本年第一天和最后一天
this_year_start = datetime.datetime(now.year, 1, 1)
this_year_end = datetime.datetime(now.year + 1, 1, 1) + datetime.timedelta(days=-1)
# 获取去年第一天和最后一天
last_year_end = this_year_start - datetime.timedelta(days=1)
last_year_start = datetime.datetime(last_year_end.year, 1, 1)
# 获取明年第一天和最后一天
next_year_start = this_year_end + datetime.timedelta(days=1)
next_year_end = datetime.datetime(next_year_start.year + 1, 1, 1) + datetime.timedelta(days=-1)
""" 注:
calendar.monthrange(year,month)
传入两个值:一个是当前的年份,另外一个是当前的月份
写法可以是:calendar.monthrange(now.year,now.month)
返回两个整数。
第一个值为该月第一天所在的星期几,对应的数字。0 - 6==>0(星期一)到6(星期日)
第二个值为该月一共几天。
"""
print (f'今天日期 = {today}')
print (f'今日周几 = {weekday}')
print (f'本周一日期 = {currentWeekday1}')
print (f'本周日日期 = {currentWeekday7}')
print (f'上周一日期 = {lastWeekday1}')
print (f'上周日日期 = {lastWeekday7}')
print (f'下周一日期 = {nextWeekday1}')
print (f'下周日日期 = {nextWeekday7}')
print (f'-----------------------------')
print (f'本月月初日期 = {currentMonthBegin}')
print (f'本月月末日期 = {currentMonthEnd}')
print (f'上月月初日期 = {lastMonthBegin}')
print (f'上月月末日期 = {lastMonthEnd}')
print (f'下月月初日期 = {nextMonthBegin}')
print (f'下月月末日期 = {nextMonthEnd}')
print (f'------------------------------')
print (f'本季季初日期 = {this_quarter_start}')
print (f'本季季末日期 = {this_quarter_end}')
print (f'上季季初日期 = {last_quarter_start}')
print (f'上季季末日期 = {last_quarter_end}')
print (f'下季季初日期 = {next_quarter_start}')
print (f'下季季末日期 = {next_quarter_end}')
print (f'------------------------------')
print (f'本年度第一天日期 = {this_year_start}')
print (f'本年最后一天日期 = {this_year_end}')
print (f'去年度第一天日期 = {last_year_start}')
print (f'去年最后一天日期 = {last_year_end}')
print (f'明年度第一天日期 = {next_year_start}')
print (f'明年最后一天日期 = {next_year_end}')
# 返回值
今天日期 = 2022-03-25
今日周几 = 4
本周一日期 = 2022-03-21
本周日日期 = 2022-03-27
上周一日期 = 2022-03-14
上周日日期 = 2022-03-20
下周一日期 = 2022-03-28
下周日日期 = 2022-04-03
-----------------------------
本月月初日期 = 2022-03-01 00:00:00
本月月末日期 = 2022-03-31 00:00:00
上月月初日期 = 2022-02-01 00:00:00
上月月末日期 = 2022-02-28 00:00:00
下月月初日期 = 2022-04-01 00:00:00
下月月末日期 = 2022-04-30 00:00:00
------------------------------
本季季初日期 = 2022-01-01 00:00:00
本季季末日期 = 2022-03-31 00:00:00
上季季初日期 = 2021-10-01 00:00:00
上季季末日期 = 2021-12-31 00:00:00
下季季初日期 = 2022-04-01 00:00:00
下季季末日期 = 2022-06-30 00:00:00
------------------------------
本年度第一天日期 = 2022-01-01 00:00:00
本年最后一天日期 = 2022-12-31 00:00:00
去年度第一天日期 = 2021-01-01 00:00:00
去年最后一天日期 = 2021-12-31 00:00:00
明年度第一天日期 = 2023-01-01 00:00:00
明年最后一天日期 = 2023-12-31 00:00:00
5、时间计算
"""
datetime.timedelta
用法: datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
"""
now = datetime.datetime.now()
# 星期加减
week_add_dt = now + datetime.timedelta(weeks=1)
week_diff_dt = now + datetime.timedelta(weeks=-1)
# 日期加减
day_add_dt = now + datetime.timedelta(days=1)
day_diff_dt = now + datetime.timedelta(days=-1)
# 小时加减
hour_add_dt = now + datetime.timedelta(hours=1)
hour_diff_dt = now + datetime.timedelta(hours=-1)
# 分钟加监
min_add_dt = now + datetime.timedelta(minutes=1)
min_diff_dt = now + datetime.timedelta(minutes=-1)
# 两个时间相减
datetime1 = now + datetime.timedelta(hours=-24)
datetime2 = now
# 两个时间相差几天,几秒,几小时。
dt_days = (datetime2-datetime1).days
dt_seconds = (datetime2-datetime1).seconds
dt_seconds2 = (datetime2-datetime1).total_seconds()
"""注意:
.days 是以年月日为单位计算的,没有涉及到时分秒的计算。直接按照日期相差的天数计算。
.seconds 只会计算小时分钟秒 部分之间的差值。如果超过24小时或者前后时间跨天,计算结果就会和预期结果不一致。比如这里相差24小时,秒差值为0。
.total_seconds() 才是计算两个时间戳之间的实际相差秒值。比如这里的秒差值86400秒。
"""
print (f'当前时间 = {now}')
print (f'当前时间加一周 = {week_add_dt}')
print (f'当前时间减一周 = {week_diff_dt}')
print (f'当前时间加一天 = {day_add_dt}')
print (f'当前时间减一天 = {day_diff_dt}')
print (f'当前时间加一小时 = {hour_add_dt}')
print (f'当前时间减一小时 = {hour_diff_dt}')
print (f'当前时间加一分钟 = {min_add_dt}')
print (f'当前时间减一分钟 = {min_diff_dt}')
print (f'------------------------------')
print (f'两个时间相差天数 = {dt_days}')
print (f'两个时间相差秒数 = {dt_seconds}')
print (f'两个时间相差秒数2 = {dt_seconds2}')
# 返回
当前时间 = 2022-03-25 14:47:22.495714
当前时间加一周 = 2022-04-01 14:47:22.495714
当前时间减一周 = 2022-03-18 14:47:22.495714
当前时间加一天 = 2022-03-26 14:47:22.495714
当前时间减一天 = 2022-03-24 14:47:22.495714
当前时间加一小时 = 2022-03-25 15:47:22.495714
当前时间减一小时 = 2022-03-25 13:47:22.495714
当前时间加一分钟 = 2022-03-25 14:48:22.495714
当前时间减一分钟 = 2022-03-25 14:46:22.495714
------------------------------
两个时间相差天数 = 1
两个时间相差秒数 = 0
两个时间相差秒数2 = 86400.0