import datetime
print("==============date类================")
#创建一个date对象:datetime.date(year, month, day)
#1、静态方法和字段
# date.max、date.min:date对象所能表示的最大、最小日期;
# date.resolution:date对象表示日期的最小单位。这里是天。
# date.today():返回一个表示当前本地日期的date对象;
# date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象; print(datetime.date(2018,5,3).max)
print('date.max:', datetime.date.max)
print('date.min:', datetime.date.min)
print('date.today():', datetime.date.today())
#print('date.fromtimestamp():', datetime.date.fromtimestamp(time.time())) #time.time()是一个float类型
print('date.fromtimestamp():', datetime.date.fromtimestamp(1525341374.7134962)) #2、方法和属性
d1 = datetime.date(2018,5,3)#date对象
print(d1.year,d1.month,d1.day) #年、月、日;
print(d1.replace(2018, 5, 8)) #生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性。(原有对象仍保持不变)
print(d1.timetuple()) #返回日期对应的time.struct_time对象;
print(type(d1.weekday()),d1.weekday())#返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此类推;
print(type(d1.isoweekday()),d1.isoweekday()) #返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此类推;
print(type(d1.isocalendar()),d1.isocalendar()) #返回格式如(year,month,day)的元组;
print(d1.isoformat()) #返回格式如'YYYY-MM-DD’的字符串;
print(d1.strftime("%Y-%m-%d")) #和time模块format相同。 now = datetime.date(2018, 5, 6)
tomorrow = now.replace(day = 27)
print('now:', now, ', tomorrow:', tomorrow)
print('timetuple():', now.timetuple())
print('weekday():', now.weekday())#0-6表示星期一到星期日
print('isoweekday():', now.isoweekday())#1-7表示星期一到星期日
print('isocalendar():', now.isocalendar())
print('isoformat():', now.isoformat())
print('strftime():', now.strftime("%Y-%m-%d")) print("=============time类============")
#datetime.time(hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ) #1、静态方法和字段
#time类所能表示的最小、最大时间。其中,time.min = time(0, 0, 0, 0), time.max = time(23, 59, 59, 999999);
print("最小值:",datetime.time.min,"最大值:",datetime.time.max)
#时间的最小单位,这里是1微秒;
print(datetime.time.resolution) #2、方法和属性
t1 = datetime.time(10,23,15)#time对象
print(t1.hour,t1.minute,t1.second,t1.microsecond) #时、分、秒、微秒;
print(t1.tzinfo) #时区信息;
#创建一个新的时间对象,用参数指定的时、分、秒、微秒代替原有对象中的属性(原有对象仍保持不变)
#print(t1.replace(,))
print(t1.isoformat()) #返回型如"HH:MM:SS"格式的字符串表示;
print(t1.strftime("%X")) #同time模块中的format; tm = datetime.time(23, 46, 10)
print('tm:', tm)
print('hour: %d, minute: %d, second: %d, microsecond: %d' % (tm.hour, tm.minute, tm.second, tm.microsecond))
tm1 = tm.replace(hour=20)
print('tm1:', tm1)
print('isoformat():', tm.isoformat())
print('strftime()', tm.strftime("%X")) print("=============datetime类=============")
# datetime相当于date和time结合起来。
# datetime.datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
#1、静态方法和字段
# datetime.today():返回一个表示当前本地时间的datetime对象;
# datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;
# datetime.utcnow():返回一个当前utc时间的datetime对象;#格林威治时间
# datetime.fromtimestamp(timestamp[, tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息;
# datetime.utcfromtimestamp(timestamp):根据时间戮创建一个datetime对象;
# datetime.combine(date, time):根据date和time,创建一个datetime对象;
# datetime.strptime(date_string, format):将格式字符串转换为datetime对象;
print('datetime.max:', datetime.datetime.max)
print('datetime.min:', datetime.datetime.min)
print('datetime.resolution:', datetime.datetime.resolution)
print('today():', datetime.datetime.today())
print('now():', datetime.datetime.now())
print('utcnow():', datetime.datetime.utcnow())
print('fromtimestamp(tmstmp):', datetime.datetime.fromtimestamp(1525345689.8390145))
print('utcfromtimestamp(tmstmp):', datetime.datetime.utcfromtimestamp(1525345689.8390145))
#2、方法和属性
dt=datetime.datetime.now()#datetime对象
print(dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second,dt.microsecond,dt.tzinfo)
print(dt.date()) #获取date对象;
print(dt.time()) #获取time对象;
print(dt.replace(year=2017))
print(dt.timetuple())
print(dt.utctimetuple())
print(dt.toordinal())
print(dt.weekday())
print(dt.isocalendar())
#print(dt.isoformat ([ sep] ))
print(dt.ctime()) #返回一个日期时间的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));
print(dt.strftime("%Y-%m-%d")) print("==========timedelta类,时间加减===========")
#使用timedelta可以很方便的在日期上做天days,小时hour,分钟,秒,毫秒,微妙的时间计算,如果要计算月份则需要另外的办法。
dt = datetime.datetime.now()
#日期减一天
dt1 = dt + datetime.timedelta(days=-1)#昨天
dt2 = dt - datetime.timedelta(days=1)#昨天
dt3 = dt + datetime.timedelta(days=3)#明天
print(type(dt1),type(dt2),type(dt3),dt1,dt2,dt3)
print(datetime.timedelta(days=3))
delta_obj = dt3-dt
print(type(delta_obj),delta_obj)#<class 'datetime.timedelta'> 3 days, 0:00:00
print(delta_obj.days ,delta_obj.total_seconds())#1 86400.0

