Python で昨日の日付を取得

Document 对象参考手册Python3 サンプル

次のコードは datetime モジュールをインポートして昨日の日付を取得します:

# Filename : test.py # author by : www.example.com # datetime モジュールをインポート import datetime def getYesterday(): today=datetime.date.today() oneday=datetime.timedelta(days=1) yesterday=today-oneday return yesterday # 出力 print(getYesterday())

上記のコードを実行すると、出力結果は次のようになります:

2015-06-10

上記のサンプルの出力は、昨日の日付が 2015 年 6 月 10 日であることを意味します。

Document 对象参考手册Python3 サンプル

その他の拡張