时间处理
获取完整时间
import pendulumimport time# here put the import lib# 获取当前完整时间now = pendulum.now(tz="local")print(now) # 2022-05-31T15:41:12.163333+08:00
获取当前时间
import pendulum# 获取当前时间print("to_time_string: ", pendulum.now(tz="local").to_time_string()) # to_time_string: 15:50:27print(pendulum.from_timestamp(timestamp, tz="local").time()) # 15:44:54.470152print("to_day_datetime_string: ", pendulum.now(tz="local").to_day_datetime_string()) # to_day_datetime_string: Tue, May 31, 2022 4:09 PMprint("to_formatted_date_string: ", pendulum.now(tz="local").to_formatted_date_string()) # to_formatted_date_string: May 31, 2022"""获取当前时间和日期"""# 2022-07-03 14:58:22pendulum.now().to_datetime_string()"""获取当前日期"""# 2022-07-03pendulum.now().to_date_string()"""获取当前时间"""# 14:58:22pendulum.now().to_time_string()
获取当前日期
import pendulumimport time# 获取当前日期print(pendulum.now(tz="local").date()) # 2022-05-31print("to_date_string: ", pendulum.now(tz="local").to_date_string()) # to_date_string: 2022-05-31timestamp = time.time()print(pendulum.from_timestamp(timestamp, tz="local").date()) # 2022-05-31
获取当前时区
import pendulum# 获取当前时区print(pendulum.local_timezone()) # Timezone('Asia/Shanghai')
获取时间元组
import pendulum# 获取时间元组res = pendulum.now().timetuple()print(res) # time.struct_time(tm_year=2022, tm_mon=5, tm_mday=31, tm_hour=15, tm_min=43, tm_sec=1, tm_wday=1, tm_yday=151, tm_isdst=0)print(res.tm_mday) # 31
时间运算
"""时间运算"""dt1 = pendulum.now()dt2 = pendulum.tomorrow()print(dt1, dt2)print(dt1.add(days=1)) # 输出时间 + 1天period = dt2 - dt1print("相隔多少年:", period.in_years()) # 相隔多少年: 0print("相隔多少月:", period.in_months())# 相隔多少月: 0print("相隔多少天:", period.in_days()) # 相隔多少天: 1print("相隔多少小时:", period.in_hours()) #相隔多少小时: 7print("相隔多少分钟:", period.in_minutes()) # 相隔多少分钟: 459print("相隔多少秒:", period.in_seconds()) # 相隔多少秒: 27543