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

Output:

05 05 05 05 15 15 15 15 复制代码 从特定日期获取月份数据的开始和结束日期 import pendulum dt = pendulum.datetime(2012, 9, 5) start = dt.start_of(\'month\') print(start.to_datetime_string()) end = dt.end_of(\'month\') print(end.to_datetime_string()) 复制代码

Output:

2012-09-01 00:00:00 2012-09-30 23:59:59 复制代码 以周为单位的两个日期之间的差异 from datetime import date date1 = date(2020, 12, 23) date2 = date(2021, 5, 11) days = abs(date1 - date2).days print(days // 7) 复制代码

Output:

19 复制代码 将字符串格式的日期转换为 Unix 时间戳 import datetime stime = \'15/05/2021\' print(datetime.datetime.strptime(stime, "%d/%m/%Y").timestamp()) 复制代码

Output:

1621017000.0 复制代码 获取最后一个周日和周六的日期 from datetime import datetime, timedelta def prior_week_end(): return datetime.now() - timedelta(days=((datetime.now().isoweekday() + 1) % 7)) def prior_week_start(): return prior_week_end() - timedelta(days=6) print(\'Sunday\', format(prior_week_start())) print(\'Saturday\', format(prior_week_end())) 复制代码

Output:

Sunday 2021-05-09 13:13:30.057765 Saturday 2021-05-15 13:13:30.058912 复制代码 检查对象是否属于 datetime.date 类型 import datetime x = \'2012-9-1\' y = datetime.date(2012, 9, 1) print(isinstance(x, datetime.date)) print(isinstance(y, datetime.date)) 复制代码

Output:

False True 复制代码 获取特定日期的周数 import datetime print(datetime.date(2020, 5, 15).isocalendar()[1]) 复制代码

Output:

20 复制代码 获取 UTC 时间 from datetime import datetime dt = datetime.utcnow() print(dt) 复制代码

Output:

2021-05-15 17:01:31.008808 复制代码 获取本周的开始和结束日期 import pendulum today = pendulum.now() start = today.start_of(\'week\') print(start.to_datetime_string()) end = today.end_of(\'week\') print(end.to_datetime_string()) 复制代码

Output:

2021-05-10 00:00:00 2021-05-16 23:59:59 复制代码 两个日期之间的差异(以分钟为单位) from datetime import datetime fmt = \'%Y-%m-%d %H:%M:%S\' d1 = datetime.strptime(\'2010-01-01 17:31:22\', fmt) d2 = datetime.strptime(\'2010-01-03 17:31:22\', fmt) days_diff = d2 - d1 print(days_diff.days * 24 * 60) 复制代码

Output:

2880 复制代码 将日期时间对象转换为日期字符串 import datetime t = datetime.datetime(2020, 12, 23) x = t.strftime(\'%m/%d/%Y\') print(x) 复制代码

Output:

12/23/2020 复制代码 获得上周五 from datetime import date from datetime import timedelta today = date.today() offset = (today.weekday() - 4) % 7 friday = today - timedelta(days=offset) print(friday) 复制代码

Output:

2021-05-14 复制代码 将 3 周添加到任何特定日期 import pendulum dt = pendulum.datetime(2012, 2, 15) dt = dt.add(weeks=3) print(dt.to_date_string()) 复制代码

Output:

2012-03-07 复制代码 在其他两个日期之间生成一个随机日期 import random import time def str_time_prop(start, end, time_format, prop): stime = time.mktime(time.strptime(start, time_format)) etime = time.mktime(time.strptime(end, time_format)) ptime = stime + prop * (etime - stime) return time.strftime(time_format, time.localtime(ptime)) def random_date(start, end, prop): return str_time_prop(start, end, \'%m/%d/%Y %I:%M %p\', prop) print(random_date("1/1/2020 1:10 PM", "1/1/2021 1:10 AM", random.random())) 复制代码

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

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