※ Getting the Current Date and Time 取得目前時間和日期
from datetime import datetime # 必須先import why?
now = datetime.now(); # 宣告一個變數等於時間
print now; # 印出時間
---------------------------------------------------------
Result: 2015-02-11 14:16:30. 782680
__________________________________________
※ Extracting Information 只印出部份需要的時間
from datetime import datetime
now = datetime.now();
current_year = now.year; # 宣告一個值為目前年份
current_month = now.month; # 宣告一個值為目前月份
current_day = now.day; # 宣告一個值為目前日期
print now.year;
print now.month;
print now.day;
---------------------------------------------------------
Result: 2015
02
11
__________________________________________
from datetime import datetime
now = datetime.now()
print '%s/%s/%s' % (now.month, now.day, now.year) # 會依此列的日期格式印出
---------------------------------------------------------
Result: 2/11/2015
__________________________________________
※ Pretty Time 印出時間格式,設:為分隔符號
print '%s:%s:%s' % (now.hour, now.minute, now.second) # 會依此列的時間格式印出
Tips:中間不能有空格會出錯
---------------------------------------------------------
Result: 6:34:45
__________________________________________
※ Grand Finale 合併印出日期和時間
print '%s/%s/%s %s:%s:%s' % (now.month,now.day,now.year,now.hour, now.minute, now.second)
沒有留言:
張貼留言