I have a variable datetime1="12/31/2016 02:00:00 PM"
However, when i run:
formatted_date = datetime.strptime(datetime1, "%m/%d/%Y %I:%M:%S %p")
print(formatted_date)
,it displays the result with dashes (-) instead of slashes (/). I want the output as a datestring object such as: 12/31/2016 02:00:00 PM instead of 2016-12-31 00:00:00
datetime1= '12/31/2016 02:00:00 PM'
formatted_date = datetime.strptime(datetime1, "%m/%d/%Y %I:%M:%S %p")
print(formatted_date)
is giving me: 2016-12-31 02:00:00
instead of 12/31/2016 02:00:00 PM
"strPtime" is for parse: it takes your date as a string, in the given format, and creates an internal datetime object.
That datetime object, in turn is convertible back to a string with the converse method ".strftime" - F for "format" - which then you can tweak as you want
In this case, if you just want to print the string representing your datetime as it is, just print it at once with
print(datetime1), no need to run it through datetime.Otherwise you want this:
And the intermediate
date_objis a datetime instance, which can do a lot of smart things date-related: