2013-06-08 12 views

答えて

29

タイムスタンプをフォーマットするには、time.strftime()を使用します。最初time.gmtime()またはtime.localtime()のいずれかを使用して、時間のタプルに変換します

time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(file))) 
1
from datetime import datetime 
from os.path import getmtime 

datetime.fromtimestamp(getmtime(file)).strftime('%m/%d/%Y') 
+2

ようこそスタックオーバーフローへ!このコードは質問に答えるかもしれませんが、問題の内容とコードがどのように問題に取り組むかを記述する方が良いでしょう。将来については、いくつかの情報、スタックオーバーフローで素晴らしい答えを解く方法:http://stackoverflow.com/help/how-to-answer – dirtydanee

0

はあなたが言及ようctimeの列strを使用してDateTimeオブジェクトを作成し、任意の形式の文字列に戻ってそれをフォーマットすることができます。

str1 = time.ctime(os.path.getmtime(file)) # Fri Jun 07 16:54:31 2013 
datetime_object = datetime.strptime(str1, '%a %b %d %H:%M:%S %Y') 
datetime_object.strftime("%m/%d/%Y") # 06/07/2013 

あなたはエポック

クレジットからのタイムゾーンは+絶対タイムスタンプに対処する必要はありません。この方法:Converting string into datetime

リンク:How to get file creation & modification date/times in Python?

http://strftime.org/

関連する問題