80 個(gè)例子,徹底掌握Python日期時(shí)間處理!

劇照 | 《沙丘》
日常工作中,用 Python 處理時(shí)間格式的數(shù)據(jù)是非常常見的,今天就來(lái)分享 DateTime 相關(guān)的示例。
文章很長(zhǎng),高低要忍一下,如果忍不了,那就收藏吧,總會(huì)用到的!
使用 time 模塊展示當(dāng)前日期和時(shí)間
將天、小時(shí)、分鐘轉(zhuǎn)換為秒
使用 Pandas 獲取當(dāng)前日期和時(shí)間
將字符串轉(zhuǎn)換為日期時(shí)間對(duì)象
以毫秒為單位獲取當(dāng)前時(shí)間
以 MST、EST、UTC、GMT 和 HST 獲取當(dāng)前日期時(shí)間
從給定的日期當(dāng)中獲取星期幾
計(jì)算兩個(gè)日期時(shí)間對(duì)象之間的時(shí)差
將 5 分鐘添加到 Unix 時(shí)間戳
在 Python 中遍歷一系列日期
巴黎時(shí)間更改為紐約時(shí)間
使用 Python 獲得最后7個(gè)工作日
從今天的日期和一個(gè)人的生日推算年齡
獲得本月的第一個(gè)星期二
將整數(shù)轉(zhuǎn)換為日期對(duì)象
當(dāng)前日期減去 N 天的天數(shù)
比較兩個(gè)日期
從 datetime 對(duì)象中提取年份
在 Python 中找到星期幾
從當(dāng)前日期獲取 7 天前的日期
將兩個(gè)日期時(shí)間對(duì)象之間的差值轉(zhuǎn)換為秒
獲得任何一個(gè)月的第三個(gè)星期五
從 Python 中的周數(shù)獲取日期
獲取特定日期的工作日
創(chuàng)建一個(gè) 15 分鐘前的 DateTime
從特定日期獲取周的開始和結(jié)束日期
兩個(gè)日期之間的差異(以秒為單位)
以這種格式獲取昨天的日期MMDDYY
從今天的日期獲取上周三
所有可用時(shí)區(qū)的列表打印
獲取指定開始日期和結(jié)束日期之間的日期范圍
毫秒轉(zhuǎn)換為數(shù)據(jù)
查找給定日期之后的第一個(gè)星期日的日期
將(Unix)時(shí)間戳秒轉(zhuǎn)換為日期和時(shí)間字符串
以月為單位的兩個(gè)日期之間的差異
將本地時(shí)間字符串轉(zhuǎn)換為 UTC
獲取當(dāng)月的最后一個(gè)星期四
從特定日期查找一年中的第幾周
從給定日期獲取星期幾
用 AM PM 打印當(dāng)前時(shí)間
獲得一個(gè)月的最后一天
從工作日值中獲取工作日名稱
將 N 小時(shí)數(shù)添加到當(dāng)前日期時(shí)間
從當(dāng)前日期獲取年、月、日、小時(shí)、分鐘
獲取特定月份和年份的最后一個(gè)星期日
查找特定日期的年份中的哪一天
查找當(dāng)前日期是工作日還是周末
組合 datetime.date 和 datetime.time 對(duì)象
獲得每月的第 5 個(gè)星期一
將日期時(shí)間對(duì)象轉(zhuǎn)換為日期對(duì)象
獲取沒(méi)有微秒的當(dāng)前日期時(shí)間
將 N 秒數(shù)添加到特定日期時(shí)間
從當(dāng)前日期獲取兩位數(shù)的月份和日期
從特定日期獲取月份數(shù)據(jù)的開始和結(jié)束日期
以周為單位的兩個(gè)日期之間的差異
將字符串格式的日期轉(zhuǎn)換為 Unix 時(shí)間戳
獲取最后一個(gè)周日和周六的日期
檢查對(duì)象是否屬于 datetime.date 類型
獲取特定日期的周數(shù)
獲取 UTC 時(shí)間
獲取本周的開始和結(jié)束日期
兩個(gè)日期之間的差異(以分鐘為單位)
將日期時(shí)間對(duì)象轉(zhuǎn)換為日期字符串
獲得上周五
將 3 周添加到任何特定日期
在其他兩個(gè)日期之間生成一個(gè)隨機(jī)日期
查找從今天開始的第一個(gè)星期一的日期
兩個(gè)日期之間的差異(以天為單位)
向當(dāng)前日期添加六個(gè)月
將數(shù)據(jù)時(shí)間對(duì)象轉(zhuǎn)換為 Unix(時(shí)間戳)
將年、月、日、時(shí)、分、秒的 N 個(gè)數(shù)字添加到當(dāng)前日期時(shí)間
獲取指定開始日期和結(jié)束日期之間的日期范圍
減去 N 個(gè)年、月、日、時(shí)、分、秒到當(dāng)前日期時(shí)間
獲取指定年份和月份的月份第一天的工作日和月份的天數(shù)
打印特定年份的所有星期一
打印特定年份的日歷
從月份編號(hào)中獲取月份名稱
從給定日期獲取一周的開始和結(jié)束日期
根據(jù)當(dāng)前日期查找上一個(gè)和下一個(gè)星期一的日期
獲取當(dāng)前季度的第一個(gè)日期和最后一個(gè)日期
1使用 time 模塊展示當(dāng)前日期和時(shí)間
import?time
from?time?import?gmtime,?strftime
?
t?=?time.localtime()
print?(time.asctime(t))
print(strftime("%a,?%d?%b?%Y?%H:%M:%S?+0000",?gmtime()))
print(strftime("%A",?gmtime()))
print(strftime("%D",?gmtime()))
print(strftime("%B",?gmtime()))
print(strftime("%y",?gmtime()))
?
#?Convert?seconds?into?GMT?date
print(strftime("%a,?%d?%b?%Y?%H:%M:%S?+0000",?gmtime(1234567890)))
Output:
Sun May 7 09:30:37 2017
Sun, 07 May 2017 04:00:37 +0000
Sunday
05/07/17
May
17
Fri, 13 Feb 2009 23:31:30 +0000
2將天、小時(shí)、分鐘轉(zhuǎn)換為秒
SECONDS_PER_MINUTE??=?60
SECONDS_PER_HOUR????=?3600
SECONDS_PER_DAY?????=?86400
?
#Read?the?inputs?from?user
days????=?int(input("Enter?number?of?Days:?"))
hours???=?int(input("Enter?number?of?Hours:?"))
minutes?=?int(input("Enter?number?of?Minutes:?"))
seconds?=?int(input("Enter?number?of?Seconds:?"))
?
#Calculate?the?days,?hours,?minutes?and?seconds
total_seconds?=?days?*?SECONDS_PER_DAY
total_seconds?=?total_seconds?+?(?hours?*?SECONDS_PER_HOUR)
total_seconds?=?total_seconds?+?(?minutes?*?SECONDS_PER_MINUTE)
total_seconds?=?total_seconds?+?seconds
?
#Display?the?result
print("Total?number?of?seconds:?","%d"%(total_seconds))
Output:
Enter number of Days: 5
Enter number of Hours: 36
Enter number of Minutes: 24
Enter number of Seconds: 15
Total number of seconds: 563055
3使用 Pandas 獲取當(dāng)前日期和時(shí)間
import?pandas?as?pd
print(pd.datetime.now())
print(pd.datetime.now().date())
print(pd.datetime.now().year)
print(pd.datetime.now().month)
print(pd.datetime.now().day)
print(pd.datetime.now().hour)
print(pd.datetime.now().minute)
print(pd.datetime.now().second)
print(pd.datetime.now().microsecond)
Output:
2018-01-19 16:08:28.393553
2018-01-19
2018
1
19
16
8
28
394553
4將字符串轉(zhuǎn)換為日期時(shí)間對(duì)象
from?datetime?import?datetime
from?dateutil?import?parser
d1?=?"Jan?7?2015??1:15PM"
d2?=?"2015?Jan?7??1:33PM"
#?If?you?know?date?format
date1?=?datetime.strptime(d1,?'%b?%d?%Y?%I:%M%p')
print(type(date1))
print(date1)
#?If?you?don't?know?date?format
date2?=?parser.parse(d2)
print(type(date2))
print(date2)
Output:
class 'datetime.datetime'
2015-01-07 13:15:00
class 'datetime.datetime'
2015-01-07 13:33:00
5以毫秒為單位獲取當(dāng)前時(shí)間
import?time
?
milliseconds?=?int(round(time.time()?*?1000))
print(milliseconds)
Output:
1516364270650
6以 MST、EST、UTC、GMT 和 HST 獲取當(dāng)前日期時(shí)間
from?datetime?import?datetime
from?pytz?import?timezone
?
mst?=?timezone('MST')
print("Time?in?MST:",?datetime.now(mst))
?
est?=?timezone('EST')
print("Time?in?EST:",?datetime.now(est))
?
utc?=?timezone('UTC')
print("Time?in?UTC:",?datetime.now(utc))
?
gmt?=?timezone('GMT')
print("Time?in?GMT:",?datetime.now(gmt))
?
hst?=?timezone('HST')
print("Time?in?HST:",?datetime.now(hst))
Output:
Time in MST: 2017-01-19 06:06:14.495605-07:00
Time in EST: 2017-01-19 08:06:14.496606-05:00
Time in UTC: 2017-01-19 13:06:14.496606+00:00
Time in GMT: 2017-01-19 13:06:14.496606+00:00
Time in HST: 2017-01-19 03:06:14.497606-10:00
7從給定的日期當(dāng)中獲取星期幾
import?datetime
?
dayofweek?=?datetime.date(2010,?6,?16).strftime("%A")
print(dayofweek)
#?weekday?Monday?is?0?and?Sunday?is?6
print("weekday():",?datetime.date(2010,?6,?16).weekday())
?
#?isoweekday()?Monday?is?1?and?Sunday?is?7
print("isoweekday()",?datetime.date(2010,?6,?16).isoweekday())
?
dayofweek?=?datetime.datetime.today().strftime("%A")
print(dayofweek)
print("weekday():",?datetime.datetime.today().weekday())
print("isoweekday()",?datetime.datetime.today().isoweekday())
Output:
Wednesday
weekday(): 2
isoweekday() 3
Friday
weekday(): 4
isoweekday() 5
8計(jì)算兩個(gè)日期時(shí)間對(duì)象之間的時(shí)差
import?datetime
from?datetime?import?timedelta
?
datetimeFormat?=?'%Y-%m-%d?%H:%M:%S.%f'
date1?=?'2016-04-16?10:01:28.585'
date2?=?'2016-03-10?09:56:28.067'
diff?=?datetime.datetime.strptime(date1,?datetimeFormat)\
????-?datetime.datetime.strptime(date2,?datetimeFormat)
?
print("Difference:",?diff)
print("Days:",?diff.days)
print("Microseconds:",?diff.microseconds)
print("Seconds:",?diff.seconds)
Output:
Difference: 37 days, 0:05:00.518000
Days: 37
Microseconds: 518000
Seconds: 300
9將 5 分鐘添加到 Unix 時(shí)間戳
import?datetime
import?calendar
?
future?=?datetime.datetime.utcnow()?+?datetime.timedelta(minutes=5)
print(calendar.timegm(future.timetuple()))
Output:
1621069619
10在 Python 中遍歷一系列日期
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
11巴黎時(shí)間更改為紐約時(shí)間
import?pendulum
?
in_paris?=?pendulum.datetime(2016,?8,?7,?22,?24,?30,?tz='Europe/Paris')
print(in_paris)
?
in_us?=?in_paris.in_timezone('America/New_York')
print(in_us)
Output:
2016-08-07T22:24:30+02:00
2016-08-07T16:24:30-04:00
12使用 Python 獲得最后7個(gè)工作日
from?datetime?import?date
from?datetime?import?timedelta
?
today?=?date.today()
?
for?i?in?range(7):
????d?=?today?-?timedelta(days=i)
????if?d.weekday()?5:
????????print(d)
Output:
2021-05-18
2021-05-17
2021-05-14
2021-05-13
2021-05-12
13從今天的日期和一個(gè)人的生日推算年齡
from?datetime?import?date
?
?
def?calculate_age(born):
????today?=?date.today()
????try:
????????birthday?=?born.replace(year=today.year)
????except?ValueError:
????????birthday?=?born.replace(year=today.year,?month=born.month?+?1,?day=1)
????if?birthday?>?today:
????????return?today.year?-?born.year?-?1
????else:
????????return?today.year?-?born.year
?
?
print(calculate_age(date(2001,?3,?1)))
Output:
20
14獲得本月的第一個(gè)星期二
import?calendar
from?datetime?import?datetime
?
c?=?calendar.Calendar(firstweekday=calendar.SUNDAY)
monthcal?=?c.monthdatescalendar(datetime.today().year,?datetime.today().month)
?
try:
????tues?=?[day?for?week?in?monthcal?for?day?in?week?if
????????????day.weekday()?==?calendar.TUESDAY?and?day.month?==?datetime.today().month][0]
????print(tues)
except?IndexError:
????print('No?date?found')
Output:
2021-05-04
15將整數(shù)轉(zhuǎn)換為日期對(duì)象
from?datetime?import?datetime
i?=?1545730073
timestamp?=?datetime.fromtimestamp(i)
print(timestamp)
print(type(timestamp))
Output:
2018-12-25 14:57:53
16當(dāng)前日期減去 N 天的天數(shù)
from?datetime?import?datetime,?timedelta
d?=?datetime.today()?-?timedelta(days=5)
print(d)
Output:
2021-05-10 12:59:14.867969
17比較兩個(gè)日期
import?datetime
a?=?datetime.datetime(2020,?12,?31,?23,?59,?59)
b?=?datetime.datetime(2020,?11,?30,?23,?59,?59)
print(a?print(a?>?b)
Output:
False
True
18從 datetime 對(duì)象中提取年份
import?datetime
?
year?=?datetime.date.today().year
print(year)
Output:
2021
19在 Python 中找到星期幾
import?pendulum
?
dt?=?pendulum.parse('2021-05-18')
print(dt.day_of_week)
?
dt?=?pendulum.parse('2021-05-01')
print(dt.day_of_week)
?
dt?=?pendulum.parse('2021-05-21')
print(dt.day_of_week)
Output:
2
6
5
20從當(dāng)前日期獲取 7 天前的日期
from?datetime?import?datetime,?timedelta
?
now?=?datetime.now()
?
for?x?in?range(7):
????d?=?now?-?timedelta(days=x)
????print(d.strftime("%Y-%m-%d"))
Output:
2021-05-18
2021-05-17
2021-05-16
2021-05-15
2021-05-14
2021-05-13
2021-05-12
21將兩個(gè)日期時(shí)間對(duì)象之間的差值轉(zhuǎn)換為秒
import?datetime
?
time1?=?datetime.datetime.strptime('19?01?2021',?'%d?%m?%Y')
time2?=?datetime.datetime.strptime('25?01?2021',?'%d?%m?%Y')
?
difference?=?time2?-?time1
print(difference)
?
seconds?=?difference.total_seconds()
print(seconds)
Output:
6 days, 0:00:00
518400.0
22獲得任何一個(gè)月的第三個(gè)星期五
import?calendar
?
c?=?calendar.Calendar(firstweekday=calendar.SUNDAY)
year?=?2021
month?=?5
monthcal?=?c.monthdatescalendar(year,?month)
?
try:
????third_friday?=?[day?for?week?in?monthcal?for?day?in?week?if
????????????????????day.weekday()?==?calendar.FRIDAY?and?day.month?==?month][2]
????print(third_friday)
except?IndexError:
????print('No?date?found')
Output:
2021-05-21
23從 Python 中的周數(shù)獲取日期
import?datetime
from?dateutil.relativedelta?import?relativedelta
?
week?=?25
year?=?2021
date?=?datetime.date(year,?1,?1)?+?relativedelta(weeks=+week)
print(date)
Output:
2021-06-25
24獲取特定日期的工作日
import?datetime
?
print(datetime.date(2020,?5,?15).isocalendar()[2])
Output:
5
25創(chuàng)建一個(gè) 15 分鐘前的 DateTime
import?datetime
?
dt?=?datetime.datetime.now()?-?datetime.timedelta(minutes=15)
print(dt)
Output:
2021-05-15 22:25:55.897365
26從特定日期獲取周的開始和結(jié)束日期
import?pendulum
?
dt?=?pendulum.datetime(2012,?9,?5)
?
start?=?dt.start_of('week')
print(start.to_datetime_string())
?
end?=?dt.end_of('week')
print(end.to_datetime_string())
Output:
2012-09-03 00:00:00
2012-09-09 23:59:59
27兩個(gè)日期之間的差異(以秒為單位)
from?datetime?import?datetime
?
fmt?=?'%Y-%m-%d?%H:%M:%S'
d1?=?datetime.strptime('2020-01-01?17:31:22',?fmt)
d2?=?datetime.strptime('2020-01-03?17:31:22',?fmt)
?
days_diff?=?d2?-?d1
print(days_diff.days?*?24?*?60?*?60)
Output:
172800
28以這種格式獲取昨天的日期MMDDYY
from?datetime?import?date,?timedelta
?
yesterday?=?date.today()?-?timedelta(days=1)
print(yesterday.strftime('%m%d%y'))
Output:
051421
29從今天的日期獲取上周三
from?datetime?import?date
from?datetime?import?timedelta
today?=?date.today()
offset?=?(today.weekday()?-?2)?%?7
wednesday?=?today?-?timedelta(days=offset)
print(wednesday)
Output:
2021-05-12
30所有可用時(shí)區(qū)的列表打印
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
31獲取指定開始日期和結(jié)束日期之間的日期范圍
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
32毫秒轉(zhuǎn)換為數(shù)據(jù)
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
33查找給定日期之后的第一個(gè)星期日的日期
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
34將(Unix)時(shí)間戳秒轉(zhuǎn)換為日期和時(shí)間字符串
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
35以月為單位的兩個(gè)日期之間的差異
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
36將本地時(shí)間字符串轉(zhuǎn)換為 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
37獲取當(dāng)月的最后一個(gè)星期四
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
38從特定日期查找一年中的第幾周
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
39從給定日期獲取星期幾
import?datetime
import?calendar
?
dt?=?datetime.datetime(2021,?4,?25,?23,?24,?55,?173504)
print(calendar.day_name[dt.weekday()])
Output:
Sunday
40用 AM PM 打印當(dāng)前時(shí)間
from?datetime?import?datetime
?
print(datetime.today().strftime("%I:%M?%p"))
Output:
10:11 PM
41獲得一個(gè)月的最后一天
import?calendar
?
print(calendar.monthrange(2002,?1)[1])
print(calendar.monthrange(2008,?6)[1])
print(calendar.monthrange(2012,?2)[1])
print(calendar.monthrange(2015,?2)[1])
Output:
31
30
29
28
42從工作日值中獲取工作日名稱
import?calendar
?
print(calendar.day_name[0])
print(calendar.day_name[1])
print(calendar.day_name[2])
print(calendar.day_name[3])
print(calendar.day_name[4])
print(calendar.day_name[5])
print(calendar.day_name[6])
Output:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
43將 N 小時(shí)數(shù)添加到當(dāng)前日期時(shí)間
from?datetime?import?datetime,?timedelta
d?=?datetime.today()?+?timedelta(hours=18)
print(d)
Output:
2021-05-16 07:36:08.189948
44從當(dāng)前日期獲取年、月、日、小時(shí)、分鐘
import?datetime
?
now?=?datetime.datetime.now()
print(now.year,?now.month,?now.day,?now.hour,?now.minute,?now.second)
Output:
2021 5 15 14 27 33
45獲取特定月份和年份的最后一個(gè)星期日
import?calendar
month?=?calendar.monthcalendar(2021,?2)
last_sunday?=?max(month[-1][calendar.SUNDAY],?month[-2][calendar.SUNDAY])
print(last_sunday)
Output:
28
46查找特定日期的年份中的哪一天
import?pendulum
?
dt?=?pendulum.parse('2015-05-18')
print(dt.day_of_year)
?
dt?=?pendulum.parse('2019-12-01')
print(dt.day_of_year)
?
dt?=?pendulum.parse('2018-01-21')
print(dt.day_of_year)
Output:
138
335
21
47查找當(dāng)前日期是工作日還是周末
import?datetime
?
weekno?=?datetime.datetime.today().weekday()
if?weekno?5:
????print("Weekday")
else:??#?5?Sat,?6?Sun
????print("Weekend")
Output:
Weekday
48組合 datetime.date 和 datetime.time 對(duì)象
import?datetime
?
d?=?datetime.datetime.combine(datetime.date(2020,?11,?14),
??????????????????????????????datetime.time(10,?23,?15))
?
print(d)
Output:
2020-11-14 10:23:15
49獲得每月的第 5 個(gè)星期一
import?calendar
?
c?=?calendar.Calendar(firstweekday=calendar.SUNDAY)
year?=?2016
month?=?2
monthcal?=?c.monthdatescalendar(year,?month)
?
try:
????fifth_monday?=?[day?for?week?in?monthcal?for?day?in?week?if
????????????????????day.weekday()?==?calendar.MONDAY?and?day.month?==?month][4]
????print(fifth_monday)
except?IndexError:
????print('No?date?found')
Output:
2016-02-29
50將日期時(shí)間對(duì)象轉(zhuǎn)換為日期對(duì)象
from?datetime?import?datetime
datetime_obj?=?datetime(2020,?12,?15,?10,?15,?45,?321474)
print(datetime_obj)
date_obj?=?datetime_obj.date()
print(date_obj)
Output:
2020-12-15 10:15:45.321474
2020-12-15
51獲取沒(méi)有微秒的當(dāng)前日期時(shí)間
from?datetime?import?datetime
print(datetime.now().isoformat('?',?'seconds'))
Output:
2021-05-15 12:55:45
52將 N 秒數(shù)添加到特定日期時(shí)間
import?datetime
a?=?datetime.datetime(2020,?12,?31,?23,?59,?45)
b?=?a?+?datetime.timedelta(seconds=30)
print(a)
print(b)
Output:
2020-12-31 23:59:45
2021-01-01 00:00:15
53從當(dāng)前日期獲取兩位數(shù)的月份和日期
import?datetime
?
dt?=?datetime.datetime.now()
?
print(dt.strftime('%m'))
print('{:02d}'.format(dt.month))
print(f'{dt.month:02d}')
print('%02d'?%?dt.month)
?
print(dt.strftime('%d'))
print('{:02d}'.format(dt.day))
print(f'{dt.day:02d}')
print('%02d'?%?dt.day)
Output:
05
05
05
05
15
15
15
15
54從特定日期獲取月份數(shù)據(jù)的開始和結(jié)束日期
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
55以周為單位的兩個(gè)日期之間的差異
from?datetime?import?date
?
date1?=?date(2020,?12,?23)
date2?=?date(2021,?5,?11)
?
days?=?abs(date1?-?date2).days
print(days?//?7)
Output:
19
56將字符串格式的日期轉(zhuǎn)換為 Unix 時(shí)間戳
import?datetime
?
stime?=?'15/05/2021'
print(datetime.datetime.strptime(stime,?"%d/%m/%Y").timestamp())
Output:
1621017000.0
57獲取最后一個(gè)周日和周六的日期
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
58檢查對(duì)象是否屬于 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
59獲取特定日期的周數(shù)
import?datetime
?
print(datetime.date(2020,?5,?15).isocalendar()[1])
Output:
20
60獲取 UTC 時(shí)間
from?datetime?import?datetime
dt?=?datetime.utcnow()
print(dt)
Output:
2021-05-15 17:01:31.008808
61獲取本周的開始和結(jié)束日期
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
62兩個(gè)日期之間的差異(以分鐘為單位)
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
63將日期時(shí)間對(duì)象轉(zhuǎn)換為日期字符串
import?datetime
?
t?=?datetime.datetime(2020,?12,?23)
x?=?t.strftime('%m/%d/%Y')
print(x)
Output:
12/23/2020
64獲得上周五
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
65將 3 周添加到任何特定日期
import?pendulum
?
dt?=?pendulum.datetime(2012,?2,?15)
dt?=?dt.add(weeks=3)
print(dt.to_date_string())
Output:
2012-03-07
66在其他兩個(gè)日期之間生成一個(gè)隨機(jī)日期
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()))
Output:
02/25/2020 08:26 AM
67查找從今天開始的第一個(gè)星期一的日期
from?dateutil.rrule?import?rrule,?WEEKLY,?MO
from?datetime?import?date
?
next_monday?=?rrule(freq=WEEKLY,?dtstart=date.today(),?byweekday=MO,?count=1)[0]
print(next_monday)
Output:
2021-05-17 00:00:00
68兩個(gè)日期之間的差異(以天為單位)
from?datetime?import?date
?
d1?=?date(2019,?8,?18)
d2?=?date(2021,?12,?10)
?
days_diff?=?d2?-?d1
print(days_diff.days)
Output:
845
69向當(dāng)前日期添加六個(gè)月
from?datetime?import?datetime
from?dateutil.relativedelta?import?*
date?=?datetime.now()
print(date)
date?=?date?+?relativedelta(months=+6)
print(date)
Output:
2021-05-15 13:48:52.135612
2021-11-15 13:48:52.135612
70將數(shù)據(jù)時(shí)間對(duì)象轉(zhuǎn)換為 Unix(時(shí)間戳)
import?datetime
import?time
#?Saturday,?October?10,?2015?10:10:00?AM
date_obj?=?datetime.datetime(2015,?10,?10,?10,?10)
print("Unix?Timestamp:?",?(time.mktime(date_obj.timetuple())))
Output:
Unix Timestamp: 1444452000.0
71將年、月、日、時(shí)、分、秒的 N 個(gè)數(shù)字添加到當(dāng)前日期時(shí)間
import?datetime
from?dateutil.relativedelta?import?relativedelta
add_days?=?datetime.datetime.today()?+?relativedelta(days=+6)
add_months?=?datetime.datetime.today()?+?relativedelta(months=+6)
add_years?=?datetime.datetime.today()?+?relativedelta(years=+6)
add_hours?=?datetime.datetime.today()?+?relativedelta(hours=+6)
add_mins?=?datetime.datetime.today()?+?relativedelta(minutes=+6)
add_seconds?=?datetime.datetime.today()?+?relativedelta(seconds=+6)
print("Current?Date?Time:",?datetime.datetime.today())
print("Add?6?days:",?add_days)
print("Add?6?months:",?add_months)
print("Add?6?years:",?add_years)
print("Add?6?hours:",?add_hours)
print("Add?6?mins:",?add_mins)
print("Add?6?seconds:",?add_seconds)
Output:
Current Date Time: 2017-04-04 18:32:10.192671
Add 6 days: 2017-04-10 18:32:10.191671
Add 6 months: 2017-10-04 18:32:10.192671
Add 6 years: 2023-04-04 18:32:10.192671
Add 6 hours: 2017-04-05 00:32:10.192671
Add 6 mins: 2017-04-04 18:38:10.192671
Add 6 seconds: 2017-04-04 18:32:16.192671
72獲取指定開始日期和結(jié)束日期之間的日期范圍
import?datetime
?
start?=?datetime.datetime.strptime("2016-06-15",?"%Y-%m-%d")
end?=?datetime.datetime.strptime("2016-06-30",?"%Y-%m-%d")
date_array?=?\
????(start?+?datetime.timedelta(days=x)?for?x?in?range(0,?(end-start).days))
?
for?date_object?in?date_array:
????print(date_object.strftime("%Y-%m-%d"))
Output:
2016-06-15
2016-06-16
2016-06-17
2016-06-18
2016-06-19
2016-06-20
2016-06-21
2016-06-22
2016-06-23
2016-06-24
2016-06-25
2016-06-26
2016-06-27
2016-06-28
2016-06-29
73減去 N 個(gè)年、月、日、時(shí)、分、秒到當(dāng)前日期時(shí)間
import?datetime
from?dateutil.relativedelta?import?relativedelta
sub_days?=?datetime.datetime.today()?+?relativedelta(days=-6)
sub_months?=?datetime.datetime.today()?+?relativedelta(months=-6)
sub_years?=?datetime.datetime.today()?+?relativedelta(years=-6)
sub_hours?=?datetime.datetime.today()?+?relativedelta(hours=-6)
sub_mins?=?datetime.datetime.today()?+?relativedelta(minutes=-6)
sub_seconds?=?datetime.datetime.today()?+?relativedelta(seconds=-6)
print("Current?Date?Time:",?datetime.datetime.today())
print("Subtract?6?days:",?add_days)
print("Subtract?6?months:",?add_months)
print("Subtract?6?years:",?add_years)
print("Subtract?6?hours:",?add_hours)
print("Subtract?6?mins:",?add_mins)
print("Subtract?6?seconds:",?add_seconds)
Output:
Current Date Time: 2017-04-04 18:36:29.213046
Subtract 6 days: 2017-03-29 18:36:29.213046
Subtract 6 months: 2016-10-04 18:36:29.213046
Subtract 6 years: 2011-04-04 18:36:29.213046
Subtract 6 hours: 2017-04-04 12:36:29.213046
Subtract 6 mins: 2017-04-04 18:30:29.213046
Subtract 6 seconds: 2017-04-04 18:36:23.213046
74獲取指定年份和月份的月份第一天的工作日和月份的天數(shù)
import?calendar
?
print("Year:2002?-?Month:2")
month_range?=?calendar.monthrange(2002,?2)
print("Weekday?of?first?day?of?the?month:",?month_range[0])
print("Number?of?days?in?month:",?month_range[1])
print()
print("Year:2010?-?Month:5")
month_range?=?calendar.monthrange(2010,?5)
print("Weekday?of?first?day?of?the?month:",?month_range[0])
print("Number?of?days?in?month:",?month_range[1])
Output:
Year:2002 - Month:2
Weekday of first day of the month: 4
Number of days in month: 28
Year:2010 - Month:5
Weekday of first day of the month: 5
Number of days in month: 31
75打印特定年份的所有星期一
from?datetime?import?date,?timedelta
year?=?2018
date_object?=?date(year,?1,?1)
date_object?+=?timedelta(days=1-date_object.isoweekday())
while?date_object.year?==?year:
????print(date_object)
????date_object?+=?timedelta(days=7)
Output:
2018-01-01
2018-01-08
2018-01-15
2018-01-22
2018-01-29
2018-02-05
2018-02-12
...
2018-11-12
2018-11-19
2018-11-26
2018-12-03
2018-12-10
2018-12-17
2018-12-24
2018-12-31
76打印特定年份的日歷
import?calendar
cal_display?=?calendar.TextCalendar(calendar.MONDAY)
#?Year:?2019
#?Column?width:?1
#?Lines?per?week:?1?
#?Number?of?spaces?between?month?columns:?0
#?No.?of?months?per?column:?2
print(cal_display.formatyear(2019,?1,?1,?0,?2))
Output:
略
77從月份編號(hào)中獲取月份名稱
import?calendar
import?datetime
#?Month?name?from?number
print("Month?name?from?number?5:")
month_num?=?1
month_abre?=?datetime.date(2015,?month_num,?1).strftime('%b')
month_name?=?datetime.date(2015,?month_num,?1).strftime('%B')
print("Short?Name:",?month_abre)
print("Full??Name:",?month_name)
print("\nList?of?all?months?from?calendar")
#?Print?list?of?all?months?from?calendar
for?month_val?in?range(1,?13):
????print(calendar.month_abbr[month_val],?"-",?calendar.month_name[month_val])
Output:
Month name from number 5:
Short Name: Jan
Full Name: January
List of all months from calendar
Jan - January
Feb - February
Mar - March
Apr - April
May - May
Jun - June
Jul - July
Aug - August
Sep - September
Oct - October
Nov - November
Dec - December
78從給定日期獲取一周的開始和結(jié)束日期
from?datetime?import?datetime,?timedelta
?
date_str?=?'2018-01-14'
date_obj?=?datetime.strptime(date_str,?'%Y-%m-%d')
?
start_of_week?=?date_obj?-?timedelta(days=date_obj.weekday())??#?Monday
end_of_week?=?start_of_week?+?timedelta(days=6)??#?Sunday
print(start_of_week)
print(end_of_week)
Output:
2018-01-08 00:00:00
2018-01-14 00:00:00
79根據(jù)當(dāng)前日期查找上一個(gè)和下一個(gè)星期一的日期
import?datetime
today?=?datetime.date.today()
last_monday?=?today?-?datetime.timedelta(days=today.weekday())
coming_monday?=?today?+?datetime.timedelta(days=-today.weekday(),?weeks=1)
print("Today:",?today)
print("Last?Monday:",?last_monday)
print("Coming?Monday:",?coming_monday)
Output:
Today: 2018-01-21
Last Monday: 2018-01-15
Coming Monday: 2018-01-22
80獲取當(dāng)前季度的第一個(gè)日期和最后一個(gè)日期
from?datetime?import?datetime,?timedelta
current_date?=?datetime.now()
current_quarter?=?round((current_date.month?-?1)?/?3?+?1)
first_date?=?datetime(current_date.year,?3?*?current_quarter?-?2,?1)
last_date?=?datetime(current_date.year,?3?*?current_quarter?+?1,?1)\
????+?timedelta(days=-1)
print("First?Day?of?Quarter:",?first_date)
print("Last?Day?of?Quarter:",?last_date)
Output:
First Day of Quarter: 2018-01-01 00:00:00
Last Day of Quarter: 2018-03-31 00:00:00
還不過(guò)癮?試試它們
▲Python 微服務(wù)框架 Nameko 初體驗(yàn)
