python - 日期处理模块
首先就是模块的调用,很多IDE都已经安装好了很多Python经常使用到的模块,所以我们暂时不需要安装模块了。
1
2
3
|
import datetime
import time
import calendar
|
1.获取到此时的准确时间
1
2
|
# 获取此时的时间
print time.localtime()
|
1
2
|
/ / 输出格式为:
time.struct_time(tm_year = 2015 , tm_mon = 12 , tm_mday = 29 , tm_hour = 1 , tm_min = 10 , tm_sec = 25 , tm_wday = 1 , tm_yday = 363 , tm_isdst = 0 )
|
2.获取当天的日期
1
2
3
|
# 获取当天的日期
print datetime.datetime.now()
print datetime.date.today()
|
3.获取昨天的日期
1
2
3
4
5
6
7
8
9
10
|
# 获取昨天的日期
def getYesterday():
today = datetime.date.today()
oneday = datetime.timedelta(days = 1 )
yesterday = today - oneday
print type (today) # 查看获取到时间的类型
print type (yesterday)
return yesterday
yesterday = getYesterday()
print "昨天的时间:" , yesterday
|
4.获取n天以前的日期
这个应该就不用给出代码了吧,稍微想想就可以得出结果了。
5.字符串转换为时间和日期
1
2
3
4
5
6
7
|
# 字符串转换为时间
def strTodatetime(datestr, format ):
return datetime.datetime.strptime(datestr, format )
print time.strftime( "%Y-%m-%d" , time.localtime())
print strTodatetime( "2014-3-1" , "%Y-%m-%d" )
print time.strftime( "%Y-%m-%d %H:%M:%S" , time.localtime())
print strTodatetime( "2005-2-16" , "%Y-%m-%d" ) - strTodatetime( "2004-12-31" , "%Y-%m-%d" )
|
输出结果:
2015-12-29
2014-03-01 00:00:00
2015-12-29 01:10:25
47 days, 0:00:00
6.获取日历相关信息
1
2
3
4
5
6
7
8
9
10
11
12
|
# 获取某个月的日历,返回字符串类型
cal = calendar.month( 2015 , 12 )
print cal
calendar.setfirstweekday(calendar.SUNDAY) # 设置日历的第一天
cal = calendar.month( 2015 , 12 )
print cal
# 获取一年的日历
cal = calendar.calendar( 2015 )
print cal
cal = calendar.HTMLCalendar(calendar.MONDAY)
print cal.formatmonth( 2015 , 12 )
|
7.calendar模块还可以处理闰年的问题
1
2
3
|
# 判断是否闰年、两个年份之间闰年的个数
print calendar.isleap( 2012 )
print calendar.leapdays( 2010 , 2015 )
|
针对Python时间模块datetime\time进行详细探讨。
转义符对应意义如下
- %a 本地简化星期名称
- %A 本地完整星期名称
- %b 本地简化的月份名称
- %B 本地完整的月份名称
- %c 本地相应的日期表示和时间表示
- %d 月内中的一天(0-31)
- %H 24小时制小时数(0-23)
- %I 12小时制小时数(01-12)
- %j 年内的一天(001-366)
- %m 月份(01-12)
- %M 分钟数(00=59)
- %p 本地A.M.或P.M.的等价符
- %S 秒(00-59)
- %U 一年中的星期数(00-53)星期天为星期的开始
- %w 星期(0-6),星期天为星期的开始
- %W 一年中的星期数(00-53)星期一为星期的开始
- %x 本地相应的日期表示
- %X 本地相应的时间表示
- %y 两位数的年份表示(00-99)
- %Y 四位数的年份表示(000-9999)
- %Z 当前时区的名称
- %% %号本身
代码:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import time
import datetime
#两日期相减
d1 = datetime.datetime( 2005 , 2 , 16 )
d2 = datetime.datetime( 2004 , 12 , 31 )
print (d1 - d2).days
#运行时间:
starttime = datetime.datetime.now()
endtime = datetime.datetime.now()
print (endtime - starttime).seconds
#计算当前时间向后10天的时间。
# 如果是小时 days 换成 hours
d1 = datetime.datetime.now()
d3 = d1 datetime.timedelta(days = 10 )
print str (d3)
print d3.ctime()
time.ctime([sec]) #把秒数转换成日期格式,如果不带参数,则显示当前的时间。
>>> import time
>>> time.ctime()
>>> "Wed Jun 14 15:02:50 2006"
>>> time.ctime( 1138068452427683 )
"Sat Dec 14 04:51:44 1901"
>>> import time
>>> time.strftime( "%Y-%m-%d %X" ,time.localtime())
"2011-03-15 20:42:12"
>>> time.strftime( "%Y-%m-%d %H:%M:%S" ,time.localtime())
"2011-03-15 20:03:47"
DateTime模块
----------------------------
datetime 将日期转化为秒
>>> import datetime,time
>>> time.mktime(datetime.datetime( 2009 , 1 , 1 ).timetuple())
1230739200.0
>>> cc = [ 2000 , 11 , 3 , 12 , 43 , 33 ] #Attributes: year, month, day, hour, minute, second
>>> time.mktime(datetime.datetime(cc[ 0 ],cc[ 1 ],cc[ 2 ],cc[ 3 ],cc[ 4 ],cc[ 5 ]).timetuple())
973226613.0
time.time()取得当前时间;
time.localtime()取得本地时间;
time.strftime()格式化日期;
time.strptime(timeString)把字符串转化为日期;
判断输入的日期是星期几
>>> datetime.datetime( 2011 , 02 , 15 ).weekday()
1
>>> datetime.datetime( 2011 , 02 , 15 ).weekday()
1
>>> datetime.datetime( 2011 , 02 , 16 ).weekday()
2
>>> datetime.datetime( 2011 , 02 , 17 ).weekday()
3
>>>
datetime模块获取当前时间
>>> datetime.datetime.utcnow()
datetime.datetime( 2011 , 3 , 15 , 13 , 19 , 32 , 264194 )
>>> datetime.datetime.utcnow().strftime( "%Y-%m-%d %H:%M:%S" ) 格式化
'2011-03-15 13:19:27'
>>>
|
以上就是关于Python时间模块的详细学习,希望对大家学习Python程序设计有所帮助。
转载于:https://www.cnblogs.com/thb-blog/p/7660152.html
python - 日期处理模块相关推荐
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- Python使用re模块进行正则匹配日期和时间
Python使用re模块进行正则匹配日期和时间 目录 Python使用re模块进行正则匹配日期和时间 日期匹配 时间匹配 日期匹配 #导入需要的正则匹配的re包 # Load regex packag ...
- Python学习八:pip 最常用命令、pip升级、pip 清华大学开源软件镜像站、Python日期和时间(Time模块、日历(Calendar)模块)
pip 是 Python 包管理工具,该工具提供了对Python 包的查找.下载.安装.卸载的功能. 注意:Python 2.7.9 + 或 Python 3.4+ 以上版本都自带 pip 工具. p ...
- python 日期和时间处理(time,datetime模块讲解)
在现实生活中,我们常常遇到时间序列任务.所以今天讲解下日期和时间处理. Python 日期时间(datetime) 1.获取当前时间 import datetimedatetime_object = ...
- python时间处理模块 datetime time模块 deltetime模块
1 首先介绍time模块,因为简单 python 自带模块 本人使用time模块,只使用两个函数 time函数和sleep函数 import time a. time.time() 函数 ...
- python时间处理模块datetime+dateutil、numpy时间处理模块datetime64以及pandas时间处理模块Timestamp的演化路径及常用处理接口
python时间处理模块datetime+dateutil.numpy时间处理模块datetime64以及pandas时间处理模块Timestamp及常用处理接口 python时间处理模块dateti ...
- 1、Python 日期时间格式化输出
今天帮朋友写自动化脚本,又需要用格式化日期,又忘记怎么写了,还是写到自己博客里面,方便日后需要的时候看一眼吧.So,临时加一篇 Python 的文章. 1.Python的time模块 import t ...
- python pexpect telnet_使用python的pexpect模块,实现远程免密登录的示例
说明 当我们需要用脚本实现,远程登录或者远程操作的时候,都要去解决如何自动输入密码的问题,一般来说有3种实现方式: 1).配置公钥私钥 2).使用shell下的命令,expect 3).使用pytho ...
- Python中time模块详解
转载自:http://qinxuye.me/article/details-about-time-module-in-python/ 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间 ...
- Python日期字符串到日期对象
本文翻译自:Python date string to date object How do I convert a string to a date object in python? 如何在pyt ...
最新文章
- 【arduino】用VSCode替代Arduino编辑器,arduino VSCode编辑器
- 基于JAVA服务器监控系统设计,远程数字图像监控系统中web服务器的设计与实现...
- python tvtk库_PyCharm创建虚拟环境 和 TVTK库的安装
- pandas重采样时指定对不同列的不同操作
- linux录音命令,安装Linux 录音软件 Record Audio
- LNMP编译安装基于centos7.2
- 2015-2020年各类国际会议与期刊基于图像的三维对象重建论文综述(2)——Encoder stage
- 计算机维修基本题,计算机组装与维护试题库附答案.doc
- python调用vb文件_vb能配合python写程序么?
- vs2015社区版安装包损坏或丢失
- 架构设计——缓存层设计思维导图总结
- Python 音频生成器
- gps网络对时Linux,GPS 网络对时设置 详细版.doc
- hexo博客之yilia主题的个性化设置
- python绘制剖面图_Python气象绘图教程—(十九)剖面图
- Java九十条经验法则之第二条:遇到多个构造器时要考虑使用构建器
- JAVA游戏土行孙_挖掘机的三种形态《非人学园》土行孙快速上手指南
- 常见英语面试问题及答案
- 浪漫是浪漫,不浪漫也是浪漫
- socket.io搭建聊天室