网站首页 > 技术文章 正文
Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。
时间间隔是以秒为单位的浮点小数。
每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。
Python 的 time 模块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳, 如下实例:
实例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
# 引入time模块
ticks = time.time()
print "当前时间戳为:", ticks
以上实例输出结果:
当前时间戳为: 1459994552.51
时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年。
什么是时间元组?
很多Python函数用一个元组装起来的9组数字处理时间:
序号 | 字段 | 值 |
0 | 4位数年 | 2008 |
1 | 月 | 1 到 12 |
2 | 日 | 1到31 |
3 | 小时 | 0到23 |
4 | 分钟 | 0到59 |
5 | 秒 | 0到61 (60或61 是闰秒) |
6 | 一周的第几日 | 0到6 (0是周一) |
7 | 一年的第几日 | 1到366 (儒略历) |
8 | 夏令时 | -1, 0, 1, -1是决定是否为夏令时的旗帜 |
上述也就是struct_time元组。这种结构具有如下属性:
序号 | 属性 | 值 |
0 | tm_year | 2008 |
1 | tm_mon | 1 到 12 |
2 | tm_mday | 1 到 31 |
3 | tm_hour | 0 到 23 |
4 | tm_min | 0 到 59 |
5 | tm_sec | 0 到 61 (60或61 是闰秒) |
6 | tm_wday | 0到6 (0是周一) |
7 | tm_yday | 1 到 366(儒略历) |
8 | tm_isdst | -1, 0, 1, -1是决定是否为夏令时的旗帜 |
获取当前时间
从返回浮点数的时间戳方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。
实例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time l
ocaltime = time.localtime(time.time())
print "本地时间为 :", localtime
以上实例输出结果:
本地时间为 : time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=3, tm_sec=27, tm_wday=3, tm_yday=98, tm_isdst=0)
获取格式化的时间
你可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime():
实例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
localtime = time.asctime( time.localtime(time.time()) )
print "本地时间为 :", localtime
以上实例输出结果:
本地时间为 : Thu Apr 7 10:05:21 2016
格式化日期
我们可以使用 time 模块的 strftime 方法来格式化日期,:
time.strftime(format[, t])
python中时间日期格式化符号:
- %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 当前时区的名称
- %% %号本身
获取某月日历
Calendar模块有很广泛的方法用来处理年历和月历,例如打印某月的月历:
实例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import calendar cal = calendar.month(2016, 1)
print "以下输出2016年1月份的日历:"
print cal
以上实例输出结果:
以下输出2016年1月份的日历:
January 2016
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Time 模块
Time 模块包含了以下内置函数,既有时间处理的,也有转换时间格式的:
序号 | 函数及描述 |
1 | time.altzone |
2 | time.asctime([tupletime]) |
3 | time.clock( ) |
4 | time.ctime([secs]) |
5 | time.gmtime([secs]) |
6 | time.localtime([secs]) |
7 | time.mktime(tupletime) |
8 | time.sleep(secs) |
9 | time.strftime(fmt[,tupletime]) |
10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') |
11 | time.time( ) |
12 | time.tzset() |
Time模块包含了以下2个非常重要的属性:
序号 | 属性及描述 |
1 | time.timezone |
2 | time.tzname |
日历(Calendar)模块
此模块的函数都是日历相关的,例如打印某月的字符月历。
星期一是默认的每周第一天,星期天是默认的最后一天。更改设置需调用calendar.setfirstweekday()函数。模块包含了以下内置函数:
序号 | 函数及描述 |
1 | calendar.calendar(year,w=2,l=1,c=6) |
2 | calendar.firstweekday( ) |
3 | calendar.isleap(year) 是闰年返回 True,否则为 False。
|
4 | calendar.leapdays(y1,y2) |
5 | calendar.month(year,month,w=2,l=1) |
6 | calendar.monthcalendar(year,month) |
7 | calendar.monthrange(year,month) |
8 | calendar.prcal(year,w=2,l=1,c=6) |
9 | calendar.prmonth(year,month,w=2,l=1) |
10 | calendar.setfirstweekday(weekday) |
11 | calendar.timegm(tupletime) |
12 | calendar.weekday(year,month,day) |
其他相关模块和函数
在Python中,其他处理日期和时间的模块还有:
- datetime模块
- pytz模块
- dateutil模块
猜你喜欢
- 2024-11-20 Python基础编程——算术运算
- 2024-11-20 Python字符串方法之-字符串填充
- 2024-11-20 Python第14题:最长公共前缀【leetcode】
- 2024-11-20 从 0 到 1:构建强大且易用的规则引擎
- 2024-11-20 分享3个干货满满的Python实战项目,点赞收藏
- 2024-11-20 python笔记8:静静一起来学习-字符串相关方法
- 2024-11-20 零基础Python完全自学教程14:Python中的序列知识详解
- 2024-11-20 你需要了解的最重要的Python概念
- 2024-11-20 Python整数缓存机制
- 2024-11-20 老鸟进阶必备技能,看懂显示器参数
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)