Python日期格式化知识
Python中日期格式化是非常常见的操作,Python 中能用很多方式处理日期和时间,转换日期格式是一个常见的功能。Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。时间间隔是以秒为单位的浮点小数。每个时间戳都以自从格林威治时间1970年01月01日00时00分00秒起经过了多长时间来表示。
注: 以下代码在Python3下运行通过, Python2下未经测试, 如遇问题可以下方留意。
1. 基本方法
获取当前日期:time.time()
获取元组形式的时间戳:time.local(time.time())
格式化日期的函数(基于元组的形式进行格式化):
(1)time.asctime(time.local(time.time()))
(2)time.strftime(format[,t])
将格式字符串转换为时间戳:
time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
延迟执行:time.sleep([secs]),单位为秒
2. 格式化符号
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 当前时区的名称
%% %号本身
3. 常用用法
3.1 将字符串的时间转换为时间戳
方法:
|
1
2
3
4
5
6
7
|
import time t = "2017-11-24 17:30:00"#将其转换为时间数组 timeStruct = time.strptime(t, "%Y-%m-%d %H:%M:%S") #转换为时间戳: timeStamp = int(time.mktime(timeStruct)) print(timeStamp) |
结果:
1511515800
3.2 时间戳转换为指定格式日期
代码:
|
1
2
3
4
|
timeStamp = 1511515800localTime = time.localtime(timeStamp) strTime = time.strftime("%Y-%m-%d %H:%M:%S", localTime) print(strTime) |
结果:
2017-11-24 17:30:00
3.3 格式切换
把-分割格式2017-11-24 17:30:00 转换为斜杠分割格式: 结果:2017/11/24 17:30:00
代码:
|
1
2
3
4
5
6
|
import time t = "2017-11-24 17:30:00"#先转换为时间数组,然后转换为其他格式 timeStruct = time.strptime(t, "%Y-%m-%d %H:%M:%S") strTime = time.strftime("%Y/%m/%d %H:%M:%S", timeStruct) print(strTime) |
结果:
2017/11/24 17:30:00
3.4 获取当前时间并转换为指定日期格式
|
1
2
3
4
5
6
7
|
import time #获得当前时间时间戳 now = int(time.time()) #转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S" timeStruct = time.localtime(now) strTime = time.strftime("%Y-%m-%d %H:%M:%S", timeStruct) print(strTime) |
结果:
2017-11-24 18:36:57
3.5 获得三天前的时间的方法
|
1
2
3
4
5
6
7
8
9
|
import time import datetime #先获得时间数组格式的日期 threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3)) #转换为时间戳: timeStamp = int(time.mktime(threeDayAgo.timetuple())) #转换为其他字符串格式: strTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S") print(strTime) |
结果:
2017-11-21 18:42:52
注:timedelta()的参数有:days,hours,seconds,microseconds
3.6 使用datetime模块来获取当前的日期和时间
|
1
2
3
4
5
6
7
8
9
10
11
|
import datetime i = datetime.datetime.now() print ("当前的日期和时间是 %s" % i) print ("ISO格式的日期和时间是 %s" % i.isoformat() ) print ("当前的年份是 %s" %i.year) print ("当前的月份是 %s" %i.month) print ("当前的日期是 %s" %i.day) print ("dd/mm/yyyy 格式是 %s/%s/%s" % (i.day, i.month, i.year) ) print ("当前小时是 %s" %i.hour) print ("当前分钟是 %s" %i.minute) print ("当前秒是 %s" %i.second) |
Python日期格式化知识的更多相关文章
- python日期格式化符号
python中时间日期格式化符号: %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数( ...
- python日期格式化与绘图
画一个量随着时间变化的曲线是经常会遇到的需求,比如画软件用户数的变化曲线.画随时间变化的曲线主要用到的函数是matplotlib.pyplot.plot_date(date,num).由于其第一个变量 ...
- python 日期格式化常用标记
符号 说明 例子 %a 英文星期的简写 Mon %A 英文星期的完整编写 Monday %b 英文月份的简写 Jun %B 英文月份的完整编写 June ...
- Python 日期格式化 及 schwartzian排序
__author__ = 'root' import datetime import time import copy # 12/Dec/2012:23:59:50 # 12/Sep/2012:23: ...
- Python日期字符串比较
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 需要用python的脚本来快速检测一个文件内的二个时间日期字符串的大小,其实实现很简单,首先一些 ...
- python中时间日期格式化符号
python中时间日期格式化符号: import time print(time.strftime('%Y%H%M%S', time.localtime())) 运行结果: 2016092308 %y ...
- python time模块介绍(日期格式化 时间戳)
import time # 1.time.time() 用于获取当前时间的时间戳, ticks = time.time() print(ticks) # 1545617668.8195682 浮点数 ...
- Python 日期和时间_python 当前日期时间_python日期格式化
Python 日期和时间_python 当前日期时间_python日期格式化 Python程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 cal ...
- 1、Python 日期时间格式化输出
今天帮朋友写自动化脚本,又需要用格式化日期,又忘记怎么写了,还是写到自己博客里面,方便日后需要的时候看一眼吧.So,临时加一篇 Python 的文章. 1.Python的time模块 import t ...
随机推荐
- Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等
对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 -(BOOL) isKindOfClass: classObj 判断是否是这个类或者这个类的子类的实例/ 判断是否是这个类 ...
- HttpClient(一)-- HelloWorld
一.简介 HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的 ...
- VS调试DLL代码使用”附加到进程“
如果一个DLL解决方案,被另一个DLL2解决方案依赖,DLL2被可执行程序exe1引用 如何调试DLL的代码断点呢 1.可以参考另一篇随笔DLL如何调试 2.先运行起来exe1,然后再DLL项目中”调 ...
- [Command] alias - 别名
alias 命令可以让用户使用预置的字符串来执行系统命令. 命令是指用户输入指令指示电脑完成工作.命令一般在命令行输入,以回车键完成输入.命令被传递给shell.shell是类Unix操作系统提供的纯 ...
- mybatis 之引入多个model
配置hessian: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurati ...
- transformNative_libsWithStripDebugSymbolForDebug
local.properties #ndk.dir=C\:\\My_other\\Android_SDK\\ndk-bundle 把NDK注释掉就ok了
- PHP中const和define()定义常量的细节区别
转自:http://www.365mini.com/page/difference-of-define-and-const.htm 众所周知,在PHP中(php 4及以后),我们可以使用函数defin ...
- 关于 ArrayList.toArray() 和 Arrays.asList().toArray()方法
1.ArrayList.toArray() 理解 * 通过源码我们可以看到返回的是Object类型的数组,失去了原有的实际类型,虽然底层存储是具体类型的对象,这也正体现了文档中说的:该方法起到了 ...
- 2015.7.12js-11(DOM基础)
1.childNodes,获取子节点,本身就是一个数组,可以通过下标childNodes[i]来获取某个子节点. alert(obj.childNodes.length); //高级浏览器会有空白节点 ...
- nagios监控报错 It appears as though you do not have permission to view...
今天在安装完nagios后,通过nagios网页界面点击主机.服务.问题页面时.均报错,报错的内容都差不多.如点击服务,报错: It appears as though you do not have ...