リンク
ちゃんと調べるときは
http://www.python.jp/doc/release/
Wiki内検索
最近更新したページ
最新コメント
文字コードの判定 by awesome things!
正規表現 by awesome things!
辞書 by stunning seo guys
文字列の16進変換 by check it out
文字コードの変換 by check this out
デバッガ by check this out
サブプロセス by watch for this
文字コードの判定 by watch for this
サニタイジング by check this out
タグ

日付、時刻の操作

2.3以降では、datetimeを使うらしい…んだけど、使い辛くてしょうがないのでひとまずtimeを使っとく。

現在時刻をとる


>>> 
>>> import time
>>> 
>>> time.time()
1224828269.1137841
>>> 
>>> time.localtime()
(2008, 10, 24, 15, 4, 29, 4, 298, 0)
>>>

日付の相互変換



エポックからの秒数からタプルにする


>>> 
>>> import time
>>> 
>>> time.localtime(1193776802)
(2007, 10, 31, 5, 40, 2, 2, 304, 0)
>>> 

引数なしのlocaltimeでシステム時刻が取れる。

タプルからエポックからの秒数にする


>>> 
>>> time.mktime((2007, 10, 31, 5, 40, 2, 2, 304, 0))
1193776802.0
>>> 

日付の足し引き


>>> 
>>> 1193776802.0 + 24 * 60 * 60
1193863202.0
>>> 

フォーマット


>>> 
>>> time.localtime(1193863202.0)
(2007, 11, 1, 5, 40, 2, 3, 305, 0)
>>> 
>>> time.strftime('%Y/%m/%d %H:%M:%S', (2007, 11, 1, 5, 40, 2, 3, 305, 0))
'2007/11/01 05:40:02'
>>> 
2009年04月04日(土) 14:48:38 Modified by hebi_




スマートフォン版で見る