LoginSignup
4
3

More than 5 years have passed since last update.

Pythonの日付型をRFC822フォーマットに変換する

Posted at

Pythonの基本となる日付型はISO8601で以下のようなフォーマットとなっている

2017-08-25 18:57:07.602290

対するRFC822は以下のようなフォーマット

Fri, 25 Aug 2017 09:57:07 -0000

PythonでISO8601をRFC822に変換する方法は以下のとおり

import datetime
import time
from email import utils

now = datetime.datetime.now()
# 2017-08-25 18:57:07.602290

nowtuple = now.timetuple()
# time.struct_time(tm_year=2017, tm_mon=8, tm_mday=25, tm_hour=18, tm_min=57, tm_sec=7, tm_wday=4, tm_yday=237, tm_isdst=-1)

nowtimestamp = time.mktime(nowtuple)
# 1503655027.0

rfc822 = utils.formatdate(nowtimestamp)

print(rfc822)
# Fri, 25 Aug 2017 09:57:07 -0000
4
3
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
3