Convert ISO 8061 datetime to timestamp in python - python

I have a string with a date in the format: 2021-03-12T14:45:34.000Z
I would like to convert it to a standard format as this one: 12-Mar-2021 14:45:34
I tried using:
print(datetime.datetime.strptime("2021-03-12T14:45:34.000Z", "%Y-%m-%dT%H:%M:%S%fZ"))
but I get the error:
ValueError: time data '2021-03-12T14:45:34.000Z' does not match format
'%Y-%m-%dT%H:%M:%S%fZ'
How can I solve it?

You need to get as datetime then convert to forrmat as you like:
import datetime
date = '2021-03-12T14:45:34.000Z'
datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ"
).strftime('%d-%b-%Y %H:%M:%S')
Output:
'12-Mar-2021 14:45:34'

You are missing a . in your format string. The correct format string is
"%Y-%m-%dT%H:%M:%S.%fZ"
Notice the . after %S and before %fZ.

Related

Convert string of dd-MON-yy to date in Python

I have a string in dd-MON-yy format. While converting to date in python, its is causing issue since the year is in tow digits.
datetime.datetime.strptime('17-JUN-03', '%d-%m-%y')
The error is,
ValueError: time data '17-JUN-03' does not match format '%d-%m-%y'
Try this:
import datetime
print(datetime.datetime.strptime('17-JUN-03', '%d-%b-%y'))
Result:
2003-06-17 00:00:00
Datetime format codes

Convert Dates pandas datetime

I'm trying to convert dates in the format '31-Aug-91' into '31-08-1991' using pandas datetime.
I've tried pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%m-%y').dt.date
But, I get the error ValueError: time data '31-Aug-91' does not match format '%d-%m-%y' (match)
How do I fix this?
Use %b for match first 3 letters of month names:
pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%b-%y').dt.date
If need format DD-MM-YYYY:
pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%b-%y').dt.strftime('%d-%m-%Y')

How can I convert a string like 3hours26minutes33seconds into HH:MM:SS format in python?

I have a string '3hours26minutes33seconds' and I need to convert this into HH:MM:SS format.
I tried using dateutil.parser.parse to no avail.
You can use datetime and its strptime function to convert it to a datetime object:
import datetime
string = "3hours26minutes33seconds"
date = datetime.datetime.strptime(string, "%Hhours%Mminutes%Sseconds")
Then you format that datetime into the desired string format with strftime:
date.strftime("%H:%M:%S")

Convert ami creation date from string to datetime format

How to convert ami creation date from string to datetime format? I am using boto3, i need to convert ami.creation_date from sting format to datetime.
Here is an example:
t = datetime.datetime.strptime(i.image.creation_date, "%Y-%m-%dT%H:%M:%S.%fZ")
The string for image.creation_date looks like "2017-08-11T09:31:59.000Z"
To get the time in seconds since 1970: secs = t.timestamp()

python parse string in date format to get the date

There is a string and a date format. I want to get the date based on format.
If date format is YYYY.MM.dd and string is 2017.01.01. It should transform to a valid date object.
How can I find the date.
You can use datetime module something like this :
from datetime import datetime
date_object = datetime.strptime('2017.01.01', '%Y.%m.%d') # Converting the given date string into a datetime object.
formatted_date = date_object.strftime('%c') #User Defined Output Format
print(formatted_date)
This will result in :
Sun Jan 1 00:00:00 2017
You can refer to the documentation here.

Categories