Package: cgai-time
cgai-time介绍简单又实用的时间python处理库注意非时分秒日期格式：推荐的日期格式为%Y-%m-%d，比如：2021-07-20也支持数字数组格式表示，比如： (2021,7,20)时分秒格式：仅支持%Y-%m-%d %H:%M:%S，比如："2021-07-20 09:43:49"建议以时间戳的形式计算时间计算，最后显示格式化日期安装教程pipinstallcgai-time案例说明：案例中演示了多个函数不同参数的实现方式，结果一致实例时间工具fromcgai_time.TimeimportTimeHandlerct=TimeHandler()日期格式转化字符串格式转成数组格式: "2021-07-16" -> (2021,7,16)t_date=ct.strDate2TupleDate('2021-07-16')print(t_date)# (2021,7,16)数组格式转成字符串格式:  (2021,7,16) -> "2021-07-16"s_date=ct.tupleDate2StrDate((2021,7,16))print(s_date)# "2021-07-16"格式化日期转时间戳str_time='2021-07-18 17:19:44'time_stamp=ct.strTime2TimeStamp(str_time)print(time_stamp)# 1626599984.0时间戳转格式化日期时间time_stamp=1626599984.0str_time=ct.timeStamp2StrTime(time_stamp)print(str_time)# "2021-07-18 17:19:44"将斜杠/日期转成减号-日期slash_date='2023/3/6'sdate=ct.slashDate2StrDate(slash_date)print(sdate)# 2023-03-06将减号-日期转成斜杠/日期date='2023-03-06'slash_date=ct.strDate2SlashDate(date)print(slash_date)# 2023/3/6秒数转分钟seconds=70minutes=ct.s2m(seconds)int_minutes=ct.s2m(seconds,getInt=True)# 获取整数部分print(minutes)# 1.1666666666666667print(int_minutes)# 1分钟转秒数minutes=12.015seconds=ct.m2s(minutes)int_seconds=ct.m2s(minutes,getInt=True)print(seconds)# 720.9000000000001print(int_seconds)# 720秒转小时seconds=1244hours=ct.s2h(seconds)int_hours=ct.s2h(seconds,getInt=True)print(hours)# 0.34555555555555556print(int_hours)# 0小时转秒hours=0.342seconds=ct.h2s(hours)int_seconds=ct.h2s(hours,getInt=True)print(seconds)# 1231.2000000000003print(int_seconds)# 1231秒转天数seconds=112341days=ct.s2d(seconds)int_days=ct.s2d(seconds,getInt=True)print(days)# 1.3002430555555555print(int_days)# 1天数转秒days=0.451seconds=ct.d2s(days)int_seconds=ct.d2s(days,getInt=True)print(seconds)# 38966.399999999994print(int_seconds)# 38966分钟转小时minutes=1234hours=ct.m2h(minutes)int_hours=ct.m2h(minutes,getInt=True)print(hours)# 20.566666666666666print(int_hours)# 20小时转分钟hours=0.241minutes=ct.h2m(hours)int_minutes=ct.h2m(hours,getInt=True)print(minutes)#  14.459999999999999print(int_minutes)# 14分钟转天数minutes=421321days=ct.m2d(minutes)int_days=ct.m2d(minutes,getInt=True)print(days)# 292.5840277777778print(int_days)# 292天数转分钟days=0.912minutes=ct.d2m(days)int_minutes=ct.d2m(days,getInt=True)print(minutes)# 1313.2800000000002print(int_minutes)# 1313小时转天数hours=482days=ct.h2d(hours)int_days=ct.h2d(hours,getInt=True)print(days)# 20.083333333333332print(int_days)# 20天转小时days=0.924hours=ct.d2h(days)int_hours=ct.d2h(days,getInt=True)print(hours)#  22.176000000000002print(int_hours)# 22小时转毫秒hours=2.5int_h,left_ms=ct.h2ms(hours)毫秒转小时ms=4600int_h,left_ms=ct.ms2h(ms)分钟转毫秒minutes=1.5ms=ct.m2ms(minutes)毫秒转分钟ms=4600int_m,left_ms=ct.ms2m(ms)秒转毫秒seconds=2.5ms=ct.s2ms(seconds)毫秒转秒ms=4600int_s,left_ms=ct.ms2s(ms)输入天数，小时，分钟转成秒seconds=ct.getSeconds(d=1,h=3,m=25)print(seconds)# 98700seconds=ct.getSeconds(h=3,m=25)print(seconds)# 12300将秒数转成以天,小时,分钟,秒对应值seconds=3600*24+3599dhms=ct.DHMS(seconds)print(dhms)#{'day': 1, 'hour': 0, 'minute': 59, 'second': 59}字幕时间转毫秒srt='00:02:42,950'ms=ct.srt2ms(srt)print(ms)# 162950毫秒转字幕时间ms=4600srt=ct.ms2srt(ms)print(srt)# 00:00:04,600时间计算获取当前时间戳cts=ct.CTS()print(cts)#  1626660957.2461472获取当前格式化日期时间str_time=ct.StrTime()print(str_time)#  2021-07-20 09:43:49计算两格式化日期之间的所差秒数start_date='2021-07-18 17:19:44'end_date='2021-07-18 22:32:11'dt=ct.deltaTime(end_date,start_date)print(dt)# 18747.0格式化日期直接进行时间增减str_time='2021-07-18 17:19:44'new_str_time=ct.addTime(str_time,d=1)# 增加一天print(new_str_time)# 2021-07-19 17:19:44new_str_time=ct.addTime(str_time,h=8)# 增加8小时print(new_str_time)# 2021-07-19 01:19:44new_str_time=ct.subTime(str_time,m=50)# 减少50分钟print(new_str_time)# 2021-07-18 16:29:44new_str_time=ct.subTime(str_time,s=21)# 减少21秒print(new_str_time)# 2021-07-18 17:18:23new_str_time=ct.addTime(str_time,d=1,h=8,m=50,s=21)print(new_str_time)# 2021-07-21 18:48:37字幕时间增减毫秒start_srt_time='00:02:42,950'ms=4600end_srt_time=ct.srt_add(start_srt_time,ms)print(end_srt_time)# 00:02:47,550end_srt_time=ct.srt_sub(start_srt_time,ms)print(end_srt_time)#  00:02:38,350日计算获取今日日期today=ct.getToday()print(today)# 2021-07-19获取昨天日期yesterday=ct.getYesterday()print(yesterday)# 2021-07-18获取明天日期tomorrow=ct.getTomorrow()print(tomorrow)# 2021-07-20计算两日期之间的相差多少天数start_date='2021-05-15'end_date='2021-07-18'days=ct.deltaDays(start_date,end_date)print(days)# 64days=ct.deltaDays(start_date=(2021,7,15),end_date=(2021,7,18))print(days)# 3日期加减new_date=ct.dateAdd('2021-07-20',5)new_date=ct.dateAdd((2021,7,20),5)print(new_date)# 2021-07-25new_date=ct.dateAdd('2021-07-20',-5)new_date=ct.dateSub('2021-07-20',5)print(new_date)#  2021-07-15是否为周一result=ct.isMonday('2021-07-19')result=ct.isMonday(2021,7,19)print(result)# True是否为周二result=ct.isTuesday('2021-07-20')result=ct.isTuesday(2021,7,20)print(result)# True是否为周三result=ct.isWednesday('2021-07-21')result=ct.isWednesday(2021,7,21)print(result)# True是否为周四result=ct.isThursday('2021-07-22')result=ct.isThursday(2021,7,22)print(result)# True是否为周五result=ct.isFriday('2021-07-23')result=ct.isFriday(2021,7,23)print(result)# True是否为周六result=ct.isSaturday('2021-07-24')result=ct.isSaturday(2021,7,24)print(result)# True是否为周日/周末result=ct.isSunday('2021-07-25')result=ct.isSunday(2021,7,25)result=ct.isWeekend('2021-07-25')result=ct.isWeekend(2021,7,25)print(result)# True获取指定前后日期之间所有日期列表start_date='2021-06-28'end_date='2021-07-18'date_list=ct.getDateList(start_date,end_date)date_list=ct.getDateList(start_date=(2021,6,28),end_date=(2021,7,18))print(date_list)['2021-06-28','2021-06-29','2021-06-30','2021-07-01','2021-07-02','2021-07-03','2021-07-04','2021-07-05','2021-07-06','2021-07-07','2021-07-08','2021-07-09','2021-07-10','2021-07-11','2021-07-12','2021-07-13','2021-07-14','2021-07-15','2021-07-16','2021-07-17','2021-07-18']获取前后日期之间所跨年份start_date='2020-05-15'end_date='2021-07-18'years=ct.acrossYears(start_date,end_date)print(years)# [2020, 2021]years=ct.acrossYears(start_date=(2020,5,15),end_date=(2021,7,18))print(years)# [2020, 2021]获取前后日期之间所跨月份start_date='2020-05-15'end_date='2021-07-18'months=ct.acrossMonths(start_date,end_date)print(months)# [5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7]months=ct.acrossMonths(start_date=(2020,5,15),end_date=(2021,7,18))print(months)# [5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7]获取前后日期之间所跨周start_date='2021-05-15'end_date='2021-07-18'weeks=ct.acrossWeeks(start_date,end_date)weeks=ct.acrossWeeks(start_date=(2021,5,15),end_date=(2021,7,18))print(weeks)[['2021-05-10','2021-05-11','2021-05-12','2021-05-13','2021-05-14','2021-05-15','2021-05-16'],['2021-05-17','2021-05-18','2021-05-19','2021-05-20','2021-05-21','2021-05-22','2021-05-23'],['2021-05-24','2021-05-25','2021-05-26','2021-05-27','2021-05-28','2021-05-29','2021-05-30'],['2021-05-31','2021-06-01','2021-06-02','2021-06-03','2021-06-04','2021-06-05','2021-06-06'],['2021-06-07','2021-06-08','2021-06-09','2021-06-10','2021-06-11','2021-06-12','2021-06-13'],['2021-06-14','2021-06-15','2021-06-16','2021-06-17','2021-06-18','2021-06-19','2021-06-20'],['2021-06-21','2021-06-22','2021-06-23','2021-06-24','2021-06-25','2021-06-26','2021-06-27'],['2021-06-28','2021-06-29','2021-06-30','2021-07-01','2021-07-02','2021-07-03','2021-07-04'],['2021-07-05','2021-07-06','2021-07-07','2021-07-08','2021-07-09','2021-07-10','2021-07-11'],['2021-07-12','2021-07-13','2021-07-14','2021-07-15','2021-07-16','2021-07-17','2021-07-18']]获取前后日期之间的所有星期一start_date='2021-05-15'end_date='2021-07-18'Mondays=ct.getMondays(start_date,end_date)Mondays=ct.getMondays(start_date=(2021,5,15),end_date=(2021,7,18))Mondays=ct.getDatesByNumber(start_date,end_date,1)Mondays=ct.getDatesByNumber(start_date=(2021,5,15),end_date=(2021,7,18),number=1)print(Mondays)['2021-05-17','2021-05-24','2021-05-31','2021-06-07','2021-06-14','2021-06-21','2021-06-28','2021-07-05','2021-07-12']获取前后日期之间的所有星期二Tuesdays=ct.getTuesdays(start_date,end_date)Tuesdays=ct.getTuesdays(start_date=(2021,5,15),end_date=(2021,7,18))Tuesdays=ct.getDatesByNumber(start_date,end_date,2)Tuesdays=ct.getDatesByNumber(start_date=(2021,5,15),end_date=(2021,7,18),number=2)print(Tuesdays)['2021-05-18','2021-05-25','2021-06-01','2021-06-08','2021-06-15','2021-06-22','2021-06-29','2021-07-06','2021-07-13']获取前后日期之间的所有星期三Wednesdays=ct.getWednesdays(start_date,end_date)Wednesdays=ct.getWednesdays(start_date=(2021,5,15),end_date=(2021,7,18))Wednesdays=ct.getDatesByNumber(start_date,end_date,3)Wednesdays=ct.getDatesByNumber(start_date=(2021,5,15),end_date=(2021,7,18),number=3)print(Wednesdays)['2021-05-19','2021-05-26','2021-06-02','2021-06-09','2021-06-16','2021-06-23','2021-06-30','2021-07-07','2021-07-14']获取前后日期之间的所有星期四Thursdays=ct.getThursdays(start_date,end_date)Thursdays=ct.getThursdays(start_date=(2021,5,15),end_date=(2021,7,18))Thursdays=ct.getDatesByNumber(start_date,end_date,4)Thursdays=ct.getDatesByNumber(start_date=(2021,5,15),end_date=(2021,7,18),number=4)print(Thursdays)['2021-05-20','2021-05-27','2021-06-03','2021-06-10','2021-06-17','2021-06-24','2021-07-01','2021-07-08','2021-07-15']获取前后日期之间的所有星期五Fridays=ct.getFridays(start_date,end_date)Fridays=ct.getFridays(start_date=(2021,5,15),end_date=(2021,7,18))Fridays=ct.getDatesByNumber(start_date,end_date,5)Fridays=ct.getDatesByNumber(start_date=(2021,5,15),end_date=(2021,7,18),number=5)print(Fridays)['2021-05-21','2021-05-28','2021-06-04','2021-06-11','2021-06-18','2021-06-25','2021-07-02','2021-07-09','2021-07-16']获取前后日期之间的所有星期六Saturdays=ct.getSaturdays(start_date,end_date)Saturdays=ct.getSaturdays(start_date=(2021,5,15),end_date=(2021,7,18))Saturdays=ct.getDatesByNumber(start_date,end_date,6)Saturdays=ct.getDatesByNumber(start_date=(2021,5,15),end_date=(2021,7,18),number=6)print(Saturdays)['2021-05-15','2021-05-22','2021-05-29','2021-06-05','2021-06-12','2021-06-19','2021-06-26','2021-07-03','2021-07-10','2021-07-17']获取前后日期之间的所有星期天Sundays=ct.getSundays(start_date,end_date)Sundays=ct.getSundays(start_date=(2021,5,15),end_date=(2021,7,18))Sundays=ct.getDatesByNumber(start_date,end_date,7)Sundays=ct.getDatesByNumber(start_date=(2021,5,15),end_date=(2021,7,18),number=7)print(Sundays)['2021-05-16','2021-05-23','2021-05-30','2021-06-06','2021-06-13','2021-06-20','2021-06-27','2021-07-04','2021-07-11','2021-07-18']周计算获取指定日期的所在周几数值weekday_number=ct.getWeekdayNumber(2021,7,16)weekday_number=ct.getWeekdayNumber('2021-07-16')print(weekday_number)# 5获取指定日期的周几名称weekday_cname=ct.getWeekdayCName(2021,7,16)weekday_cname=ct.getWeekdayCName('2021-07-16')print(weekday_cname)# 周五判定是否为周末 也就是星期天isWeekend=ct.isWeekend(2021,7,16)print(isWeekend)# FalseisWeekend=ct.isWeekend('2021-07-18')print(isWeekend)# True获取本周起始日期与结束日期current_week_start,current_week_end=ct.getCurrentWeekStartAndEnd()print(current_week_start,current_week_end)# 2021-07-19 2021-07-25获取上周起始日期与结束日期last_week_start,last_week_end=ct.getLastWeekStartAndEnd()print(last_week_start,last_week_end)# 2021-07-12 2021-07-18获取下周起始日期与结束日期next_week_start,next_week_end=ct.getNextWeekStartAndEnd()print(next_week_start,next_week_end)# 2021-07-26 2021-08-01获取指定日期所在周的起始与结束日期week_start,week_end=ct.getWeekStartAndEnd(2021,7,16)week_start,week_end=ct.getWeekStartAndEnd('2021-07-16')print(week_start,week_end)# '2021-07-12', '2021-07-18'获取指定某天所在周的所有日期列表weekdates=ct.getWeekDates('2021-07-16')weekdates=ct.getWeekDates(2021,7,16)print(weekdates)# ['2021-07-12', '2021-07-13', '2021-07-14', '2021-07-15', '2021-07-16', '2021-07-17', '2021-07-18']获取指定两日期之间所跨周start_date='2021-07-15'end_date='2021-07-18'weeks=ct.acrossWeeks(start_date,end_date)weeks=ct.acrossWeeks(start_date=(2021,7,1),end_date=(2021,7,18))print(len(weeks))print(weeks)3[['2021-06-28','2021-06-29','2021-06-30','2021-07-01','2021-07-02','2021-07-03','2021-07-04'],['2021-07-05','2021-07-06','2021-07-07','2021-07-08','2021-07-09','2021-07-10','2021-07-11'],['2021-07-12','2021-07-13','2021-07-14','2021-07-15','2021-07-16','2021-07-17','2021-07-18']]月操作月份加减new_date=ct.monthAdd('2021-12-03',11)print(new_date)#2022-11-3new_date=ct.monthAdd('2021-12-03',-11)print(new_date)#2021-01-03判定指定月份是否为31天isMonth31=ct.isMonth31(8)isMonth31=ct.isMonth31('8')print(isMonth31)# True获取本月起始与结束日期current_month_start,current_month_end=ct.getCurrentMonthStartAndEnd()print(current_month_start,current_month_end)# 2021-07-01 2021-07-31获取上月起始与结束日期last_month_start,last_month_end=ct.getLastMonthStartAndEnd()print(last_month_start,last_month_end)# 2021-06-01 2021-06-30获取下月起始与结束日期next_month_start,next_month_end=ct.getNextMonthStartAndEnd()print(next_month_start,next_month_end)# 2021-08-01 2021-08-31获取指定某天所在月份的起始与结束日期start_day,end_day=ct.getMonthStartAndEnd(2021,7,16)start_day,end_day=ct.getMonthStartAndEnd('2021-07-16')print(start_day,end_day)#  2021-07-01 2021-07-31获取指定年指定月的所有日期列表month_dates=ct.getMonthDateList(2021,2)month_dates=ct.getMonthDateList(date='2021-02-01')month_dates=ct.getMonthDateList(date=(2021,2,12))print(month_dates)['2021-02-01','2021-02-02','2021-02-03','2021-02-04','2021-02-05','2021-02-06','2021-02-07','2021-02-08','2021-02-09','2021-02-10','2021-02-11','2021-02-12','2021-02-13','2021-02-14','2021-02-15','2021-02-16','2021-02-17','2021-02-18','2021-02-19','2021-02-20','2021-02-21','2021-02-22','2021-02-23','2021-02-24','2021-02-25','2021-02-26','2021-02-27','2021-02-28']获取指定年月所有周month_weeks=ct.getMonthWeeks(2021,7)month_weeks=ct.getMonthWeeks(date='2021-07-02')month_weeks=ct.getMonthWeeks(date=(2021,7,2))print(month_weeks)[['2021-06-28','2021-06-29','2021-06-30','2021-07-01','2021-07-02','2021-07-03','2021-07-04'],['2021-07-05','2021-07-06','2021-07-07','2021-07-08','2021-07-09','2021-07-10','2021-07-11'],['2021-07-12','2021-07-13','2021-07-14','2021-07-15','2021-07-16','2021-07-17','2021-07-18'],['2021-07-19','2021-07-20','2021-07-21','2021-07-22','2021-07-23','2021-07-24','2021-07-25']]季度计算获取指定天所在的季度信息date='2023-03-23'quarter_number,quarter_list=ct.getQuarter(date)print(quarter_number,quarter_list)# 1  [1,2,3]    表示是在第一季度,该季度有1,2,3个月获取指定天所在的季度的各月起始日期date='2023-03-23'quarter_start_dates=ct.getQuarterStartDates(date)print(quarter_start_dates)# ['2023-01-01', '2023-02-01', '2023-03-01']获取本季度的各月起始日期quarter_start_dates=ct.getCurrentQuarterStartDates()print(quarter_start_dates)# ['2023-01-01', '2023-02-01', '2023-03-01']获取上季度的各月起始日期quarter_start_dates=ct.getLastQuarterStartDates()print(quarter_start_dates)# ['2022-10-01', '2022-11-01', '2022-12-01']获取下季度的各月起始日期quarter_start_dates=ct.getNextQuarterStartDates()print(quarter_start_dates)# ['2023-04-01', '2023-05-01', '2023-06-01']基于指定天进行季度相加，返回季度的各月起始日期date='2022-02-02'quarter_start_dates=ct.quarterAdd(date,3)# 该天所在季度向后再加3个季度print(quarter_start_dates)# ['2022-10-01', '2022-11-01', '2022-12-01']基于指定天进行季度相减，返回季度的各月起始日期date='2022-03-23'quarter_start_dates=ct.quarterSub(date,3)# 该天所在季度向前再减3个季度print(quarter_start_dates)# ['2021-04-01', '2021-05-01', '2021-06-01']年计算判定是否为闰年result=ct.isLeapyear(2021)print(result)# False获取指定年份的天数days=ct.getYearDays(2020)print(days)# 366