Convert ami creation date from string to datetime format - python

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()

Related

Convert custom string date to date

Is there a way to convert a string date that is stored in some non-traditional custom manner into a date using datetime (or something equivalent)? The dates I am dealing with are S3 partitions that look like this:
year=2023/month=2/dayofmonth=3
I can accomplish this with several replaces but im hoping to find a clean single operation to do this.
You might provide datetime.datetime.strptime with format string holding text, in this case
import datetime
dt = datetime.datetime.strptime("year=2023/month=2/dayofmonth=3","year=%Y/month=%m/dayofmonth=%d")
d = dt.date()
print(d) # 2023-02-03
you can do that converting your string into a date object using "datetime" combined with strptime() method.
The strtime() takes two arguments, the first is the string to be parsed, and the second a string with the format.
Here's an example:
from datetime import datetime
# your string
date_string = "year=2023/month=2/dayofmonth=3"
# parse the string into a datetime object
date = datetime.strptime(date_string, "year=%Y/month=%m/dayofmonth=%d")
# print the datetime object
print(date)

Unable to return only time from datetime object

I have some columns which has time in string format like '01:19:55' and so on. I want to convert this string into time format. For that, I am trying to use the code like below:
col_list2=['clm_rcvd_tm','gtwy_rcvd_tm']
pandas_df_1=df2.toPandas()
for x in col_list2:
pandas_df_1[x]=pd.to_datetime(pandas_df_1[x].replace(" ",""),format='%H:%M:%S').dt.time
As an output, these clumns are returning decimal values. (Ex:0.26974537037037)
Any help will be appreciated.
If you have a string in your pandas dataframe as you mentioned i.e;
01:19:55 so that you can easily convert this by using python datetime module
For example;
import datetime
str = "01:19:55"
dt = datetime.strptime(str, '%H:%M:%S') # for 24 hour format
dt = datetime.strptime(str, '%I:%M:%S') # for 12 hour format
for references have a look
Python Datetime formating

Datetime still showing time component

I have this line of code-
future_end_date = datetime.strptime('2020/02/29','%Y/%m/%d')
and when I print this-
2020-02-29 00:00:00
it still shows the time component even though I did strptime
This is because strptime returns datetime rather than date. Try converting it to date:
datetime.strptime('2020/02/29','%Y/%m/%d').date()
datetime.strptime(date_string, format) function returns a datetime
object corresponding to date_string, parsed according to format.
When you print datetime object, it is formatted as a string in ISO
8601 format, YYYY-MM-DDTHH:MM:SS
So you need to convert the datetime into date if you only want Year, month and Day -
datetime.strptime('2020/02/29','%Y/%m/%d').date()
Another possible way is using strftime() method which returns a string representing date and time using date, time or datetime object.
datetime.strptime('2020/02/29','%Y/%m/%d').strftime('%Y/%m/%d')
Output of both code snippets -
2020/02/29

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")

ValueError: time data does not match format (convert part of string to time)

I have data that is formatted like DDHHMM (day hour minutes), for example 120630. So the 12th at 06:30. I want to extract only the hour and minutes and convert it to a time object. Is this possible. I get the following error.
time = datetime.strptime(column[3], '%H:%M') #data is from CSV
ValueError: time data '120630' does not match format '%H:%M'
You first need to parse the datetime string in the format it currently is using strptime, and then convert the datetime object to the format you want using strftime:
datetime.strptime('120630', '%d%H%M').strftime('%H:%M')
# '06:30'

Categories