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 ...
随机推荐
- __iter__的有无
迭代器和生成器 1.迭代器 我们之前⼀直在⽤可迭代对象进⾏迭代操作. 那么到底什么是可迭代对象.⾸先我们先回顾⼀下⽬前我们所熟知的可迭代对象有哪些: str, list, tuple, dict, s ...
- [Luogu P4143] 采集矿石 [2018HN省队集训D5T3] 望乡台platform
[Luogu P4143] 采集矿石 [2018HN省队集训D5T3] 望乡台platform 题意 给定一个小写字母构成的字符串, 每个字符有一个非负权值. 输出所有满足权值和等于这个子串在所有本质 ...
- 使用concurrent.futures模块并发,实现进程池、线程池
Python标准库为我们提供了threading和multiprocessing模块编写相应的异步多线程/多进程代码 从Python3.2开始,标准库为我们提供了concurrent.futures模 ...
- Linux--安全加固02
目录:1.BIOS2.SSH安全3.禁用telnet4.禁用代码编译5.ProFTP6.TCPwrappers7.创建一个SU组8.root通知9.history安全10.欢迎信息11.禁用所有特殊账 ...
- 深入 Java 调试体系: 第 1 部分,初探JPDA 体系
JPDA(Java Platform Debugger Architecture)是 Java 平台调试体系结构的缩写,通过 JPDA 提供的 API,开发人员可以方便灵活的搭建 Java 调试应用程 ...
- HNOI2018退役记
不想记流水账了,总结一下考炸的原因吧.. $day1$: $12$点才知道$t3$怎么做. 可以用容斥+动态$dp$来搞,但是没时间写了. 事实上这个方法也比较复杂,标算比这优美多了. 所以还是想得太 ...
- C++编译器符号表有哪些内容?
http://blog.csdn.net/wangbingcsu/article/details/48340479 C++编译器符号表有哪些内容? 很早就想写一篇关于符号表的学习小结,可是迟迟不能下笔 ...
- UVa 1412 - Fund Management(状压DP + 预处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [USACO08NOV]Mixed Up Cows
嘟嘟嘟 一看n那么小,那一定是状压dp了(表示从没写过,慌). 首先dp[i][j](i 是一个二进制数,第x位为1代表选了第x头牛),表示 i 这个状态最后一头牛是第 j 头牛时的方案数. 然后当 ...
- Day2 Mybatis初识(二)
mapper接口开发 传统dao的开发问题(ibatis) 方法调用:字符串易错,硬编码 mapper代理开发 a) 编写全局配置 b) 编写接口(自动根据接口和映射文件创建实现类) c) 编写映射文 ...