前提条件: 日時モジュール
Python では、日付と時刻はそれ自体のデータ型ではありませんが、DateTime という名前のモジュールをインポートして、日付と時刻を操作することができます。 Datetime モジュールは Python に組み込まれているため、外部にインストールする必要はありません。の 日時モジュール 現在の日付と時刻を取得するいくつかの関数を提供します。
date.today() を使用して現在の日付を取得します。
DateTime モジュールの date クラスの today() メソッドは、今日の日付の値を含む日付オブジェクトを返します。
構文: date.today()
戻り値: 現在のローカル日付を返します。
例:
Python3
メガバイトとギガバイトの違いは何ですか
# Import date class from datetime module> from> datetime>import> date> # Returns the current local date> today>=> date.today()> print>(>'Today date is: '>, today)> |
>
>
出力:
Today date is: 2019-12-11>
datetime.now() を使用して現在の日付を取得します。
Python ライブラリは、主に現在時刻と時刻を取得するために使用できる関数を定義します。 datetime.now() 関数を使用して、DateTime モジュールで定義されている現在のローカル日付と時刻を返します。
構文: 日時.現在(tz)
順序ツリーの走査パラメーター : つ : 現在の時刻と日付が必要な指定されたタイムゾーン。 (デフォルトではグリニッジ子午線時間を使用します。) 戻り値 : 現在の日付と時刻を時刻形式で返します。
例:
Python3
おっとコンセプト
# Import datetime class from datetime module> from> datetime>import> datetime> # returns current date and time> now>=> datetime.now()> print>(>'now = '>, now)> |
>
>
出力:
now = 2019-12-11 10:58:37.039404>
now() の属性
now() には、年、月、日、時、分、秒などの時間の属性と同じように、さまざまな属性があります。
例
Python3
ロムとは何ですか
# importing datetime module for now()> import> datetime> > # using now() to get current time> current_time>=> datetime.datetime.now()> > # Printing attributes of now().> print> (>'The attributes of now() are : '>)> > print> (>'Year: '>, end>=> '')> print> (current_time.year)> > print> (>'Month: '>, end>=> '')> print> (current_time.month)> > print> (>'Day: '>, end>=> '')> print> (current_time.day)> |
>
>
出力:
The attributes of now() are: Year: 2019 Month: 12 Day: 11>
おすすめの記事 – Python を使用して現在の日付と時刻を取得する