datetime学习
四、datetime类
(一)、datetime类的数据构成
datetime类其实是可以看做是date类和time类的合体,其大部分的方法和属性都继承于这二个类,相关的操作方法请参阅,本文上面关于二个类的介绍。其数据构成也是由这二个类所有的属性所组成的。
datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
- 1
(二)、专属于datetime的方法和属性
1、 date(…):返回datetime对象的日期部分:
>>> a = datetime.datetime.now()
>>> a
datetime.datetime(2017, 3, 22, 16, 9, 33, 494248)
>>> a.date()
datetime.date(2017, 3, 22)
- 1
- 2
- 3
- 4
- 5
2、time(…):返回datetime对象的时间部分:
>>> a = datetime.datetime.now()
>>> a
datetime.datetime(2017, 3, 22, 16, 9, 33, 494248)
>>> a.time()
datetime.time(16, 9, 33, 494248)
- 1
- 2
- 3
- 4
- 5
3、utctimetuple(…):返回UTC时间元组:
>>> a = datetime.datetime.now()
>>> a
datetime.datetime(2017, 3, 22, 16, 9, 33, 494248)
>>> a.utctimetuple()
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=22, tm_hour=16, tm_min=9, tm_sec=33, tm_wday=2, tm_yday=81, tm_isdst=0)
- 1
- 2
- 3
- 4
- 5
4、combine(…):将一个date对象和一个time对象合并生成一个datetime对象:
>>> a = datetime.datetime.now()
>>> a
datetime.datetime(2017, 3, 22, 16, 9, 33, 494248)
>>>datetime.datetime.combine(a.date(),a.time())
datetime.datetime(2017, 3, 22, 16, 9, 33, 494248)
- 1
- 2
- 3
- 4
- 5
5、now(…):返回当前日期时间的datetime对象:
>>> a = datetime.datetime.now()
>>> a
datetime.datetime(2017, 3, 22, 16, 9, 33,
- 1
- 2
- 3
6、utcnow(…):返回当前日期时间的UTC datetime对象:
>>> a = datetime.datetime.utcnow()
>>> a
datetime.datetime(2017, 3, 22, 8, 26, 54, 935242)
- 1
- 2
- 3
7、strptime(…):根据string, format 2个参数,返回一个对应的datetime对象:
>>> datetime.datetime.strptime('2017-3-22 15:25','%Y-%m-%d %H:%M')
datetime.datetime(2017, 3, 22, 15, 25)
- 1
- 2
8、utcfromtimestamp(…):UTC时间戳的datetime对象,时间戳值为time.time():
>>> datetime.datetime.utcfromtimestamp(time.time())
datetime.datetime(2017, 3, 22, 8, 29, 7, 654272)
- 1
- 2
五、timedelta类
timedelta类是用来计算二个datetime对象的差值的。
此类中包含如下属性:
1、days:天数
2、microseconds:微秒数(>=0 并且 <1秒)
3、seconds:秒数(>=0 并且 <1天)
六、日期计算实操
1.获取当前日期时间:
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2017, 3, 22, 16, 55, 49, 148233)
>>> today = datetime.date.today()
>>> today
datetime.date(2017, 3, 22)
>>> now.date()
datetime.date(2017, 3, 22)
>>> now.time()
datetime.time(16, 55, 49, 148233)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2.获取上个月第一天和最后一天的日期:
>>> today = datetime.date.today()
>>> today
datetime.date(2017, 3, 22)
>>> mlast_day = datetime.date(today.year, today.month, 1) - datetime.timedelta(1)
>>> mlast_day
datetime.date(2017, 2, 28)
>>> mfirst_day = datetime.date(mlast_day.year, mlast_day.month, 1)
>>> mfirst_day
datetime.date(2017, 2, 1)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
3.获取时间差
时间差单位为秒
>>> start_time = datetime.datetime.now()
>>> end_time = datetime.datetime.now()
>>> (end_time - start_time).seconds
7
- 1
- 2
- 3
- 4
差值不只是可以查看相差多少秒,还可以查看天(days), 秒(seconds), 微秒(microseconds).
4.计算当前时间向后8个小时的时间
>>> d1 = datetime.datetime.now()
>>> d2 = d1 + datetime.timedelta(hours = 8)
>>> d2
datetime.datetime(2017, 3, 23, 1, 10, 37, 182240)
- 1
- 2
- 3
- 4
可以计算: 天(days), 小时(hours), 分钟(minutes), 秒(seconds), 微秒(microseconds).
5.计算上周一和周日的日期
today = datetime.date.today()
>>> today
datetime.date(2017, 3, 23)
>>> today_weekday = today.isoweekday()
>>> last_sunday = today - datetime.timedelta(days=today_weekday)
>>> last_monday = last_sunday - datetime.timedelta(days=6)
>>> last_sunday
datetime.date(2017, 3, 19)
>>> last_monday
datetime.date(2017, 3, 13)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
6.计算指定日期当月最后一天的日期和本月天数
>>> date = datetime.date(2017,12,20)
>>> def eomonth(date_object):
... if date_object.month == 12:
... next_month_first_date = datetime.date(date_object.year+1,1,1)
... else:
... next_month_first_date = datetime.date(date_object.year, date_object.month+1, 1)
... return next_month_first_date - datetime.timedelta(1)
...
>>> eomonth(date)
datetime.date(2017, 12, 31)
>>> eomonth(date).day
31
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
7.计算指定日期下个月当天的日期
这里要调用上一项中的函数eomonth(...)
>>> date = datetime.date(2017,12,20)
>>> def edate(date_object):
... if date_object.month == 12:
... next_month_date = datetime.date(date_object.year+1, 1,date_object.day)
... else:
... next_month_first_day = datetime.date(date_object.year,date_object.month+1,1)
... if date_object.day > eomonth(last_month_first_day).day:
... next_month_date = datetime.date(date_object.year,date_object.month+1,eomonth(last_month_first_day).day)
... else:
... next_month_date = datetime.date(date_object.year, date_object.month+1, date_object.day)
... return next_month_date
...
>>> edate(date)
datetime.date(2018, 1, 20)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
8.获得本周一至今天的时间段并获得上周对应同一时间段
>>> today = datetime.date.today()
>>> this_monday = today - datetime.timedelta(today.isoweekday()-1)
>>> last_monday = this_monday - datetime.timedelta(7)
>>> last_weekday = today -datetime.timedelta(7)
>>> this_monday
datetime.date(2017, 3, 20)
>>> today
datetime.date(2017, 3, 23)
>>> last_monday
datetime.date(2017, 3, 13)
>>> last_weekday
datetime.date(2017, 3, 16)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
附录: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 |
当前时区的名称 |
%% |
%号本身 |
datetime学习的更多相关文章
- python datetime学习
Python中处理时间的模块datetime, 这个模块里包含的类也叫datetime,所以要使用需要先import from datetime import datetime 获取当前日期和时间 d ...
- day5模块学习 -- time、datetime时间模块
1.定义 模块:用来从逻辑上组织python(变量,函数,类,逻辑:实现一个功能)代码,本质就是.py结尾的python文件(文件名:test.py,对应的模块名test) 包:用来从逻辑上组织模块的 ...
- python—datetime time 模板学习
写在前面:本人在学习此内容是通过 https://www.cnblogs.com/pycode/p/date.html 文章学习! 时间模块——time python 中时间表示方法有:时间戳_:格式 ...
- Python学习第二阶段Day2,模块time/datetime、random、os、sys、shutil
1.Time. Datetime(常用) UTC时间:为世界标准时间,时区为0的时间 北京时间,UTC+8东八区 import time print(time.time()) # timestamp ...
- python学习道路(day6note)(time &datetime,random,shutil,shelve,xml处理,configparser,hashlib,logging模块,re正则表达式)
1.tiim模块,因为方法较多我就写在code里面了,后面有注释 #!/usr/bin/env python #_*_coding:utf-8_*_ print("time".ce ...
- Python学习总结15:时间模块datetime & time & calendar (二)
二 .datetime模块 1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...
- Python学习总结14:时间模块datetime & time & calendar (一)
Python中的常用于处理时间主要有3个模块datetime模块.time模块和calendar模块. 一.time模块 1. 在Python中表示时间的方式 1)时间戳(timestamp):通常来 ...
- python学习笔记23(时间与日期 (time, datetime包))
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime. time包 time包基于C语言的库函数(library functions).Python的解释器通 ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
随机推荐
- vue-router-7-重定向及别名
const router = new VueRouter({ mode: 'history', base: __dirname, routes: [ { path: '/', component: H ...
- 对编译特性(* ASYNC_REG = “TRUE” *)的理解
(*ASYNC_REG = "TRUE"*)命令用于声明寄存器能够接收相对于时钟源的异步数据,或者说寄存器是一个同步链路上正在同步的寄存器.这条命令可以放在任何寄存器上,除了设置它 ...
- 参数优化-API
网格搜索 对给定参数进行组合,用某标准进行评价,只适合小数据集 class sklearn.model_selection.GridSearchCV(estimator, param_grid, sc ...
- http协议相关
HTTP请求方法 HTTP消息头 HTTP请求头 HTTP响应头 HTTP cookie机制和实现原理 HTTP请求方法 超文本传输协议(HTTP, HyperText Transfer Protoc ...
- Oracle 12c中新建pdb用户登录问题分析
Oracle 12c新建用户登录问题分析1 用sys用户新建用户,提示公用用户名或角色名无效.原因:Oracle 12c中,在容器中建用户(或者应该称为使用者),须在用户名前加c##.默认登录连接的就 ...
- 转--HC05-两个蓝牙模块间的通信
示例蓝牙: 蓝牙A地址:3014:10:271614 蓝牙B地址:2015:2:120758 //============================================= 步骤: 1 ...
- 如何实时查看Linux下日志
以下以Tomcat为例子,其他WEB服务器目录自己灵活修改即可: 1.先切换到:cd usr/local/tomcat5/logs2.tail -f catalina.out3.这样运行时就可以实时查 ...
- vue 手写组件 集合
Num.1 : 链接 向右滑动, 显示删除按钮, 根据touchStart touchEnd 的 clientX 差距 > 30; 说明是向左滑动, 显示; 改变 e.currentTarg ...
- ubantu安装node、npm、cnpm、live-server
更新ubuntu软件源 sudo apt-get update sudo apt-get install -y python-software-properties software-properti ...
- robotframework·WEB项目
date:2018527 day11 一.项目分层 1.测试数据(配置变量,如网址.用户名.密码等) 2.关键字(关键字封装,要调用直接使用关键字名即可,输入内容.点击元素.滚动滑动条等等) 3.测试 ...