Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python date from string

from datetime import datetime, timezone

timestamp_str = str(datetime.now(timezone.utc))
print(timestamp_str, type(timestamp_str))   # 2022-05-06 11:23:00.718012+00:00 <class 'str'>

timestamp = datetime.fromisoformat(timestamp_str) # Python 3.7+: 
print(timestamp, type(timestamp))   # 2022-05-06 11:23:00.718012+00:00 <class 'datetime.datetime'>

timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S.%f%z')
print(timestamp, type(timestamp))   # 2022-05-06 11:23:00.718012+00:00 <class 'datetime.datetime'>
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #date #string
ADD COMMENT
Topic
Name
6+5 =