LoginSignup
0
0

Pythonにおける日本(東京)時刻の取得 ※Google colabでdatetimeを使用

Posted at

Pythonにおける日本時刻の取得について書く。

このコードはGoogle colabで使用することを想定しています。

目的

Google colab上で日本時間を取得して格納すること

まず一般的に現在の時刻を取得する方法は下記のコードである。

import datetime 
now = datetime.datetime.now() 

しかし、これはUTC時刻であり、日本時刻とは違います。
Google Colabでは協定世界時をデフォルトにしているため、これを日本時間に直す必要があります。

日本時間の取得方法

import datetime 
from pytz import timezone
now = datetime.datetime.now() #現在時刻生成
now = now.astimezone(timezone('Asia/Tokyo')) #タイムゾーンを東京に

上記を付け加えることで、nowに現在の東京の時刻を格納することができる。

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