python格式化日期時間的函式為datetime.datetime.strftime();由字串轉為日期型的函式為:datetime.datetime.strptime(),兩個函式都涉及日期時間的格式化字串,列舉如下:
%a abbreviated weekday name
%a full weekday name
%b abbreviated month name
%b full month name
%d day of month as decimal number (01 - 31)
%hhour in 24-hour format (00 - 23)
%i hour in 12-hour format (01 - 12)
%j day of year as decimal number (001 - 366)
%mmonth as decimal number (01 - 12)
%mminute as decimal number (00 - 59)
%p current locale's a.m./p.m. indicator for 12-hour clock
%ssecond as decimal number (00 - 59)
%u week of year as decimal number, with sunday as first day of week (00 - 51)
%w weekday as decimal number (0 - 6; sunday is 0)
%w week of year as decimal number, with monday as first day of week (00 - 51)
%x date representation for current locale
%x time representation for current locale
%y year without century, as decimal number (00 - 99)
%y year with century, as decimal number
%z, %z time-zone name or abbreviation; no characters if time zone is unknown
%% percent sign
示例:
#!/usr/bin/python
# it-homer in 2013
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import datetime
def format_time():
t = datetime.datetime.now()
print(t) # 2013-11-20 09:36:51.198680
t = datetime.datetime.now().strftime('%y-%m-%d %h:%m:%s')
print(t) # 2013-11-20 09:39:16
t = datetime.datetime.now().strftime('%b-%d-%y %h:%m:%s')
print(t) # nov-20-13 09:36:51
t = datetime.datetime.now().strftime('%b-%d-%y %h:%m:%s')
print(t) # nov-20-2013 09:36:51
# weekday
t = datetime.datetime.now().strftime('%a %a %u %w %w')
print(t) # wed wednesday 46 46 3
# month
t = datetime.datetime.now().strftime('%b %b')
print(t) # nov november
# day
t = datetime.datetime.now().strftime('%d %j')
print(t) # 20 324
# date and time for locale
t = datetime.datetime.now().strftime('%c')
print(t) # wed nov 20 10:15:49 2013
# hour
t = datetime.datetime.now().strftime('%h %l')
print(t) # 10 10
# a.m/p.m
t = datetime.datetime.now().strftime('%p')
print(t) # am
t = datetime.datetime.now().strftime('%x')
print(t) # 11/20/13
t = datetime.datetime.now().strftime('%x')
print(t) # 10:23:36
t = datetime.datetime.now().strftime('%x %x')
print(t) # 11/20/13 10:24:47
t = datetime.datetime.now().strftime('%z')
print(t) #
t = datetime.datetime.now().strftime('%z')
print(t) #
# 字串轉換成datetime
t = datetime.datetime.strptime('nov-20-13 09:42', '%b-%d-%y %h:%m')
print(t) # 2013-11-20 09:42:00
t = datetime.datetime(2013, 11, 20, 9, 42)
print(t) # 2013-11-20 09:42:00
# datetime轉換成字串
t = datetime.datetime.now().strftime('%b-%d-%y %h:%m:%s')
print(t) # nov-20-13 10:26:40
if __name__ == "__main__":
format_time()
執行結果:2013-11-20 10:29:26.456640
2013-11-20 10:29:26
nov-20-13 10:29:26
nov-20-2013 10:29:26
wed wednesday 46 46 3
nov november
20 324
wed nov 20 10:29:26 2013
10 10
am11/20/13
10:29:26
11/20/13 10:29:26
2013-11-20 09:42:00
2013-11-20 09:42:00
nov-20-13 10:29:26
10 時間相加
問題描述 輸入兩個時間a和b,分別都由3個整數組成,分別表示時分秒,比如,假設a為34 45 56,就表示a所表示的時間是34小時 45分鐘 56秒。輸出a b即兩個時間相加後的結果。輸入說明 輸入資料由6個整數ah,am,as,bh,bm,bs組成,分別表示時間a和b所對應的時分秒。題目保證所有的...
LINUX學習筆記10 時間程式設計
a 需要包涵標頭檔案 include b 時間型別 1.t time null d 時間轉化 b time t t 定義時間變數 3.儲存至tm結構 struct tm 日光節約時間 4.將tm格式的時間轉化為字串,便於顯示 a char asctime const struct tm tm 5.將...
python學習22 時間模組
5.datetime 世間表示的幾種形式 方法 說明time.asctime tupletime 接受時間元組並返回乙個可讀的形式為 tue dec 11 18 07 14 2008 2008年12月11日 周二18時07分14秒 的24個字元的字串。time.ctime secs 作用相當於asc...