In [1]:
from datetime import datetime
In [2]:
oldformat = '20140716'
datetimeobject = datetime.strptime(oldformat,'%Y%m%d')
In [3]:
newformat = datetimeobject.strftime('%m-%d-%Y')
print newformat
In [4]:
newformat2 = datetimeobject.strftime('%m/%d/%Y')
print newformat2
In [5]:
#Here is a link to the python strftime character references
#http://strftime.org/
What if i datetime is like
ReplyDeleted='4/26/2018 10:45:19 PM'
I tried this but didnt not worked
datetimeobject = datetime.strptime(d,'%m/%d/%Y %H:%M:%S %p')
newformat = datetimeobject.strftime('%Y-%m-%d %I:%M:%S')
Though a very old post, sharing a link here for others to refer.
DeleteThe following link has formatting details related to date and time:
https://stackabuse.com/how-to-format-dates-in-python/
Your code worked like a charm! Thank you
ReplyDeleteWhat if the date format is like "Monday, May 28, 2018 at 8:00 PM"
ReplyDeleteWhat if i want to print it like DAYMONTHYEAR, 21092020 ?
ReplyDeletedatetimeobject.strftime("%d%m%Y")
DeleteWhat if I have an API response that includes this date format: '/Date(1488281267907)/'
ReplyDeleteHow am I able to convert all these values to a normal date format throughout the whole file?