python-时间处理
一、datatime获取当前时间str(time.strftime(%Y%m%d%H%M, time.localtime(time.time())))1.1 datetime.timedelta一个时间可以通过或-一个timedelta得到另一个时间。from datetimeimportdatetime, timedelta datetime.strftime(datetime.strptime(DT_PROCESS,%Y-%m-%d)- timedelta(days1),%Y-%m-%d)与relativedelta()与timedelta()的区别timedelta()函数仅支持days和weeks参数 而relativedelta()函数可以支持年 、月、日、周、时、分、秒的参数。1.2 datetime.strftimestrftime() 函数用于格式化时间返回以可读字符串表示的当地时间格式由参数 format 决定。datetime.strftime(datetime.strptime(DT_PROCESS,%Y-%m-%d)- timedelta(days1),%Y-%m-%d)1.3 字符串转换为datetimefrom datetimeimportdatetime datetime.strptime(2022-12-24,%Y-%m-%d)https://blog.csdn.net/ztf312/article/details/883222221.4 月份减减importdateutil#统计3月份的时间statis_time2023-03-01#前一个月时间statis_time_datedatetime.strptime(statis_time,%Y-%m-%d)st_bone_month_datestatis_time_date - dateutil.relativedelta.relativedelta(months1)bone_month_timest_bone_month_date.strftime(%Y-%m-%d)#前三个月时间st_bthree_month_datestatis_time_date - dateutil.relativedelta.relativedelta(months3)bthree_month_timest_bthree_month_date.strftime(%Y-%m-%d)上面的有问题就用这个current_datedatetime.now()- timedelta(days1)start_datecurrent_date - relativedelta(months3)start_datestart_date.strftime(%Y-%m-%d)end_datecurrent_date.strftime(%Y-%m-%d)target_partitionend_date二、pandaspandas 生成时间范围pd.date_range(start2021-07-01,end2022-07-20,freq30D)字符串时间转位date时间order_detail[dt]pd.to_datetime(order_detail[dt])三、参考资料将时间精确到小时https://blog.csdn.net/tz_zs/article/details/89270050/https://blog.csdn.net/qq_26870933/article/details/82425315