1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【python】wwから日付を逆算

Posted at

2024年が月曜始まりだったせいで2025年で泥沼にはまった。
week 01 ,2025 => 2024-12-30
にしてほしかったという話。
まあ2025年なのに2024年出てくるのはキモいって話はあるのですけど、要望なのでしゃーない。

before

from datetime import datetime, date
ww = "2025-W01"
tar_date = datetime.strptime(ww + '-1', "%Y-W%W-%w").date() 
# datetime.date(2025, 1, 6)

isoのカレンダーウィークとズレる。

after

from datetime import datetime, date
ww = "2025-W01"
new_ww = ww.replace('W','').split('-')
tar_date = date.fromisocalendar(int(new_ww[0]), int(new_ww[1]), 1)
# datetime.date(2024, 12, 30)

isoのカレンダーウィークと一致。

参考

https://docs.python.org/ja/3/library/datetime.html#datetime.date.isoformat
https://qiita.com/Butterthon/items/a2f7bd3cc1cb4f7528d7#38%E3%81%A7%E8%BF%BD%E5%8A%A0%E3%81%95%E3%82%8C%E3%81%9Ffromisocalendar

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?