Python日期時間(詳細)

獲取當前時間戳

import time

t = time.time()

millis1 = int(t)
print('10位時間戳:{}'.format(millis1))

millis2 = int(t * 1000)
print('13位時間戳:{}'.format(millis2))

列印結果:
10位時間戳:1594800086
13位時間戳:1594800086816

 

格式化時間

import time

now = time.strftime("%Y-%m-%d %H:%M:%S")
print('當前時間格式化:{}'.format(now))

now2 = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(1594368331))
print('指定時間格式化:{}'.format(now2))

列印結果:
當前時間格式化:2020-07-15 16:08:57
指定時間格式化:2020-07-10 16:05:31

 

格式化符號表:
%y 兩位數的年份表示(00-99)
%Y 四位數的年份表示(000-9999)
%m 月份(01-12)
%d 月內中的一天(0-31)
%H 24小時制小時數(0-23)
%I 12小時制小時數(01-12)
%M 分鐘數(00=59)
%S 秒(00-59)
%a 本地簡化星期名稱
%A 本地完整星期名稱
%b 本地簡化的月份名稱
%B 本地完整的月份名稱
%c 本地相應的日期表示和時間表示
%j 年內的一天(001-366)
%p 本地A.M.或P.M.的等價符
%U 一年中的星期數(00-53)星期天為星期的開始
%w 星期(0-6),星期天為星期的開始
%W 一年中的星期數(00-53)星期一為星期的開始
%x 本地相應的日期表示
%X 本地相應的時間表示
%Z 當前時區的名稱
%% %號本身

 

格式化時間轉時間戳

import time

str = '2020-07-10 16:05:31'
ts = time.mktime(time.strptime(str,"%Y-%m-%d %H:%M:%S"))
print('格式化時間轉時間戳:{}'.format(int(ts)))

列印結果:
格式化時間轉時間戳:1594368331

 

時間元組+拆分時間

當前時間元組

import time
t_tuple = time.localtime()
print('當前時間元組:{}'.format(t_tuple))
print('當前年:{}'.format(t_tuple.tm_year))
print('當前月:{}'.format(t_tuple.tm_mon))
print('當前日:{}'.format(t_tuple.tm_mday))
print('當前時:{}'.format(t_tuple.tm_hour))
print('當前分:{}'.format(t_tuple.tm_min))
print('當前秒:{}'.format(t_tuple.tm_sec))
print('當前周幾:{}'.format(t_tuple.tm_wday))
print('當前是一年中第幾天:{}'.format(t_tuple.tm_yday))
print('當前是否是夏令時:{}'.format(t_tuple.tm_isdst))

列印結果:
當前時間元組:time.struct_time(tm_year=2020, tm_mon=7, tm_mday=15, tm_hour=16, tm_min=59, tm_sec=45, tm_wday=2, tm_yday=197, tm_isdst=0)
當前年:2020
當前月:7
當前日:15
當前時:16
當前分:59
當前秒:45
當前周幾:2
當前是一年中第幾天:197
當前是否是夏令時:0

 

時間戳轉時間元組

import time

t_tuple2 = time.localtime(1594368331)
print('時間戳轉時間元組:{}'.format(t_tuple2))

列印結果:
時間戳轉時間元組:time.struct_time(tm_year=2020, tm_mon=7, tm_mday=10, tm_hour=16, tm_min=5, tm_sec=31, tm_wday=4, tm_yday=192, tm_isdst=0)

 

格式化時間轉時間元組

import time

tts = time.strptime('2018-09-30 11:32:23', '%Y-%m-%d %H:%M:%S')
print('格式化時間轉時間元組:{}'.format(tts))

列印結果:
格式化時間轉時間元組:time.struct_time(tm_year=2018, tm_mon=9, tm_mday=30, tm_hour=11, tm_min=32, tm_sec=23, tm_wday=6, tm_yday=273, tm_isdst=-1)

 

時間元組轉時間戳

import time

tt = time.mktime((2020, 7, 10, 16, 5, 31, 0, 0, 0))
print('時間元組轉時間戳:{}'.format(int(tt)))

列印結果:
時間元組轉時間戳:1594368331

 

獲取今天日期

下面兩種寫法 效果相同 前者是格式化時間的方法 可以傳入任意時間戳指定返回任意格式  後者是專門為獲取今天日期提供的方法

import time
import datetime

today1 = time.strftime("%Y-%m-%d")
print(today1)

today2 = datetime.date.today()
print(today2)

列印結果:
2020-07-15
2020-07-15

 

today方法的結果還可以進一步轉化成元組:

import datetime

d = datetime.date.today().timetuple()
print(d)

列印結果:
time.struct_time(tm_year=2020, tm_mon=7, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=197, tm_isdst=-1)

 

時間戳轉日期格式

import time
import datetime

d1 = time.strftime('%Y-%m-%d',time.localtime(1594804107))
print('時間戳轉日期:{}'.format(d1))

d2 = datetime.date.fromtimestamp(1594804107)
print('時間戳轉日期:{}'.format(d2))

列印結果:
2020-07-15
2020-07-15

 

兩個日期相差天數

import datetime

a = datetime.date(2020, 7, 1)
b = datetime.date(2020, 7, 15)
print('b的日期減去a的日期:{}'.format(b.__sub__(a).days))
print('a的日期減去b的日期:{}'.format(a.__sub__(b).days))

列印結果:
b的日期減去a的日期:14
a的日期減去b的日期:-14

 

獲取幾天/小時/分 之前/之後的時間

import datetime

