Python【datetime】模块
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】模块的更多相关文章
- python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客
python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客 python datetime模块strptime/strptime form ...
- python datetime模块参数详解
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...
- Python datetime模块的介绍
datetime模块常用的主要有下面这四个类:(要清楚import datetime : 导入的是datetime这个包,包里有各种类) 1. datetime.date 用于表示年月日构成的日期 ...
- python——datetime模块
一.datetime模块介绍 (一).datetime模块中包含如下类: 类名 功能说明 date 日期对象,常用的属性有year, month, day time 时间对象 datetime 日期时 ...
- Python datetime模块的datetime类
datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day. datetime.time:表示时间的类.常用的属性有hour, m ...
- python datetime模块详解
datetime是python当中比较常用的时间模块,用于获取时间,时间类型之间转化等,下文介绍两个实用类. 一.datetime.datetime类: datetime.datetime.now() ...
- python datetime模块
该模块的时间有限时限:1 - 9999 dir(datetime) from datetime import datetime, timedelta, timezone dt = datetime. ...
- python datetime模块用strftime 格式化时间
1 2 3 #!usr/bin/python import datetime datetime.datetime.now() 这个会返回 microsecond.因此这个是我们不需要的.所以得做一下修 ...
- python datetime模块来获取当前的日期和时间
#!/usr/bin/python # -*- coding: UTF- -*- import datetime i = datetime.datetime.now() print ("当前 ...
- python datetime模块用法
1. 创建naive(无时区信息)的datetime对象 import datetime dt_utc = datetime.datetime.utcnow() dt_utc # datetime.d ...
随机推荐
- 关于手机端h5上传图片配合exif.min.js,processImg.js的使用
首先这里有个new FileReader()的概念,这是h5新增的,用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API,使用该API可以在浏览器主线程中异步访问文件 ...
- 【探路者】Postmortem会议(“事后诸葛亮”会议)
[探路者]Postmortem会议(“事后诸葛亮”会议) 整理:米赫 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的贪吃蛇游戏主要将完成一个 ...
- 使用switchPage.js插件jQuery全屏滚动翻页
1. 先引入jquery.js,再引入switchPage.js 文件地址:点击打开链接 <script src="jquery.min.js"></script ...
- Sprint计划表
Sprint会议计划 一.Sprint 需求 准备环节:小组成员利用周六周日在网上查阅Android开发的教程,练习开发一些简单的小程序,具备一定的开发能力,在电脑上搭建Android开发环境,做好 ...
- JS学习:JavaScript的核心
分享到 分类 JS学习 发布 ourjs 2013-12-02 注意 转载须保留原文链接,译文链接,作者译者等信息. 作者: JeremyWei 原文: JavaScript The ...
- Android自定义View实现仿QQ实现运动步数效果
效果图: 1.attrs.xml中 <declare-styleable name="QQStepView"> <attr name="outerCol ...
- Scrum Meeting Beta - 6
Scrum Meeting Beta - 6 NewTeam 2017/12/5 地点:主南201 任务反馈 团队成员 完成任务 计划任务 安万贺 完成了离线状态本地存储的读取Issue #133Pu ...
- Android Holo Theme的三种表现形式
摘录自:http://blog.csdn.net/xyz_lmn/article/details/12000941 Holo Theme的三种表现形式 Holo Theme是android4.0开始提 ...
- H5实现的时钟
源码如下: <!doctype html> <html> <head></head> <body> <canvas id=" ...
- macOS & SVN
macOS & SVN mac 下已经自带了svn环境; 使用 svn –version 查看版本号 安装方法: 已安装 XCode,只需要在 code > Preferences &g ...