时间无疑是生活各个方面中最关键的因素之一,因此,记录和跟踪时间变得非常重要。在 python 中,可以通过其内置库跟踪日期和时间。今天我们来介绍关于 python 中的日期和时间,一起来了解如何使用time和datetime模块查找和修改日期和时间。
python 中处理日期和时间的模块python 提供了time和datetime模块,可以帮助我们轻松获取和修改日期和时间,下面让我们来逐一了解一下。
time 模块该模块包括使用时间执行各种操作所需的所有与时间相关的功能,它还允许我们访问多种用途所需的时钟类型。
内置函数:
请看下表,它描述了时间模块的一些重要内置功能。
function
description
time()
返回自epoch以来经过的秒数
ctime()
以经过的秒数作为参数,返回当前日期和时间
sleep()
在给定的持续时间内停止线程的执行
time.struct_time class
函数要么将此类作为参数,要么将其作为输出返回
localtime()
以自epoch以来经过的秒数作为参数,并以时间形式返回日期和时间。struct_time格式
gmtime()
与localtime()类似,返回时间。utc格式的struct_time
mktime()
ocaltime()的倒数。获取包含9个参数的元组,并返回自epoch pas输出以来经过的秒数
asctime()
获取包含9个参数的元组,并返回表示相同参数的字符串
strftime()
获取包含9个参数的元组,并根据使用的格式代码返回表示相同参数的字符串
strptime()
分析字符串并及时返回。struct_time格式
代码格式化:
在用示例解释每个函数之前,先看一下所有合法的格式化代码的方式:
code
description
example
%a
weekday (short version)
mon
%a
weekday (full version)
monday
%b
month (short version)
aug
%b
month (full version)
august
%c
local date and time version
tue aug 23 1:31:40 2019
%d
depicts the day of the month (01-31)
07
%f
microseconds
000000-999999
%h
hour (00-23)
15
%i
hour (00-11)
3
%j
day of the year
235
%m
month number (01-12)
07
%m
minutes (00-59)
44
%p
am / pm
am
%s
seconds (00-59)
23
%u
week number of the year starting from sunday (00-53)
12
%w
weekday number of the week
monday (1)
%w
week number of the year starting from monday (00-53)
34
%x
local date
06/07/22
%x
local time
12:30:45
%y
year (short version)
22
%y
year (full version)
2022
%z
utc offset
+0100
%z
timezone
cst
%%
% character
%
struct_time 类具有以下属性:
attribute
value
tm_year
0000, .., 2019, …, 9999
tm_mon
1-12
tm_mday
1-31
tm_hour
0-23
tm_min
0-59
tm_sec
0-61
tm_wday
0-6 (monday is 0)
tm_yday
1-366
tm_isdst
0, 1, -1 (daylight savings time, -1 when unknown)
现在让我们看几个 time 模块的例子。
使用time模块查找日期和时间使用上表中描述的内置函数和格式化代码,可以在 python 中轻松获取日期和时间。
import time
#time
a=time.time() #total seconds since epoch
print(seconds since epoch :,a,end='n----------n')
#ctime
print(current date and time:)
print(time.ctime(a),end='n----------n')
#sleep
time.sleep(1) #execution will be delayed by one second
#localtime
print(local time :)
print(time.localtime(a),end='n----------n')
#gmtime
print(local time in utc format :)
print(time.gmtime(a),end='n-----------n')
#mktime
b=(2019,8,6,10,40,34,1,218,0)
print(current time in seconds :)
print( time.mktime(b),end='n----------n')
#asctime
print(current time in local format :)
print( time.asctime(b),end='n----------n')
#strftime
c = time.localtime() # get struct_time
d = time.strftime(%m/%d/%y, %h:%m:%s, c)
print(string representing date and time:)
print(d,end='n----------n')
#strptime
print(time.strptime parses string and returns it in struct_time format :n)
e = 06 august, 2019
f = time.strptime(e, %d %b, %y)
print(f)
output:
seconds since epoch : 1565070251.7134922
———-
current date and time:
tue aug 6 11:14:11 2019
———-
local time :
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=11, tm_min=14, tm_sec=11, tm_wday=1, tm_yday=218, tm_isdst=0)
———-
local time in utc format :
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=5, tm_min=44, tm_sec=11, tm_wday=1, tm_yday=218, tm_isdst=0)
———–
current time in seconds :
1565068234.0
———-
current time in local format :
tue aug 6 10:40:34 2019
———-
string representing date and time:
08/06/2019, 11:14:12
———-
time.strptime parses string and returns it in struct_time format :
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=218, tm_isdst=-1)
datetime 模块与time模块类似,datetime模块包含处理日期和时间所必需的所有方法。
内置功能:
下表介绍了本模块中的一些重要功能:
function
description
datetime()
datetime 的构造函数
datetime.today()
返回当前本地日期和时间
datetime.now()
返回当前本地日期和时间
date()
以年、月、日为参数,创建相应的日期
time()
以小时、分钟、秒、微秒和tzinfo作为参数,并创建相应的日期
date.fromtimestamp()
转换秒数以返回相应的日期和时间
timedelta()
它是不同日期或时间之间的差异(持续时间)
使用 datetime 查找日期和时间现在,让我们尝试实现这些函数,以使用datetime模块在 python 中查找日期和时间。
import datetime
#datetime constructor
print(datetime constructor:n)
print(datetime.datetime(2019,5,3,8,45,30,234),end='n----------n')
#today
print(the current date and time using today :n)
print(datetime.datetime.today(),end='n----------n')
#now
print(the current date and time using today :n)
print(datetime.datetime.now(),end='n----------n')
#date
print(setting date :n)
print(datetime.date(2019,11,7),end='n----------n')
#time
print(setting time :n)
print(datetime.time(6,30,23),end='n----------n')
#date.fromtimestamp
print(converting seconds to date and time:n)
print(datetime.date.fromtimestamp(23456789),end='n----------n')
#timedelta
b1=datetime.timedelta(days=30, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=4, weeks=8)
b2=datetime.timedelta(days=3, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=4, weeks=8)
b3=b2-b1
print(type(b3))
print(the resultant duration = ,b3,end='n----------n')
#attributes
a=datetime.datetime.now() #1
print(a)
print(the year is :,a.year)
print(hours :,a.hour)
output:
datetime constructor:
2019-05-03 08:45:30.000234
———-
the current date and time using today :
2019-08-06 13:09:56.651691
———-
the current date and time using today :
2019-08-06 13:09:56.651691
———-
setting date :
2019-11-07
———-
setting time :
06:30:23
———-
converting seconds to date and time:
1970-09-29
———-
the resultant duration = -27 days, 0:00:00
———-
2019-08-06 13:09:56.653694
the year is : 2019
hours : 13
以上就是python 日期和时间用法超强总结的详细内容。