[Python2] Date string-> UnixTime-> Date string

# Python2
import datetime, time

base_data = "Feb 27 00:51:29 2021 GMT"
date_time = time.strptime(base_data, "%b %d %H:%M:%S %Y GMT")
unix_time = int(time.mktime(date_time))
gen_based = datetime.datetime.fromtimestamp(unix_time).strftime("%b %d %H:%M:%S %Y GMT")

# >>> print base_data
# Feb 27 00:51:29 2021 GMT
# >>> print date_time
# time.struct_time(tm_year=2021, tm_mon=2, tm_mday=27, tm_hour=0, tm_min=51, tm_sec=29, tm_wday=5, tm_yday=58, tm_isdst=-1)
# >>> print unix_time
# 1614354689
# >>> print gen_based
# Feb 27 00:51:29 2021 GMT

# >>> base_data == gen_based
# True

Recommended Posts

[Python2] Date string-> UnixTime-> Date string
[python] Convert date to string
Python string
String date manipulation in Python
Python string format
Python date arithmetic
python string slice
Python2 string type
Python string format
Python # string type
Python string inversion
Conversion of string <-> date (date, datetime) in Python
String manipulation in python
Date manipulation in Python
Random string generation (Python)
python unix-time <-> datetime conversion
[Python3] Get date diff
Get date in Python
Date calculation in python
Get date with python
[Python] Split the date
Python3> documentation string / docstring
Date calculation in Python
Python string processing illustration
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Python indentation and string format
String object methods in Python
[Python] Use a string sequence
Various Python built-in string operations
Date and time ⇔ character string
[Python 2/3] Parse the format string
About Python string comparison operators
Python f character (formatted string)
String format with Python% operator
Sort by date in python
Determine the date and time format in Python and convert to Unixtime
Python
String replacement with Python regular expression
6 ways to string objects in Python
Python vba to create a date string for creating a file name
How to calculate date with python
python string processing map and lambda
Create a random string in Python
[Python] How to change character string (str) data to date (strptime of datetime)
Python Date / Time Library Reverse Lookup Reference
(Java, JavaScript, Python) Comparison of string processing
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
# 5 [python3] Extract characters from a character string
[Python] Use string data with scikit-learn SVM
[Python] How to invert a character string
[Python beginner memo] Python character string, path operation
Function to save images by date [python3]
Receive date type (datetime) with ArgumentParser [python]
Basic grammar of Python3 system (character string)
[python] EOL while scanning string literal resolution
Convert Python date types to RFC822 format
Generate a class from a string in Python
python> link> remove CR, LF>'test string \ n'.rstrip ()
Python basic course (4 numeric type / character string type)
String → Bool value conversion in Python Consideration
A memorandum of python string deletion process