python-整理--时间模块
使用datetime模块处理时间
###########################################################################
#
# datetime模块
#
########################################################################### # 导入datetime类,取了别名DateTime(和C#一样的)
from datetime import datetime as DateTime # 当前时间,按2016-02-05 13:01:01的格式输出
print(DateTime.now().strftime('%Y-%m-%d %H:%M:%S')) # 当前日期,按2016年02月15格式输出
print(DateTime.now().date().strftime('%Y{y}%m{m}%d').format(y='年', m='月')) # 将字符串转为DateTime.第二个参数是字符串的格式,这个格式要匹配进行转换的字符串
date='2016/02/23'
print(type(DateTime.strptime(date,'%Y/%m/%d')))
print(DateTime.strptime(date,'%Y/%m/%d')) ## datetime类的一些属性
# 最小日期 0001-01-01 00:00:00
print(DateTime.min) # 最大日期 9999-12-31 23:59:59.999999
print(DateTime.max) # 在比较时间时间隔时,能比较出的时间差别. 0:00:00.000001(1微秒)
print(DateTime.resolution) ## datetime对象的一些属性
curr=DateTime.now() # 时间区域信息,没有就显示None
print(curr.tzinfo) # 年2016 月2 日5 时1 分12 秒23 微秒123456(1000000)
print(curr.year)
print(curr.month)
print(curr.day)
print(curr.hour)
print(curr.minute)
print(curr.second)
print(curr.microsecond) ## 时间的操作
# 时间差
t1=DateTime.strptime('2016-02-05 12:20:00','%Y-%m-%d %H:%M:%S')
t2=DateTime.strptime('2016-02-06 12:00:00','%Y-%m-%d %H:%M:%S') # 时间相减的结果是datetime.timedelta类
diff=t1-t2 # <class 'datetime.timedelta'>
print(type(diff)) # -1 day, 0:20:00
print(diff) # 相差天数 相差秒数 相差微秒数
print(diff.days) # -1
print(diff.seconds) #
print(diff.microseconds) # # 还有很多属性和方法在python3.4文档上.
python-整理--时间模块的更多相关文章
- python的时间模块
python有两个重要的时间模块,分别是time和datetime 先看time模块 表示时间的几种方法: 1)时间元组:time.struct_time(tm_year=2016, tm_mon ...
- Python—day17时间模块、系统模块、递推遍历、序列化
一.time'''时间戳(timestamp):time.time()延迟线程的运行:time.sleep(secs)(指定时间戳下的)当前时区时间:time.localtime([secs])(指定 ...
- 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码
本文转载自脚本之家,源网址为:https://www.jb51.net/article/147429.htm 一.Python中日期时间模块datetime介绍 (一).datetime模块中包含如下 ...
- 【13】python time时间模块知识点备查
表示时间的三种形式 # 时间模块 '''UTC(世界协调时间):格林尼治天文时间,世界标准时间,在中国来说是UTC+8DST(夏令时):是一种节约能源而人为规定时间制度,在夏季调快1个小时 时间的表示 ...
- Python之时间模块、random模块、json与pickle模块
一.时间模块 1.常用时间模块 import time # 时间分为三种格式 #1.时间戳---------------------以秒计算 # start= time.time() # time.s ...
- python(时间模块,序列化模块等)
一.time模块 表示时间的三种方式: 时间戳:数字(计算机能认识的) 时间字符串:t='2012-12-12' 结构化时间:time.struct_time(tm_year=2017, tm_mon ...
- python time时间模块
在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串 (1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00: ...
- Python基础-时间模块和radom模块
时间模块 import time # 引入时间模块 print(time.time()) # 1508146954.9455004: 时间戳 print(time.clock()) # 计算CPU执行 ...
- Python的时间模块小结(转自:不懂真人)
import datetimeprint time.time() #时间戳 print time.localtime(time.time()) #时间元组 print time.strftime('% ...
- python(29)----时间模块
time模块 1. 三种时间表现形式 时间戳(timestamp) 格式化的时间字符串 元祖/结构化时间(struct_time) 2. 时间戳(timestamp) 通常来说,时间戳表示的是从197 ...
随机推荐
- 单片微机原理P3:80C51外部拓展系统
外部拓展其实是个相对来说很好玩的章节,可以真正开始用单片机写程序了,比较重要的是外部存储器拓展,81C55拓展,矩阵键盘,动态显示,DAC和ADC. 0. IO接口电路概念与存储器拓展 1. 为什 ...
- Linux下安装并启动MongDB
1.下载MongoDB 下载链接: http://www.mongodb.org/downloads 2.解压下载的压缩包 tar -zxvf mongodb-linux-x86_64-3.2.8.t ...
- 手把手教你清除WIN7的C盘垃圾
WIN7系统用着用着C盘会变得越来越大,可用空间变得越来越小,磁盘清理,和安全卫士怎么清也清不出这些系统深度的垃圾.我们可以手动删除,释放C盘空间. 这样一清理下来,结果我的C盘就释放了近10个GB的 ...
- httpcomponents-client-4.3.6 HttpPost的简单使用
/** * httpcomponents-client-4.3.6 * @author y */ public class HttpUtil { public static String httpPo ...
- Xs and Os Referee
Xs and Os Referee 1 def checkio(game_result): 2 winner = 'D' 3 4 for row in game_result: 5 if row[0] ...
- unix c 07
进程的结束函数 (exit._Exit) exit 并不是立即退出,退出前执行 用atexit/on_exit函数 注册的函数. exit(int status)中的status可以用 w ...
- Android应用开发学习之状态栏通知
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 状态栏通知涉及到两个类,一是Notification,它代表一个通知:另一个是NotificationManager ...
- hdu 5344 MZL's xor(数学之异或)
Problem Description MZL loves xor very much.Now he gets an array A.The length of A ≤i,j≤n) The xor ...
- linux文件权限整理
网上对linux文件权限的已经很多,不过还是要自己整理一下,不然每次都要查资料. linux下所有东西都是文件,包括设备,所以这里的文件也包括文件夹. 先是查看文件权限:ls -lh xzc@xzc- ...
- linux下部署svn服务器
系统Linux debian 2.6.32-5-686 先安装svn工具:apt-get install subversion,耐心等待安装完成.安装完成后svn客户端.服务器都有了. 接者建立svn ...