python模块-datetime模块
上面一篇已经讲了time模块,再来学习datetime模块。

datetime主要有datetime、timedelta、time、date这4个子模块。
a、datetime常用的函数(datetime对象)
1、获取当前时间:结果为struct_time
>>> a=datetime.datetime.now()
>>> print a.year,a.hour,a.minute
2017 15 58
2、获取当天时间:结果为struct_time
>>> datetime.datetime.today()
datetime.datetime(2017, 9, 23, 16, 1, 34, 637868)
3、格式化时间戳,结果为struct_time
>>> datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2017, 9, 23, 16, 3, 26, 926928)
4、datetime.datetime.strftime():结果为string_time
>>> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
'2017-09-23 16:10:05'
5、datetime.datetime.strptime(),结果为struct_time
>>> datetime.datetime.now().strptime('2016-10-11','%Y-%m-%d')
datetime.datetime(2016, 10, 11, 0, 0)
6、datetime的所有函数
>>> dt=datetime.datetime.now()
>>> dt.weekday()
5
>>> dt.
dt.combine( dt.min dt.toordinal(
dt.ctime( dt.minute dt.tzinfo
dt.date( dt.month dt.tzname(
dt.day dt.now( dt.utcfromtimestamp(
dt.dst( dt.replace( dt.utcnow(
dt.fromordinal( dt.resolution dt.utcoffset(
dt.fromtimestamp( dt.second dt.utctimetuple(
dt.hour dt.strftime( dt.weekday(
dt.isocalendar( dt.strptime( dt.year
dt.isoformat( dt.time(
dt.isoweekday( dt.timetuple(
dt.max dt.timetz(
dt.astimezone( dt.microsecond dt.today(
b、timedelta,时间段,可以用来获取将来或过去某个时间
timedelta是一个时间段,不是表示一个时间点。所以我们可以把时间段用来加减操作。
比如:获取当天此时,明天此时,昨天此时,上周同一时刻
>>> datetime.datetime.now() + datetime.timedelta(days=0)
datetime.datetime(2017, 9, 23, 16, 21, 50, 308900)
>>> datetime.datetime.now() + datetime.timedelta(days=1)
datetime.datetime(2017, 9, 24, 16, 21, 57, 788849)
>>> datetime.datetime.now() + datetime.timedelta(days=-1)
datetime.datetime(2017, 9, 22, 16, 21, 59, 829977)
>>> datetime.datetime.now() + datetime.timedelta(days=-7)
datetime.datetime(2017, 9, 16, 16, 24, 41, 5906)
>>>
可以通过days、hours、minutes、seconds、microseconds指定天、时、分、秒、微妙来获取将来或过去的某个时间点。
>>> datetime.datetime.now() + datetime.timedelta(days=0)
datetime.datetime(2017, 9, 23, 16, 32, 56, 16910)
>>> datetime.datetime.now() + datetime.timedelta(days=-1,hours=1,seconds=10,minutes=2,microseconds=10)
datetime.datetime(2017, 9, 22, 17, 35, 7, 392881)
>>> datetime.datetime.now() + datetime.timedelta(days=1,hours=1,seconds=10,minutes=2,microseconds=10)
datetime.datetime(2017, 9, 24, 17, 35, 10, 857056)
>>>
c、date,生成一个日期对象,参数为:年、月、日
date(year, month, day) --> date object
>>> datetime.date(2017,9,23)
datetime.date(2017, 9, 23)
d、time,生成一个时间对象,参数为:时、分、秒、微妙
time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object
>>> datetime.time(16,11,11,11)
datetime.time(16, 11, 11, 11)
python模块-datetime模块的更多相关文章
- Python,datetime模块实例
Python的标准模块datetime模块,在我们的工作中应用非常频繁,下面对datetime中常用的方法进行了总结和测试:对每一个方法都使用了单元测试框架Unittest来配合测试. 主要的类型有: ...
- python的datetime模块处理时间
python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...
- 基于Python的datetime模块和time模块源码阅读分析
目录 1 前言 2 datetime.pyi源码分步解析 2.1 头部定义源码分析 2.2 tzinfo类源码分析 2.3 date类源码分析 2.4 time类源码分析 2.5 timedelta ...
- 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块
孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...
- python使用datetime模块计算各种时间间隔的方法
python使用datetime模块计算各种时间间隔的方法 本文实例讲述了python使用datetime模块计算各种时间间隔的方法.分享给大家供大家参考.具体分析如下: python中通过datet ...
- Python模块01/自定义模块/time模块/datetime模块/random模块
Python模块01/自定义模块/time模块/datetime模块/random模块 内容大纲 1.自定义模块 2.time模块 3.datetime模块 4.random模块 1.自定义模块 1. ...
- python中datetime模块
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
- python处理时间--- datetime模块
1 Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...
- Python的datetime模块分析
datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...
- Python全栈之路----常用模块----datetime模块详解
相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...
随机推荐
- matlab操作(整理)
http://blog.csdn.net/ysuncn/article/details/1741828 http://zhan.renren.com/h5/entry/3602888498000464 ...
- 解决qpidd服务安装不上的问题
前几天对一个文件夹命名,忘记了qpidd的路径在这个文件夹的下面,导致后来qpidd用不了. 并且有打开计算机-管理-服务时,看到Advanced MessageQueuing Protocol 未启 ...
- 如何运行vue项目(从gethub上download的开源项目)
前提:入坑vue.js,从GitHub上download一个vue.js的开源项目,发现不知如何在浏览器运行,通过查阅网上教程,发现网上的很多是教你怎么新建项目,并没有一个是教如何打开已有的项目.自已 ...
- HDU 5550 - Game Rooms(DP + 前缀和预处理)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5550 题意: 一个大楼有n(2≤n≤4000)层,每层可以建一个乒乓球房或者一个游泳房,且每种房间在大楼 ...
- BZOJ3786:星系探索(Splay,括号序)
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- Day19 网络编程
基本概念 网络:一组由网线连接起来的计算机. 网络的作用: 1.信息共享. 2.信息传输. 3.分布式处理. 4.综合性的处理. internet:互联网 Internet:是互联网中最大的一个. w ...
- Java反射学习二
利用反射进行对象拷贝的例子 如下例程ReflectTester类进一步演示了Reflection API的基本使用方法. ReflectTester类有一个copy(Object object)方法, ...
- html手机网页自适应宽度
#在head之间加如下代码即可 <meta name="viewport" content="width=device-width, initial-scale=1 ...
- POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48380 Accepted: 18252 ...
- [图解tensorflow源码] Session::Run()流程图 (单机版)