$python日期和时间的处理
总结一下python中对日期和时间的常用处理方法。
准备
import time,datetime
常用操作
输出当前的日期时间
方式一:
now = time.localtime()
print '【Output】'
print type(now)
print now
print now[:3]
【Output】
<type 'time.struct_time'>
time.struct_time(tm_year=2017, tm_mon=8, tm_mday=21, tm_hour=23, tm_min=15, tm_sec=42, tm_wday=0, tm_yday=233, tm_isdst=0)
(2017, 8, 21)
输出当前时间戳(单位:秒):
print '【Output】'
print time.time()
【Output】
1503329021.99
方式二:
now = datetime.datetime.now()
print '【Output】'
print now.strftime('%Y-%m-%d %H:%M:%S')
【Output】
2017-08-21 23:23:46
格式化输出当前时间
t = time.localtime()
print '【Output】'
print time.strftime('%Y-%m-%d %H:%M:%S',t)
time.sleep(2)
print time.strftime('%Y-%m-%d %H:%M:%S') # 如果不指定时间,输出的就是当前时间
【Output】
2017-08-21 23:17:57
2017-08-21 23:17:59
附:格式化字符串总结
- %a 英文星期简称
- %A 英文星期全称
- %b 英文月份简称
- %B 英文月份全称
- %c 本地日期时间
- %d 日期,1~31
- %H 小时,0~23
- %I 小时,0~12
- %m 月,01~12
- %M 分钟,0~59
- %S 秒,0~59
- %j 年中当天的天数
- %w 星期数,1~7
- %W 年中的第几周
- %x 当天日期,格式:01/31/17
- %X 本地的当天时间
- %y 年份,00~99
- %Y 年份完整拼写
字符串转为日期时间对象
t = time.strptime('2000-1-1 10:00','%Y-%m-%d %H:%M') # 注:前后格式要保持一致,否则转换会出错
print '【Output】'
print type(t)
print t
【Output】
<type 'time.struct_time'>
time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1)
构造datetime对象
dt = datetime.datetime(2010,1,1,23)
print '【Output】'
print type(dt)
print dt
【Output】
<type 'datetime.datetime'>
2010-01-01 23:00:00
将struct_time对象转为时间戳(秒)
now = time.localtime()
timestamp = time.mktime(now)
print '【Output】'
print timestamp
【Output】
1503329307.0
将时间戳(秒)转为struct_time对象
timestamp = 1480000000
print '【Output】'
print time.localtime(timestamp)
【Output】
time.struct_time(tm_year=2016, tm_mon=11, tm_mday=24, tm_hour=23, tm_min=6, tm_sec=40, tm_wday=3, tm_yday=329, tm_isdst=0)
随机推荐
- CENTOS --5分钟搞定Nginx安装的教程
1. 安装gcc(centos 7之后一般已自带,可以在第6步失败后再安装) yum install gcc gcc-c++ 2. 安装pcre yum install -y pcre pcre-de ...
- C语言跳出循环
使用while或for循环时,如果想提前结束循环(在不满足结束条件的情况下结束循环),可以使用break或continue关键字. break关键字 在<C语言switch语句>一节中,我 ...
- JS图片加载时获取图片宽高信息
; var img = new Image(); img.src = node.find("img[class='img1_1']").attr("src"); ...
- docker的本地仓库换成阿里云的镜像仓库
1,阿里云上注册账号,我的已经注册好了,仓库名称:registry.cn-hangzhou.aliyuncs.com/woccb2/chen 2,本地安装docker: yum -y install ...
- android签名,制作key
签名具体步骤: Apk签名首先要有一个keystore的签名用的文件. keystore是由jdk自带的工具keytool生成的.具体生成方式参考一下: 开始->运行->cmd->c ...
- 160614、Eclipse下JRebel6.2.0热部署插件安装、破解及配置
标签: 这两天在做后台管理系统,前端框架用Bootstrap,后端用SpringMVC+Velocity.在开发过程中,经常需要对界面进行微调,调整传参等,每次更改一次java代码,就得重新部署一次, ...
- 透明 Transparent connections through HTTP proxies.
透明语境: 5.7层模型中数据链路层:透明传输: 谈谈如何使用Netty开发实现高性能的RPC服务器 - Newland - 博客园 http://www.cnblogs.com/jietang/p/ ...
- Cookies Client Identification
HTTP The Definitive Guide Cookies are the best current way to identify users and allow persistent se ...
- webpack4学习笔记(三)
webpack打包资源文件 1,打包css文件,先安装css-loader和style-loader npm install --save-dev css-loader style-loader we ...
- D. Mike and Feet---cf548D(最值)
题目链接:http://codeforces.com/problemset/problem/548/D 给你n个数,对于(1,n)长度,让你找到线段的最小值的最大值是多少 #include<io ...