python 中 time 有三種格式: float, struct tuple(time.struct_time 或 datetime.datetime), str 常用的: float --> struct tuple: time.localtime( float ) struct time t ...
python 中 time 有三種格式:
float,
struct tuple(time.struct_time 或 datetime.datetime),
str
常用的:
float --> struct tuple: time.localtime( float )
struct time tuple --> str: time.strftime(format, struct time tuple)
str --> struct time tuple: time.strptime(str, format)
struct time tuple --> float : time.mktime(struct time tuple)
struct time tuple --> datetime: datetime(*time_tuple[0:6])
float --> datetime: datetime.datetime.fromtimestamp( float )
datetime --> str: datetime.strftime(format, datetime)
str --> datetime: datetime.strptime(str, format)
datetime --> struct time tuple: datetime.timetuple()
Note:
time 是 float 為基礎,小數點後是毫秒,整數部分是秒。(Java 是毫秒,所以,python_time*1000 == Java_time)
datetime 是int, 略去了毫秒部分。datetime tuple 少於 struct_time
1. 當前時間
>>> import time >>> time.time() 1450681042.751 >>> time.localtime(time.time()) time.struct_time(tm_year=2015, tm_mon=12, tm_mday=21, tm_hour=15, tm_min=0, tm_sec=2, tm_wday=0, tm_yday=355, tm_isdst=0) >>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) '2015-12-21 15:01:28' >>> from datetime import datetime >>> datetime.now() datetime.datetime(2015, 12, 21, 14, 58, 38, 279000) >>> datetime.today() datetime.datetime(2015, 12, 21, 14, 59, 20, 204000) >>> now = datetime.now() >>> now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond >>> now.isocalendar() # 2015年 第52周 星期一 (2015, 52, 1) >>> now.isoweekday() # 星期幾,1:Monday; 而 now.weekday() 返回值從0開始 1
2. 日期字元串--> 日期
>>> s='2015-12-21 15:01:28' >>> timeTuple = datetime.strptime(s, '%Y-%m-%d %H:%M:%S') datetime.datetime(2015, 12, 21, 15, 1, 28) >>> datetime.datetime.strftime('%Y/%m/%d %H:%M:%S', timeTuple)
>>> s='2015-12-21 15:01:28' >>> timeTuple = datetime.strptime(s, '%Y-%m-%d %H:%M:%S') time.struct_time(tm_year=2015, tm_mon=12, tm_mday=21, tm_hour=15, tm_min=1, tm_sec=28, tm_wday=0, tm_yday=355, tm_isdst=-1) >>> time.strftime('%Y/%m/%d %H:%M:%S',timeTuple)
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. 時間戳
>>> time.mktime(time.strptime(s,'%Y-%m-%d %H:%M:%S')) 1450681288.0 >>> int(time.time())
# timestamp to time tuple in UTC timestamp = 1226527167.595983 time_tuple = time.gmtime(timestamp) print repr(time_tuple)
# timestamp to time tuple in local time timestamp = 1226527167.595983 time_tuple = time.localtime(timestamp) print repr(time_tuple)
2. 日期相加減
import datetime now = datetime.datetime.now() # datetime.datetime(2015, 12, 16, 15, 6, 37, 420000) dayOfweek = datetime.datetime.isoweekday() if dayOfweek == 1: # Monday last_time = now + datetime.timedelta(days=-3) else: last_time = now + datetime.timedelta(days=-1)轉:http://www.cnblogs.com/snow-backup/p/5063665.html