1
1

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)

Posted at

毎年、年越しギリギリに煩悩を無くすなんて無計画だと思いませんか?
今ならまだ間に合います。これを期に計画的に煩悩をなくしましょう。

Step.1 今日の日付を取得

まずは今年があと何日残っているか調べます。
そのために今日の日付を取得しましょう。

import datetime

これをインポートしておけば日付、時刻関連の関数を使うことができます。

datetime.date.today()

今日の日付をdate型で得る関数はこれですね。

import datetime
print(datetime.date.today())

これを実行すると

2025-02-23

上手くいきましたね。

Step.2 今年の大晦日の日付から今年の残り日数を取得

次に大晦日の日付から今日の日付を引くことで今年の残り日数を取得します。

datetime.date(year,month,day)

この関数で指定した日付をdate型で取得できます。
たとえば

import datetime
print(datetime.date(2020,1,1))

なら

2020-01-01

となります。そして今年の西暦をint型で取得する関数

datetime.date.today().year

を組み合わせることで

import datetime
print(datetime.date(datetime.date.today().year,12,31))
2025-12-31

今年の大晦日の日付をdate型で取得できます。
この関数からStep.1の関数を引くことで

import datetime
thisyear=(datetime.date(datetime.datetime.today().year,12,31)-datetime.date.today()).days
print(f"今年は残り{thisyear}日です")
今年は残り311日です

となります。

Step.3 残り日数を108で割る

あとは残り日数を108で割ることで一日に何個煩悩を消せば年明けに間に合うか計算できます。

bonnou.py
import datetime
thisyear=(datetime.date(datetime.datetime.today().year,12,31)-datetime.date.today()).days
print(f"今年は残り{thisyear}日です")
print(f"1日{thisyear/108}個煩悩をなくせば間に合います、頑張りましょう。")

年は残り311日です
1日2.8796296296296298個煩悩をなくせば間に合います、頑張りましょう。

ばっちりですね。
皆様も計画的に煩悩をなくしていきましょう。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?