Python【datetime】模块的更多相关文章

  1. python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客

    python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客     python datetime模块strptime/strptime form ...

  2. python datetime模块参数详解

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...

  3. Python datetime模块的介绍

    datetime模块常用的主要有下面这四个类:(要清楚import datetime : 导入的是datetime这个包,包里有各种类) 1. datetime.date   用于表示年月日构成的日期 ...

  4. python——datetime模块

    一.datetime模块介绍 (一).datetime模块中包含如下类: 类名 功能说明 date 日期对象,常用的属性有year, month, day time 时间对象 datetime 日期时 ...

  5. Python datetime模块的datetime类

    datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day. datetime.time:表示时间的类.常用的属性有hour, m ...

  6. python datetime模块详解

    datetime是python当中比较常用的时间模块,用于获取时间,时间类型之间转化等,下文介绍两个实用类. 一.datetime.datetime类: datetime.datetime.now() ...

  7. python datetime模块

    该模块的时间有限时限:1 - 9999 dir(datetime)  from datetime import datetime, timedelta, timezone dt = datetime. ...

  8. python datetime模块用strftime 格式化时间

    1 2 3 #!usr/bin/python import datetime datetime.datetime.now() 这个会返回 microsecond.因此这个是我们不需要的.所以得做一下修 ...

  9. python datetime模块来获取当前的日期和时间

    #!/usr/bin/python # -*- coding: UTF- -*- import datetime i = datetime.datetime.now() print ("当前 ...

  10. python datetime模块用法

    1. 创建naive(无时区信息)的datetime对象 import datetime dt_utc = datetime.datetime.utcnow() dt_utc # datetime.d ...

随机推荐

  1. 软件工程-东北师大站-第十二次作业(PSP)

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  2. 第四节 Linux目录文件及文件基本操作

    一.Linux目录结构 Linux 的目录与 Windows 的目录的区别: 一种不同是体现在目录与存储介质(磁盘,内存,DVD 等)的关系上,以往的 Windows 一直是以存储介质为主的,主要以盘 ...

  3. ### Error building SqlSession.

    org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession.### The error may e ...

  4. Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索

    题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ...

  5. Oracle 11g R2 for Win7旗舰版(64位)- 安装

    1.下载Oracle 11g R2 for Windows的版本                                   下载地址:http://www.oracle.com/techne ...

  6. 【CS231N】1、图像分类

    一.知识点 1. 计算机识别物体面临的困难 视角变化(Viewpoint variation):同一个物体,摄像机可以从多个角度来展现. 大小变化(Scale variation):物体可视的大小通常 ...

  7. 【搜索】POJ-3050 基础DFS

    一.题目 Description The cows play the child's game of hopscotch in a non-traditional way. Instead of a ...

  8. mvc学习-编辑提交需要注意-mvc重点

    示例代码: // GET: /Movies/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpSt ...

  9. 交换机、linux光衰查询

    RX收光,TX发光 一.交换机 命令: display interface transceiver brief 结果: ...... HW6851 10GE1/0/15 transceiver dia ...

  10. Jenkins权限控制-Role Strategy Plugin插件使用

    Role Strategy Plugin插件可以对构建的项目进行授权管理,让不同的用户管理不同的项目,将测试和生产环境分开. 具体配置方法如下(操作需要管理员用户权限). Jenkins版本:1.64 ...