「Python」isleap()で年がうるう年かどうかを判定する方法

2020年12月7日

説明
calendar.isleap(year)
year が閏年なら True を、そうでなければ False を返します。
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# coding: utf-8
#!/usr/bin/python3
import calendar
import datetime
hh = datetime.datetime(2020, 11, 12, 8, 20, 35)
print(hh)
print(calendar.isleap(hh.year))
cft = datetime.date(2020, 9, 25)
print(cft)
print(calendar.isleap(cft.year))
# coding: utf-8 #!/usr/bin/python3 import calendar import datetime hh = datetime.datetime(2020, 11, 12, 8, 20, 35) print(hh) print(calendar.isleap(hh.year)) cft = datetime.date(2020, 9, 25) print(cft) print(calendar.isleap(cft.year))
# coding: utf-8

#!/usr/bin/python3
 
import calendar
import datetime

hh = datetime.datetime(2020, 11, 12, 8, 20, 35)
print(hh)

print(calendar.isleap(hh.year))

cft = datetime.date(2020, 9, 25)
print(cft)

print(calendar.isleap(cft.year))

実行結果
>python 101.py
2020-11-12 08:20:35
True
2020-09-25
True

Python

Posted by arkgame