How to change Datetime Format in python? - python

I have a DATETIME format like this: '17-09-2019 06:22:00 PM'. I'm trying to use this format to convert '2019-09-17 06:22:00 PM'.
datetime.strptime(startingDate, '%y-%m-%d %I:%M:%S %p')
But I get an error. Anyone know how to do this?

Your input time format is wrong, it should match the string you are putting in.
"%Y-%m-%d %I:%M:%S %p" becomes "%d-%m-%Y %I:%M:%S %p"
Notice that the order of the date has changed.
import datetime
startingDate = "17-09-2019 06:22:00 PM"
dt = datetime.datetime.strptime(startingDate, "%d-%m-%Y %I:%M:%S %p")
datetime.datetime.strftime(dt, "%Y-%m-%d %I:%M:%S %p")
# "2019-09-17 06:22:00 PM"

Try this:
from datetime import datetime
startingDate = '17-09-2019 06:22:00 PM'
newDate = datetime.strptime(startingDate, '%d-%m-%Y %I:%M:%S %p').strftime("%Y-%m-%d %I:%M:%S %p")
print(newDate)
https://repl.it/repls/JuniorPushyLoopfission

Related

In Python, how will we convert this type of string into datetime format in pandas dataframe?

I have done:
1.
from datetime import datetime
date_string = df['Date']
format = '%m/%d/%Y %I:%M %p'
for i in date_string:
my_date = datetime.strptime(i, format)
print (my_date.strftime(format))
df['Date']=pd.to_datetime(df['Date'],format='%m/%d/%Y, %I:%M %p')
Use %y for years in format YY, %Y is for format YYYY:
df['Date']=pd.to_datetime(df['Date'],format='%m/%d/%y, %I:%M %p')
print (df)
Date
0 2018-02-26 12:31:00

issue in conversion of date using python

I am very new in python working on dates. I have 2 type of dates (because getting in array dynamically). Something like this ['2022-02-17 08:29:36.345374' , '2021-03-18 08:29:36']
I am trying to get these in this format 17/02/2022 08:29 PM.
Until now trying something like this:
def update_date(datetime):
date_formating = datetime.strptime(date_time, '%m/%d/%Y %I:%M %p')
return date_formating
but getting errors like:
ValueError: time data '2022-02-17 08:29:36.345374' does not match format '%m/%d/%Y %I:%M %p'
I need a solution which can turn both formats I am getting into desired format. Any help would be highly appreciated.
Try a list comprehension with datetime.fromisoformat(date_string):
>>> from datetime import datetime
>>> date_strs = ['2022-02-17 08:29:36.345374', '2021-03-18 08:29:36']
>>> [datetime.fromisoformat(s).strftime('%d/%m/%Y %I:%M %p') for s in date_strs]
['17/02/2022 08:29 AM', '18/03/2021 08:29 AM']
The times in the given list come in two different formats
with microseconds
with seconds (and no microseconds)
This code highlights the differences and would work:
import datetime as dt
t = ['2022-02-17 08:29:36.345374' , '2021-03-18 08:29:36']
# format with microseconds
d1 = dt.datetime.strptime(t[0], '%Y-%m-%d %H:%M:%S.%f')
# format with seconds
d2 = dt.datetime.strptime(t[1], '%Y-%m-%d %H:%M:%S')
# format user wants
x = d1.strftime('%m/%d/%Y %I:%M %p')
y = d2.strftime('%m/%d/%Y %I:%M %p')
print(x)
print(y)
The result:
02/17/2022 08:29 AM
03/18/2021 08:29 AM

Value error while using datetime.datetime.strptime

I want to covert a string item to the datetime format that I require.
The string item is this : '3/27/2013 2:54:00 PM'
My code is as follows: map(datetime.datetime.strptime,'3/27/2013 2:54:00 PM', '%m/%d/%Y %I:%M:%S %p')
I get the following error : stray % in format '%'
Any help is appreciated.
Not sure why you use map, you can just use strptime on that string, worked for me
import datetime
dt = datetime.datetime.strptime('3/27/2013 2:54:00 PM', '%m/%d/%Y %I:%M:%S %p')
print(dt)

Python strptime not parsing time zone EST %z

I am trying to parse the string '10/23/2019 6:02:05 PM EST' into a datetime with time zone using Python 3.7.
Code:
from datetime import datetime
timestamp = datetime.strptime(date_str, '%m/%d/%Y %I:%M:%S %p %Z')
Error:
ValueError: time data '10/23/2019 6:02:05 PM EST' does not match format '%m/%d/%Y %I:%M:%S %p %Z'
When I create a datetime and output it using the same formatting I get the correct output. The only difference is that there is a 0 in front of the hour, but adding 0 in front of the 6 in my date string results in the same error.
My current solution is to parse the datetime without the timezone and then localize it, but this is not ideal.
date_lst = date.split()
date_str = ' '.join(date_lst[0:3])
timestamp = datetime.strptime(date_str, '%m/%d/%Y %I:%M:%S %p')
new_tz = pytz.timezone(date_lst[3])
timestamp_tz = new_tz.localize(timestamp)```
It is reasonable to expect that parsing a string with a timezone included would produce a timezone aware datetime object.
Try it
>>timestamp = datetime.strptime('10/23/2019 6:02:05 PM EST', '%m/%d/%Y %I:%M:%S %p EST')
>>2019-10-23 06:02:05
You can try this.

DateTime Python

How to convert "12/17/2010 4:12:12 PM" to a datetime object?
For eg, If it was like "2007-03-04T21:08:12Z", I would have done
dd =datetime.strptime( "2007-03-04T21:08:12Z", "%Y-%m-%dT%H:%M:%SZ" )
but for time with AM/PM is there any direct way of doing?
From the strptime(3) man page:
%I The hour on a 12-hour clock (1-12).
...
%p The locale’s equivalent of AM or PM. (Note: there may be none.)
you can use below to handle "AM" or "PM" in a date
%I refers 12-hour format
%H refers 24-hours format
t = "12/17/2010 4:12:12 PM"
res = datetime.datetime.strptime(t, "%m/%d/%Y %I:%M:%S %p")
print res
datetime.datetime(2010, 12, 17, 16, 12, 12)
%p - refers am/AM/pm/PM.
d1= '12/17/2010 4:12:12 PM'
fmt = '%m/%d/%Y %H:%M:%S %p'
d2=datetime.datetime.strptime(d1, fmt)

Categories

Resources