python日期函数_python 时间相关函数

python 中与时间处理相关的模块包括 time、datetime、以及 calendar

time 模块

time() 函数:time() 函数用于返回当前时间的时间戳(1970年01月08时00分00秒到现在的浮点秒数)

time() 函数的语法:time.time() //此语句中的第一个 time 指的是 time 模块,该函数参数列表为空

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 print('当前时间的时间戳:%f' % time.time())

View Code

localtime([secs]) 函数

localtime() 函数的作用是格式化时间戳为本地 struct_time。如果 secs 参数未输入则默认为当前时间

localtime() 函数语法:time.localtime([secs]) //time指的是 time 模块,可选参数 secs 表示 1970-1-1 到现在的秒数

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 print('time.localtime():', time.localtime())4 #输出:

5 #time.localtime(): time.struct_time(tm_year=2018, tm_mon=4, tm_mday=17, tm_hour=15, tm_min=20, tm_sec=12, tm_wday=1, tm_yday=107, tm_isdst=0)

6 #最后三个输出的值的含义为 一周的第几日 0 ~ 6(0 是周一),一年中的第几日,夏令时(-1, 0, 1, -1)

View Code

gmtime([secs]) 函数

gmtime() 函数用于将一个时间戳转换成 UTC 时区(0 时区)的 struct_time,可选参数 secs 表示 1970-1-1 到现在的秒数,若 secs 参数未输入则默认为当前时间

gmtime() 函数的语法: time.gmtime([secs])

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 print('time.gmtime():', time.gmtime())4 #输出:

5 #time.gmtime(): time.struct_time(tm_year=2018, tm_mon=4, tm_mday=17, tm_hour=7, tm_min=32, tm_sec=54, tm_wday=1, tm_yday=107, tm_isdst=0)

View Code

mktime(t) 函数

mktime() 函数用于执行与 gmtime()、localtime() 相反的操作,接收 struct_time 对象作为参数,返回用秒表示时间的浮点数。如果输入不合法的时间则触发 OverflowError 或 ValueError 异常

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 t = (2018, 4, 17, 15, 20, 12, 1, 107, 0);4 print('time.mktime(t): %f' %time.mktime(t))5 #输出:

6 #time.mktime(t): 1523949612.000000

View Code

asctime([t]) 函数

asctime() 函数用于接收时间元组(struct_time)并返回一个可读形式为 Tue Apr 17 15:45:13 2018 的 24 个字符的字符串

部输出参数 t 则默认为当前时间点

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 t =time.localtime()4 print('time.asctime(t): %s' %time.asctime(t))5 #输出:

6 #time.asctime(t): Tue Apr 17 15:45:13 2018

View Code

ctime([secs]) 函数

ctime 函数用于将一个时间戳转化为 time.asctime() 的形式。若为指定参数 secs 则默认将 time.time() 作为参数

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 print('time.ctime(): %s' %time.ctime())4 #输出:

5 #time.ctime(): Tue Apr 17 15:51:00 2018

View Code

sleep(secs) 函数

sleep() 函数用于推迟调用线程的运行,可通过参数 secs 指定进程挂起的时间

sleep() 函数语法:time.sleep(escs) //其中 time 是指 time 模块,secs 指推迟执行的秒数,此函数没有返回值

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 print('start: %s' %time.ctime())4 time.sleep(5)5 print('end: %s' %time.ctime())6 #输出:

7 #start: Tue Apr 17 16:00:16 2018

8 #end: Tue Apr 17 16:00:21 2018

View Code

clock() 函数

clock() 函数用于以浮点数计算的秒数返回当前 cpu 时间,用来衡量不同程度程序的耗时。该函数在不同的系统上含义不同。在 UNIX 系统上,返回的是 “进程时间”,是用秒表示的浮点数(时间戳)。在 Windows 中,第一次调用返回的是进程运行的实际时间,第二次之后的调用返回的是自第一次调用后到现在的运行时间

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 defprocedure():4 time.sleep(2)5

6 #measure process time

7 procedure()#sleep的时间不算程序运行的时间,sleep()是没有占用cpu

8 t1 =time.clock()9 print(t1)#程序运行的时间

10 procedure()11 print('seconds process time:', time.clock())#第一次调用clock到本次调用clock的时间间隔

12

13 #measure wall time

14 t2 =time.time()15 procedure()16 print('seconds wall time:', time.time() -t2)17 #输出:

18 #0.0

19 #seconds process time: 2.0004229494329526

20 #seconds wall time: 2.000399351119995

View Code

strftime(format[, t]) 函数

用于接收时间 struct_time 元组,并返回可读字符串表示的当地时间,格式由参数 format 决定

