又肝了3天,整理了80个Python DateTime 例子,必须收藏! (4)

Output:

2021-05-12 复制代码 所有可用时区的列表打印 import pytz for i in pytz.all_timezones: print(i) 复制代码

Output:

Africa/Abidjan Africa/Accra Africa/Addis_Ababa Africa/Algiers Africa/Asmara Africa/Asmera Africa/Bamako Africa/Bangui Africa/Banjul Africa/Bissau ... US/Mountain US/Pacific US/Samoa UTC Universal W-SU WET Zulu 复制代码 获取指定开始日期和结束日期之间的日期范围 import datetime start = datetime.datetime.strptime("21-06-2020", "%d-%m-%Y") end = datetime.datetime.strptime("05-07-2020", "%d-%m-%Y") date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end - start).days)] for date in date_generated: print(date.strftime("%d-%m-%Y")) 复制代码

Output:

21-06-2020 22-06-2020 23-06-2020 24-06-2020 25-06-2020 26-06-2020 27-06-2020 28-06-2020 29-06-2020 30-06-2020 01-07-2020 02-07-2020 03-07-2020 04-07-2020 复制代码 毫秒转换为数据 import datetime time_in_millis = 1596542285000 dt = datetime.datetime.fromtimestamp(time_in_millis / 1000.0, tz=datetime.timezone.utc) print(dt) 复制代码

Output:

2020-08-04 11:58:05+00:00 复制代码 查找给定日期之后的第一个星期日的日期 import datetime def next_weekday(d, weekday): days_ahead = weekday - d.weekday() if days_ahead <= 0: days_ahead += 7 return d + datetime.timedelta(days_ahead) d = datetime.date(2021, 5, 16) next_sunday = next_weekday(d, 6) print(next_sunday) 复制代码

Output:

2021-05-23 复制代码 将(Unix)时间戳秒转换为日期和时间字符串 from datetime import datetime dateStr = datetime.fromtimestamp(1415419007).strftime("%A, %B %d, %Y %I:%M:%S") print(type(dateStr)) print(dateStr) 复制代码

Output:

Saturday, November 08, 2014 09:26:47 复制代码 以月为单位的两个日期之间的差异 from datetime import datetime from dateutil import relativedelta date1 = datetime.strptime(\'2014-01-12 12:00:00\', \'%Y-%m-%d %H:%M:%S\') date2 = datetime.strptime(\'2021-07-15 12:00:00\', \'%Y-%m-%d %H:%M:%S\') r = relativedelta.relativedelta(date2, date1) print(r.months + (12 * r.years)) 复制代码

Output:

90 复制代码 将本地时间字符串转换为 UTC from datetime import * from dateutil import * from dateutil.tz import * utc_zone = tz.gettz(\'UTC\') local_zone = tz.gettz(\'America/Chicago\') utc_zone = tz.tzutc() local_zone = tz.tzlocal() local_time = datetime.strptime("2020-10-25 15:12:00", \'%Y-%m-%d %H:%M:%S\') print(local_time) local_time = local_time.replace(tzinfo=local_zone) print(local_time) utc_time = local_time.astimezone(utc_zone) print(utc_time) utc_string = utc_time.strftime(\'%Y-%m-%d %H:%M:%S\') print(utc_string) 复制代码

Output:

2020-10-25 15:12:00 2020-10-25 15:12:00+05:30 2020-10-25 09:42:00+00:00 2020-10-25 09:42:00 复制代码 获取当月的最后一个星期四 import calendar from datetime import datetime month = calendar.monthcalendar(datetime.today().year, datetime.today().month) thrusday = max(month[-1][calendar.THURSDAY], month[-2][calendar.THURSDAY]) print(thrusday) 复制代码

Output:

27 复制代码 从特定日期查找一年中的第几周 import pendulum dt = pendulum.parse(\'2015-05-18\') print(dt.week_of_year) dt = pendulum.parse(\'2019-12-01\') print(dt.week_of_year) dt = pendulum.parse(\'2018-01-21\') print(dt.week_of_year) 复制代码

Output:

21 48 3 复制代码 从给定日期获取星期几 import datetime import calendar dt = datetime.datetime(2021, 4, 25, 23, 24, 55, 173504) print(calendar.day_name[dt.weekday()]) 复制代码

Output:

Sunday 复制代码 用 AM PM 打印当前时间 from datetime import datetime print(datetime.today().strftime("%I:%M %p")) 复制代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzppdz.html