python日期和時間

來源:互聯網
上載者:User

標籤:time()   時間   end   日期格   技術分享   gpo   class   sds   字元   

time 模組下有很多函數可以轉換常見日期格式。如函數time.time()用於擷取目前時間戳, 例:#!/usr/bin/python# -*- coding: UTF-8 -*-import time;  # 引入time模組ticks = time.time()print "目前時間戳為:", ticks輸出為目前時間戳為: 1459994552.51 擷取目前時間#!/usr/bin/python# -*- coding: UTF-8 -*-import timelocaltime = time.localtime()print "本地時間為 :", localtime 輸出為本地時間為 : time.struct_time(tm_year=2018, tm_mon=3, tm_mday=12, tm_hour=22, tm_min=21, tm_sec=51, tm_wday=0, tm_yday=71, tm_isdst=0)  擷取格式化的時間#!/usr/bin/python# -*- coding: UTF-8 -*-import timelocaltime = time.asctime()print "本地時間為 :", localtime 輸出為本地時間為 : Mon Mar 12 22:24:06 2018 例 #!/usr/bin/python# -*- coding: UTF-8 -*- import time # 格式化成2016-03-20 11:45:39形式print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 格式化成Sat Mar 28 22:24:24 2016形式print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # 將格式字串轉換為時間戳記a = "Sat Mar 28 22:24:24 2016"print time.mktime(time.strptime(a, "%a %b %d %H:%M:%S %Y")) 輸出為2018-03-12 22:25:29Mon Mar 12 22:25:29 20181459175064.0  擷取某月日曆 Calendar模組有很廣泛的方法用來處理年曆和月曆,例如列印某月的月曆:#!/usr/bin/python# -*- coding: UTF-8 -*-import calendarcal = calendar.month(2016, 1)print "以下輸出2016年1月份的日曆:"print cal;          

python日期和時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.