LoginSignup
4
5

More than 5 years have passed since last update.

【Python】Pythonの時間・時刻の取り扱い

Last updated at Posted at 2016-07-06

Pythonで時間・時刻データを取り扱う方法をいつも忘れるので備忘録。
気付いたら徐々に追加していきたい。

import datetime

ref_date = datetime.datetime(2016, 4, 1, 10, 10)
cur_date = datetime.datetime(2016, 5, 15, 12, 30)
出力結果
ref_date
2016-04-01 10:10:00

cur_date
2016-05-15 12:30:00

基準時刻(ref_date)からの累積時間。

delta = cur_date - ref_date
delta.total_seconds()
出力結果
3810000.0 # 時・分などが欲しいときは適宜割る

pandasを使って文字列から変換。

import pandas as pd
pd.to_datetime('2016-04-01 10:15:30')
出力結果
Timestamp('2016-04-01 10:15:30')
4
5
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
4
5