今天在弄个自动关机小脚本的时候,遇到了时间转换的问题,也难怪,以前没学过,
不能怪我不会哦!
首先,先学会打印出当前时间的几种方式
参考开源社区: http://my.oschina.net/u/1032854/blog/198179#OSC_h1_3
菜鸟编程: http://www.runoob.com/python/python-date-time.html
1. time.ctime()函数—输出固定的日期格式
from time import ctime # 该函数只能以 " 星期 月 日 时:分:秒 年 " 的格式显示日期
print ctime()
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdgAAAAWCAIAAADsLhq+AAACQ0lEQVR4nO2cQW7EIAxFc5buu+35OVoXlUYpxOZjJ4FE761ag78dGH/NtNJsX98/GwAATAQjBgCYDEYMADCZPyMupZRS9vE2ohPIzZQLU3Y4GzLisbrdLEshptzNTd6O31XmFjLPC7AQKxjxlHGa4mjdukMNV+lh5W7uPnKFsrI6qgzwJKYbcWbIw1Sj205yZryd3G7drmy3aFi5expKG0PKmVvIPC/AcuhG7EzF9n8SRi3AiuS70nGm/VLBzFldp1xF/EsZYvR+h4IAT6Uy4vaHP0rDXqRdPcUCRIMYLaq04cfDmqL1OIL+FWSUnYh/KTpV52LPehDgqShGfDg/4q8+/igqq7G6laCVlTcdK3h4zkpuxYnKVq5VaJRLe461BLAKuhHvs/RVn4xypm6VYiVmJlyxj+62rn57GgFlP7cY+O353Z7Vc6YlgIUYekd8+Lp3BqxLRjlT12rjMB4QtHJP7Hk77zSUc7buSNcPn4YTbJvUuwJYCIzYyb1B8KwS+VsQc0cbru40UPeGkwSYTOBvxBXVqr/5MNeK+Mr5ut3czGw7glf0fMNp7FcVWVEqdguZ5wVYjr0Rt3y2OUtWulLdkhKVw3W7T2S1p+Nrxur6udcpKwrOZkf8np4BVufzXRP7l/Lhy1qZlmL/y+WQdqcVsZRjdatcKysz3l3ZWF3xFk5XVhR8TUv/hp4BVudNX/rDQALAI3mNEfPOCACeyqON2P/MCwDwDN5kxLPbAQAI8WgjBgB4AxgxAMBkMGIAgLn8AoU5LSdTMzbfAAAAAElFTkSuQmCC" alt="" />
2. time.localtime()函数—同time.ctime()都是按固定顺序输出日期
from time import localtime # 也是以固定顺序输出—"年 月 日 时:分:秒"
print "%d/%d/%d %d:%d:%d" % localtime()[0:6]
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgcAAAAUCAIAAACWHglwAAABj0lEQVR4nO3WW27DMAxEUe9/VV5afwKjlqzR8GEUbe/5aiwNpTIAneMAAAAAAOX8Jrp6bUhU1kUqWX0rp1q6G07xxLlO2dx3BAA35yS3Gq38uLk9K1KiVK4bfuXouV139msC+KeGeRH6eMiJs83Om1uy+lZasRtm8WKvotl0NwDgOO5DdjVwRUQ/dA71z9VLLXMw0Q2/YH2bn+WtACDj8YfnvOEx5TwUh26vER399TmY64ZTs74nmuWtACBsHr7Xx/mPObh6KGb6Nj4ws9slR283Qqu5/6iSBYDR40ypzMGusf7S/NXauxFaTfSqmAWAm9U0EZPRfHg9948QNwxl03Ow2A2neO4Cfv10nwFAzaD6WyFRTVwylM3NwXo3nPpd2/wsbwUAFv2zdFgN/Q51snr+5rL+hu25iVuZRzRWrnxHADA6F8SGVRGnuJOqZ/0920Mbu6FX3+uzcysA+NjOwWGPKLKt76fqWXOPOPGNbujVl/rs3AoAflhlSDHgAOCv4a0AAAAAAAAAAAAAAL/VF5Kzc0w2AOFxAAAAAElFTkSuQmCC" alt="" />
3. datetime.datetime—输出固定的日期格式
【不过是精确到微秒10的-6方秒】
from datetime import datetime # 以年-月-日 时:分:秒的格式显示
# print datetime.now()
print datetime.today()
4. time.strftime()函数—同上
from time import strftime # 格式化成 2016-08-31 10:53:42的形式
print strftime("%Y-%m-%d %H:%M:%S")
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhIAAAAXCAIAAADRDTFyAAACK0lEQVR4nO3cTU7DMBCG4ZyFPVvOXzXtghYJuBG7KPXP5/FMEhTrfVbFscf2LGaogpjuz9+394/ptC6v/vs4ADC6+9e52wYA4FC3k3/bAAAc6vb8oW0AAKzmB20DAGCWtA39ern58rn5yPfiOngqd+RJ3sgY3Ldvc1UtgjsbzSWRPAMYx7pt2OtRb7XSa7XIqbaKbI9pWes+c74wWR7PRv45HhnAUJa2kZSDrh8nWSiba4XgqdyR9Y3swffIRnNTR+RJto1gZABDmR/ftXcboo4UR7oGjfS+xlO59zom4FZtIzhfJzZ4EgDjqLWN4i+Y+YTiKstgca9kU8u+lkMWIxsPGamPxmzYt7BfpytsPt+SK3daAJxbsW2I8p1/yBfWBmvlRlRDy76ijFrqrC6CkfpoyUZzWvGom9+olvbe4ADGN3+mbaNYFw4odkXNfd2RixHEARx0ihyZLM7RCe86VTOZ7iQDGMf1tW3oeqFH9OAy3lV69L6RyPqQ4gC9MZuDm2+xftSV55w7IIBhrduGqAvGYrd5odRrDyjB520bkcj0DABVS9vQdSF5WpssBptrHfvuF1nfyOhE2dAnj4QCMJo5axuJZaZ4lMypjeu1NXrtfpH1jezxt903ng3LjZI5edhIWgCcW/5tw1iVitGaj3zlxlIH94g87dM2gvsGs2G5UTLnUqGDABjTNftLKgAAqmgbAIAOtA0AQAfaBgCgA20DANBB/AdcAAASf5o1qtRKLoGiAAAAAElFTkSuQmCC" alt="" />
当然,你也可以这样格式化日期
from time import strftime # 格式化成 Wed Aug 10:58:25 2016 形式
print strftime("%a %b %H:%M:%S %Y")
另一种格式
在这里,我们要用到 "年/月/日 时:分:秒" 的格式, 所以就选择后者time.strftime()!
-|---->-----------------------------------------------------------------------------------------------------<<<---
在开始前,有必要给大家介绍下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)
1. 字符串(string)转化为datetime.datetime
这里我们用到datetime.datetime.strptime()
# 字符串转化为datetime.datetime
import datetime str = "2016/8/31" # Y:(Year)年, m:(month)月, d:(day)日
dt = datetime.datetime.strptime(str, "%Y/%m%d")
pritn dt
str 2 datetime
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcwAAAAXCAIAAAAJN+iJAAABYUlEQVR4nO3WQXLDIAwFUO5/qhytm0ybBCOwCAnuvLdKsf0RzEjTUgAA4NpuD84+/X0hkby0qnRyCU80k3zF25hJBkp57qK6l8afnk1eV9W7kscz96l5n2SglIcuSvxZwpHU/XZdVenk+ER71rxnMtBU91XraWIxUUO6qvReucB1NV8xGTj28t/K0iF7qwTfBi+0Xj5MHiwyMU3WDawrJgMHgmFX/6g/bC22Jl0wCkf2PfywmxwctnuiWFzzG29yt2RgyOHQmW/gF6eKCfZNJx8mBAWcDWzVPH+T2yYDfa1xUy+ebeCXjh1s0XjfmeS4yKCAwajWyuBN/ptk4E8wpAbbbHBxz+Y3ZFckA3fBhK2ftl4OFrvfJvZdlxyfaM+a90wG7m4NwQutkJHwmcI+kxyfaHXNuZv8bjIQqbso7rQgpJs/U9snk0t2yHaT193kF5MBAAAAAAAAAAAAAACAt/gBqU362+zJdcIAAAAASUVORK5CYII=" alt="" />
2. datetime.datetime转化为时间戳
大部分语言里的时间戳是从格林尼治的1970年1月1日子夜(0点)到现在的总毫秒数,不过在python里则
是总秒数,由于我们是东八区,所以对我们来说是从8点开始算起的,要查看起始时间,可以用上面说
到的time.ctime(0)函数! ! !
# datetime.datetime 转化为时间戳 # str转化为datetime.datetime
str = "2016/8/31"
dt = datetime.datetime.strptime(str, "%Y/%m/%d") # datetime.datetime转化为时间戳
time.mktime(dt.timetuple())
datetime 2 timestamp
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeYAAAAYCAIAAACgHslFAAABLUlEQVR4nO3WS47DIBAFwNz/VBxtthbETUObjBJVrWz6Y5TFU14vAAAAAAD4Iq211lr+/Frt3DVMB68925tPV2OVWYC5TH6Nz+NsJnbjwWlDZvPRaqwyC5ASR8z1fBpDY3P8unrJ/OYTr5kb7s0CrKlH9jShtiO7uPmp6uolRTZwSjGyk6k6/gmd/htd3RzfWWQDv+BtvkzzLj7vGsZV09Te2Dzuf7twfJhWY5VZgDXT4AuyNc6mIJGnPXubgwuLbOAXHIrsTF5PL7C0uTt/+3r3ibi6en+RDZySyZe7nvh8O/L2NldCWWQD3+HxyI7zuqveNT++ufIaq8wCrDkX2Z2g4TObK9XM7xDPAjwgEzHPBmvXk/9ifXOlener5CwAAAAAAAAAAAAAAAD/4Q9Gu8y9r0NWuQAAAABJRU5ErkJggg==" alt="" />
3. 时间戳转化为字符串
# 时间戳转化为字符串 import time
import datetime # str转为datetime.datetime
str = "2016/8/31"
dt = datetime.datetime.strptime(str, "%Y/%m/%d") # str 转为时间戳
time_stamp = time.mktime(dt.timetuple()) # 时间戳转为字符串
str = time.strftime("%Y/%m/%d", time.localtime(time_stamp)) print str
timestamp 2 str
也许大家看了上面的介绍会觉得好像也就这样,弄不出点什么好玩的东东,那你就错了,还记得前面说的自动关机的脚本么 ?
下一篇就带大家实现这个功能!