Convert Python date types to RFC822 format

The basic date type of Python is ISO 8601 and has the following format

2017-08-25 18:57:07.602290

RFC822, on the other hand, has the following format

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

Here's how to convert ISO 8601 to RFC 822 in Python

python


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

Recommended Posts

Convert Python date types to RFC822 format
[python] Convert date to string
Determine the date and time format in Python and convert to Unixtime
Convert "number" of excel date to python datetime
[Python] How to change the date format (display format)
Convert strings to character-by-character list format with python
Convert timezoned date and time to Unixtime in Python2.7
[Python] Convert list to Pandas [Pandas]
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert / return class object to JSON format in Python
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Convert python 3.x code to python 2.x
Python / datetime> Implementation to convert YYYYMMDD format to YYYY / MM / DD
Convert markdown to PDF in Python
Workflow to convert formula (image) to python
Convert list to DataFrame with python
Python> list> Convert double list to single list
[Python] Convert natural numbers to ordinal numbers
How to calculate date with python
Convert decimal numbers to n-ary numbers [python]
Python> tuple> Convert double tuple to single tuple
[Python] How to convert db file to csv
Convert xml format data to txt format data (yolov3)
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
[Python] How to convert a 2D list to a 1D list
Convert matplotlib graphs to emf file format
[Python] Convert csv file delimiters to tab delimiters
Convert psd file to png in Python
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Function to save images by date [python3]
Convert FX 1-minute data to 5-minute data with Python
python> Convert tuple to list> aList = list (pi_tuple)
Convert PDF attached to email to text format
Recommended books by 3 types related to Python
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
How to convert DateTimeField format in Django
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
Sample to convert image to Wavelet with Python
Note: [Python3] Convert datetime to a string in any format you like
Convert XML document stored in XML database (BaseX) to CSV format (using Python)
I made a script in python to convert .md files to Scrapbox format
Preprocessing with Python. Convert Nico Nico Douga tag search results to CSV format
Python code to convert region mesh code to latitude / longitude
Updated to Python 2.7.9
Python string format
[Tensorflowjs_converter] How to convert Tensorflow model to Tensorflow.js format
Convert FBX files to ASCII <-> BINARY in Python
Python date arithmetic
[Python] How to use two types of type ()
Convert PDF to image (JPEG / PNG) with Python
Convert to HSV
Convert PDFs to images in bulk with Python
Python script to convert latitude / longitude to mesh code
[Python] Convert from DICOM to PNG or CSV
Convert svg file to png / ico with Python
[Introduction to Python] How to handle JSON format data