python日期

来源:互联网 发布:路老膏方 诈骗让网络 编辑:程序博客网 时间:2024/05/23 13:37

转换日期格式是一个常见的例行琐事。
Python有一个time and calendar模组可以帮忙。

获取当前时间Unix time

从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数。
打开cmd查看现在的Unix time。

import time;  # This is required to include time module.ticks = time.time()print ticks   #1442477018.57

1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年某日。

获取当前时间-年月日

import time;localtime = time.localtime(time.time()) #time.time()是当前时间的时间戳,也可以直接输入某个时间戳1443688070print "Local current time :", localtime

输出

Local current time : time.struct_time(tm_year=2013, tm_mon=7, tm_mday=17, tm_hour=21, tm_min=26, tm_sec=3, tm_wday=2, tm_yday=198, tm_isdst=0)

unix time转当前时间

time.localtime(某个unix time)

格式化的时间

最简单的获取可读的时间模式的函数是asctime():

import time;localtime = time.asctime( time.localtime(time.time()) )print "Local current time :", localtime#输出:Local current time : Tue Jan 13 10:17:09 2009

datetime

0 0