python 与时间处理相关的函数,处理时间的方法time、datetime、calendar,获取当前系统日期时间,时间戳与时间元组转换

import time

time.localtime() #以元组的形式获取当前系统的日期和时间time.struct_time(tm_year=2020, tm_mon=12, tm_mday=27, tm_hour=20, tm_min=9, tm_sec=4, tm_wday=6, tm_yday=362, tm_isdst=0)
#tm_wday=6代表周日,0代表星期一
#tm_yday=362代表今年的第362天
#tm_isdst=0 #代表是否为夏令时,1表示当前为夏令时,0表示非夏令时,-1表示不确定是否是夏令时
time.localtime().tm_year #获取当前年份
time.localtime().tm_mon #获取当前月份,以此类推
time.localtime()[0] #通过坐标取年份
time.localtime()[1] #通过坐标取月份,以此类推
str(time.localtime().tm_year)+"年"+str(time.localtime().tm_mon)+"月"+str(time.localtime().tm_mday)+"日"
#拼接获取当前系统年月日'2020年12月27日'
time.time() #获取当前系统的时间戳1609072019.7015758,表示距离1970/1/1 00:00:00的总秒数,可通过获取两个时间点的时间戳计算差值
time.gmtime() #获取格林威治时间,与北京时间差八个小时
time.localtime(1609072019.7015758) #将时间戳转化为时间元组
time.mktime(time.localtime()) #将时间元组转换为时间戳
time.strptime("2020-12-28 22:29:11","%Y %m %d %H %M %S") #将字符串转化为时间元组,继而可转换为时间戳
time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) #将时间元组转换为指定的格式'2020-12-28 22:29:11'

#如果想获取含中文的时间格式,执行以下代码
import time
import locale
locale.setlocale(locale.LC_CTYPE,'chinese')
time.strftime("%Y年%m月%d日 %H时%M分%S秒",time.localtime()) #'2020年12月28日 22时55分21秒'

time.sleep(3) #进程休眠三秒
time.perf_counter() #记录进程占用cup的时间戳

import datetime
a=datetime.date.today() #获取今天的日期datetime.date(2020, 12, 28)
b=a+datetime.timedelta(days=3) #获取三天后的日期datetime.date(2020, 12, 31)
c=a.replace(day=25) #获取a日期当月25号的日期datetime.date(2020, 12, 25)
d=a-c #算出两个日期的时间间隔datetime.timedelta(days=3),d.days可获得3
f=b+d #datetime.date(2021, 1, 3)

a=datetime.datetime.now() #获得当前日期时间datetime.datetime(2020, 12, 29, 21, 56, 34, 483999)
b=a.replace(year=2019,month=11,day=28,hour=20,minute=10,second=35) 
#获得修改后的时间datetime.datetime(2019, 11, 28, 20, 10, 35, 483999)

a=datetime.datetime(2020,3,8)
b=datetime.datetime(2020,4,11)
c=(b-a).days #计算两个日期之间的天数c=34

a=datetime.datetime.now()
b=datetime.timedelta(days=1)
c=str(a+b)[:-7] #获取一天后的日期时间

#获取日历
import calendar
a=calendar.calendar(2020,3,1,1)

#生成一个网页文件日历
a=calendar.HTMLCalendar(calendar.SUNDAY)
with open('e:\\calendar.html','w') as fp:
	fp.write(a.formatmonth(2020,10))


#获取各种日期时间格式的方法
import time
import locale

locale.setlocale(locale.LC_CTYPE,'chinese')

def get_current_chinese_date(): #获取含中文日期
    return time.strftime('%Y年%m月%d日',time.localtime())

def get_current_date(): #不含中文日期
    return time.strftime('%Y-%m-%d',time.localtime())

def get_current_chinese_time(): #含中文时间
    return time.strftime('%H时%M分%S秒',time.localtime())

def get_current_time(): #不含中文时间
    return time.strftime('%H:%M:%S',time.localtime())

def get_current_chinese_datetime(): #含中文日期时间
    return time.strftime('%Y年%m月%d日 %H时%M分%S秒',time.localtime())

def get_current_datetime(): #不含中文日期时间
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())

def stamptime_to_chinesedatetime(stamptime): #戳入时间戳stamptime,转换为含中文日期时间
    if not isinstance(stamptime,(int,float)):
        print("传入的参数不是数字")
        return None
    current_time=time.localtime(stamptime)
    return time.strftime('%Y年%m月%d日 %H时%M分%S秒',current_time)

def chinese_datetime_to_stamptime(current_datetime): #含中文日期时间转换为时间戳
    try:
        struct_time=time.strptime(current_datetime,'%Y年%m月%d日 %H时%M分%S秒')
    except Exception as e:
        print("输入的日期时间格式错误",e)
    return time.mktime(struct_time)

def datetime_to_stamptime(current_datetime): #不含中文日期时间转换为时间戳
    try:
        struct_time=time.strptime(current_datetime,'%Y-%m-%d %H:%M:%S')
    except Exception as e:
        print("输入的日期时间格式错误",e)
    return time.mktime(struct_time)

猜你喜欢

转载自blog.csdn.net/weixin_44123630/article/details/111825383