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 ...
随机推荐
- 解决windows 10英文版操作系统中VS2017控制台程序打印中文乱码问题
当您在windows 10英文版的操作系统中运行Vs2017控制台应用程序时,程序可能无法正常显示中文,中文都变成了乱码.这是由于大部分中文程序所使用的文字编码与Windows 英文系统的文字编码不同 ...
- November 23rd 2016 Week 48th Wednesday
I always like walking in the rain, so no one can see me crying. 我一直喜欢在雨中行走,那样就没人能看到我的眼泪. I like walk ...
- python3 装饰器全解
本章结构: 1.理解装饰器的前提准备 2.装饰器:无参/带参的被装饰函数,无参/带参的装饰函数 3.装饰器的缺点 4.python3的内置装饰器 5.本文参考 理解装饰器的前提:1.所有东西都是对象( ...
- Connection:Keep-alive
名词解释: HTTP无状态:无状态是指协议对于事务处理没有记忆能力,服务器不知道客户端是什么状态.从另一方面讲,打开一个服务器上的网页和你之前打开这个服务器上的网页之间没有任何联系 如果你要实现一个购 ...
- Centos7.3 坑爹网络配置
1.目的: 我想在Vmvare配置成NAT模式的上网,因为这个模式宿主机可以访问虚拟机,虚拟机也可以访问宿主机,但仅主机模式只能是宿主机能访问虚拟机,但虚拟机不能访问宿主机.所以我为了能在虚拟机里面上 ...
- Cocos2D-x-3.0 编译(Win7)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/logotostudent/article/details/25425969 第一次開始用手游引擎挺激 ...
- 在CentOS7上部署 Kubernetes集群
yum -y install etcd docker flannel kubenetes 一般会遇到没有k8s源的问题,先 yum update -y 看是否有效,如果还是没用就创建yum 源,再 ...
- 1001. [BJOI2006]狼抓兔子【最小割】
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...
- HBase学习之路 (十)HBase表的设计原则
建表高级属性 下面几个 shell 命令在 hbase 操作中可以起到很大的作用,且主要体现在建表的过程中,看 下面几个 create 属性 1. BLOOMFILTER 默认是 NONE 是否使用布 ...
- virtualbox+vagrant学习-2(command cli)-7-vagrant login命令
Login ⚠️该命令已经弃用了,别名为vagrant cloud auth login.看本博客的 格式: vagrant cloud auth login [options] 登录命令用于使用Ha ...