13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

201612のように、日付はともかく先月の値をとりたかったので書いたコード。

# ライブラリのインポート
import datetime

# 今日を取得
today = datetime.datetime.today()
print today.strftime("%Y-%m-%d")

# 当月1日の値を出す
thismonth = datetime.datetime(today.year, today.month, 1)
print thismonth.strftime("%Y-%m-%d")

# 前月末日の値を出す
lastmonth = thismonth + datetime.timedelta(days=-1)
print lastmonth

# フォーマットを指定して、年月だけ拾う
print lastmonth.strftime("%Y%m")

ちょっと手間なやり方ですが、AWS Lambdaで使えるデフォルトのライブラリだけでやりたかったので。

13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?