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 ...
随机推荐
- Suffix树,后缀树
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 图解前序遍历线索化二叉树,前序线索二叉树遍历,C\C++描述
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- Azulão--青鸟--IPA--巴西葡萄牙语
这是巴西很有名的民谣.
- MicroOrm.Dapper.Repositories 的使用
https://github.com/geffzhang/MicroOrm.Dapper.Repositories 1.特性标记都是要引用: System.ComponentModel.DataAnn ...
- MFC 中GetClientRect、ClientToScreen、GetWindow、RectScreenToClient的使用
CWnd* pWnd = GetDlgItem(IDB_BUT_RECOGNIZE); pWnd->GetClientRect(&rect); //指该控件自身客户区的矩形,原点为控 ...
- 201621123001 《java程序设计》第2周学习总结
1. 本周学习总结 学会String 类的一些用法,比如用subString()截取字符串,频繁对字符串进行修改应使用StringBuilder()等. 学会Array 类的一些用法,比如sort() ...
- 循环神经网络-RNN进阶
这部分许多内容要类比CNN来进行理解和解释,所以需要对CNN比较熟悉. RNN的特点 1. 权值共享 CNN权值共享,RNN也有权值共享,在入门篇可以看到RNN结构图中,权重使用的是同样的字母 为什么 ...
- 软件工程项目程序:WC
1:代码来源:http://yuncode.net/code/c_5087c8e4cd77190 2:Platform:Eclipse Language:Java 3:Bug:暂时没有 4. Func ...
- Linux平台 获取程序依赖文件
创建sh脚本文件 =========================================================== #!/bin/sh exe=$1 #发布的程序名称des=$2 ...
- chromium ②
这篇研究两个问题:chromium对线程的封装和进程通信.主要参考chromium的官方技术文档:Treading和Inter-process Communication (IPC). chrome速 ...