time 指的是 time 模块,format 指格式化字符串,t 指可选的参数,是一个 struct_time 对象

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 t = (2016, 9, 25, 17, 50, 38, 6, 48, 0)4 t = time.mktime(t)#将 struct_time 转换成浮点秒数

5 print(time.strftime('%b %d %Y %H : %M : %S', time.gmtime(t)))#gmtime()将时间戳转换成0时区struct_time

6 #输出:

7 #Sep 25 2016 09 : 50 : 38

View Code

strptime(string[, format]) 函数

strptime 函数用于根据指定的格式把一个时间字符串解析成时间元组 struct_time,和 strftime 的作用刚好相反

其中 time 指的是 time 模块,string 指时间字符串,format 指格式化字符串

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importtime2

3 struct_time = time.strptime("25 Sep 16", "%d %b %y")4 print(struct_time)5 #输出:

6 #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=269, tm_isdst=-1)

View Code

datetime 模块

today()

语法:datetime.datetime.today() //其中 datetime.datime 指的是 datetime.datime 类

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importdatetime2

3 print('today is:', datetime.datetime.today())4 #输出:

5 #today is: 2018-04-19 19:38:59.625746

View Code

now([tz])

语法: datetime.datetime.now([tz]) // 后去 tz 参数所指时区的本地时间

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importdatetime2

3 print('now is:', datetime.datetime.now())4 #输出:

5 #now is: 2018-04-19 19:42:43.731264

View Code

datetime.utcnow()

语法:datetime.datetime.utcnow() //返回一个当前 utc 时间的 datetime 对象

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importdatetime2

3 print('utcnow is:', datetime.datetime.utcnow())4 #输出:

5 #utcnow is: 2018-04-19 11:46:16.801027

View Code

fromtimestmap(timestamp[, tz])

根据时间戳创建一个 datetime 对象。其中 tz 指定时区信息。返回一个 datetime 对象

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importdatetime, time2

3 print('fromtimestamp is:', datetime.datetime.fromtimestamp(time.time()))4 #输出:

5 #fromtimestamp is: 2018-04-19 19:50:01.279816

View Code

utcfromtimestamp(timestamp)

根据时间戳创建一个 datetime 对象。其中,timestamp 指时间戳,返回一个 datetime 对象

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importdatetime, time2

3 print('utcfromtimestamp is:', datetime.datetime.utcfromtimestamp(time.time()))4 #输出:

5 #utcfromtimestamp is: 2018-04-19 11:54:30.934395

View Code

strptime(date_string, format)

将格式字符串转换成 datetime 对象

其中 date_string 指日期字符串,format 为格式化方式,返回一个 datetime 对象

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importdatetime2

3 dt =datetime.datetime.now()4 print('strptime is:', dt.strptime(str(dt), '%Y-%m-%d %H:%M:%S.%f'))5 #输出:

6 #strptime is: 2018-04-19 20:07:49.020946

View Code

strftime(format)

将格式化字符串转换为 datetime 对象

其中 format 为格式化方式

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importdatetime2

3 dt =datetime.datetime.now()4 print('strftime is:', dt.strftime('%Y-%m-%d %H:%M:%S'))5 #输出:

6 #strftime is: 2018-04-19 20:11:17

View Code

calendar 模块(日历)

calendar.calendar(year, w = 2, l = 1, c = 6)

该函数返回一个多行字符串格式的 year 年历,3 个月一行,间隔距离为 c。每日宽度间隔为 w 字符。每行长度为 21 * w + 18 + 2 * c。l 是每星期行数

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importcalendar2

3 print(calendar.calendar(2018, w = 2, l = 1, c = 6))4 '''

5 输出:6 20187

8 January February March9 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su10 1 2 3 4 5 6 7 1 2 3 4 1 2 3 411 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 1112 15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 1813 22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 2514 29 30 31 26 27 28 26 27 28 29 30 3115

16 April May June17 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su18 1 1 2 3 4 5 6 1 2 319 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 1020 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 1721 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 2422 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 3023 3024

25 July August September26 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su27 1 1 2 3 4 5 1 228 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 929 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 1630 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 2331 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 3032 30 3133

34 October November December35 Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su36 1 2 3 4 5 6 7 1 2 3 4 1 237 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 938 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 1639 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 2340 29 30 31 26 27 28 29 30 24 25 26 27 28 29 3041 3142 '''

View Code

calendar.isleap(year)

如果是闰年就返回 True,否则返回 False

代码:

ContractedBlock.gif

ExpandedBlockStart.gif

1 importcalendar2

3 print(calendar.isleap(2018))4 #输出:

5 #False

View Code

calendar.leapdays(y1, y2)

返回 y1, y2 之间的闰年总数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值