Python 的格式化時間練習(2)

來源:互聯網
上載者:User

這次添加了一個函數 getTime(),使用者可以輸入自訂日期進行查詢。

 

代碼如下:

showMenuTest.py

 1 # -*- coding:UTF-8 -*- 2  3 def showMenu(): 4     print "Welcome!" 5     print "我能告訴你關於時間的一切,只要你告訴我你想要知道什麼" 6  7     dateDict = {'y' : '兩位元的年份表示(00-99)', 8                 'Y' : '四位元的年份表示(000-9999)', 9                 'm' : '月份(01-12)',10                 'd' : '月內中的一天(0-31)',11                 'H' : '24小時制小時數(0-23)',12                 'I' : '12小時制小時數(01-12)',13                 'M' : '分鐘數(00-59)',14                 'S' : '秒(00-59)',15                 'a' : '本地簡化星期名稱',16                 'A' : '本地完整星期名稱',17                 'b' : '本地簡化的月份名稱',18                 'B' : '本地完整的月份名稱',19                 'c' : '本地相應的日期表示和時間表示',20                 'j' : '年內的一天',21                 'p' : '本地A.M.或P.M.的等價符',22                 'U' : '一年中的星期數(00-53)星期天為星期的開始',23                 'w' : '星期(0-6),星期天為星期的開始',24                 'W' : '一年中的星期數(00-53)星期一為星期的開始',25                 'x' : '本地相應的日期表示',26                 'X' : '本地相應的時間表示',27                 'Z' : '當前時區的名稱' }28 29     for dD_symbol, dD_desc in dateDict.items():30         print dD_symbol, ':', dD_desc31 32 if __name__ == '__main__':33     showMenu()
formatTimeTest.py

 1 # -*- coding:UTF-8 -*- 2  3 import time 4 import showMenuTest 5  6 def getFormat(): 7     return raw_input('Input the format you want: ').split() 8      9 def getTime():10     gTime = raw_input('Input day(yy-mm-dd) ')11     12     if gTime:13         return time.strptime(gTime, "%Y-%m-%d")14     else:15         return time.localtime(time.time())16 17 def main(): 18     showMenuTest.showMenu()19 20     while True:21         formatList = getFormat()22         gotTime    = getTime()23 24         for fmt in formatList:25             if len(fmt) != 1:26                 print "%s : invalid vlaue!" % fmt27                 continue28             else:29                 try:30                     fmt     = '%' + fmt31                     fmtTime = time.strftime(fmt, gotTime)32                     print '%s : %s' % (fmt[1:], fmtTime)33                 except:34                     print "%s : value invalid!" % fmt[1:]35                    36     37 38 if __name__ == '__main__':39     #print getFormat()40     main()41     raw_input()

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.