# 3小時之後
later_hours = datetime.datetime.now() + datetime.timedelta(hours=3)
print(later_hours.strftime('%Y-%m-%d %H:%M:%S'))

# 3小時之後的時間戳
print(int(later_hours.timestamp()))

# 10分鐘之後
later_minutes = datetime.datetime.now() + datetime.timedelta(minutes=10)
print(later_minutes.strftime('%Y-%m-%d %H:%M:%S'))

# 10分鐘之後的時間戳
print(int(later_minutes.timestamp()))

# 7天之後
later_days = datetime.datetime.now() + datetime.timedelta(days=7)
print(later_days.strftime('%Y-%m-%d %H:%M:%S'))

# 7天之後的時間戳
print(int(later_days.timestamp()))

 

import datetime

# 3小時之前
before_hours = datetime.datetime.now() - datetime.timedelta(hours=3)
print(before_hours.strftime('%Y-%m-%d %H:%M:%S'))

# 3小時之前的時間戳
print(int(before_hours.timestamp()))

# 10分鐘之前
before_minutes = datetime.datetime.now() - datetime.timedelta(minutes=10)
print(before_minutes.strftime('%Y-%m-%d %H:%M:%S'))

# 10分鐘之前的時間戳
print(int(before_minutes.timestamp()))

# 7天之前
before_days = datetime.datetime.now() - datetime.timedelta(days=7)
print(before_days.strftime('%Y-%m-%d %H:%M:%S'))

# 7天之前的時間戳
print(int(before_days.timestamp()))

 

獲取上個月開始結束時間

import datetime

# 上個月第一天
date = datetime.datetime.today()
year,month = date.year,date.month
if month == 1:
    startDate = datetime.date(year-1, 12, 1)
else:
    startDate = datetime.date(year, month-1, 1)
print(startDate)

# 上個月最後一天
today = datetime.datetime.today()
endDate = datetime.date(today.year, today.month, 1) - datetime.timedelta(days=1)
print(endDate)

列印結果:
2020-06-01
2020-06-30

 

import datetime

# 上個月開始時間戳
if month == 1:
    startTime = int(time.mktime(datetime.date(year-1,12,31).timetuple()))
else:
    startTime = int(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month-1,1).timetuple()))
print(startTime)

# 上個月結束時間戳
endTime = int(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month,1).timetuple())) - 1
print(endTime)

列印結果:
1590940800
1593532799

 

獲取本月開始結束時間

import datetime
import time

# 本月第一天
date = datetime.datetime.today()
year,month = date.year,date.month
startDate = datetime.date(year, month, 1)
print(startDate)

# 本月最後一天
today = datetime.datetime.today()
if month == 12:
    endDate = datetime.date(year, month, 31)
else:
    endDate = datetime.date(today.year, today.month + 1, 1) - datetime.timedelta(days=1)
print(endDate)

列印結果:
2020-07-01
2020-07-31

 

import datetime
import time

# 本月開始時間戳
startTime = int(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month,1).timetuple()))
print(startTime)

# 本月結束時間戳
if month == 12:
    endTime = int(time.mktime(datetime.date(datetime.date.today().year + 1,1, 1).timetuple())) - 1
else:
    endTime = int(time.mktime(datetime.date(datetime.date.today().year,month+1, 1).timetuple())) - 1
print(endTime)

列印結果:
1593532800
1609430399

 

獲取本周上周時間

import datetime
import time

# 本周一的時間
today = datetime.datetime.today()
monday_date = today - datetime.timedelta(today.isoweekday()-1)
t = monday_date.strftime('%Y-%m-%d')
print(t)

# 本周一的時間戳
ts = time.mktime(time.strptime(t,"%Y-%m-%d"))
print(int(ts))

# 上周一的時間
last_monday_date = today - datetime.timedelta(days=today.weekday()+7)
ta = last_monday_date.strftime('%Y-%m-%d')
print(ta)

# 上周一的時間戳
tas = time.mktime(time.strptime(ta,"%Y-%m-%d"))
print(int(tas))

# 上周的結束時間
last_over_date = today - datetime.timedelta(days=today.weekday()+1)
o = last_over_date.strftime('%Y-%m-%d')
print(o)

# 上周的結束時間戳
oa = time.mktime(time.strptime(o,"%Y-%m-%d"))
oat = int(oa)+86400-1
print(oat)

列印結果:
2020-07-13
1594569600
2020-07-06
1593964800
2020-07-12
1594569599

 

獲取今年去年時間

import datetime
import time

today = datetime.datetime.today()

# 今年開始時間
start_year_date = datetime.datetime(today.year, 1, 1)
syd = start_year_date.strftime('%Y-%m-%d')
print(syd)

# 今年開始時間戳
print(int(start_year_date.timestamp()))

# 今年結束時間
end_year_date = datetime.datetime(today.year,12,31,23,59,59)
eyd = end_year_date.strftime('%Y-%m-%d')
print(eyd)

# 今年結束時間戳
print(int(end_year_date.timestamp()))


# 去年開始時間
last_year_date = datetime.datetime(today.year-1, 1, 1)
lyd = last_year_date.strftime('%Y-%m-%d')
print(lyd)

# 去年開始時間戳
print(int(last_year_date.timestamp()))

# 去年結束時間
last_year_end_date = datetime.datetime(today.year, 1, 1,23,59,59) - datetime.timedelta(days=1)
lyed = last_year_end_date.strftime('%Y-%m-%d')
print(lyed)

# 去年結束時間戳
print(int(last_year_end_date.timestamp()))

列印結果:
2020-01-01
1577808000
2020-12-31
1609430399
2019-01-01
1546272000
2019-12-31
1577807999

 

Tags: