How to convert date to mysql date format like Dec 21, 2019 to 2019-12-21 in python.
Please help, I am new to python.
i tried date.strftime("%Y-%m-%d %H:%M:%S") but its won't work.
You need to provide the correct format string.
import datetime
d = datetime.datetime.strptime("Dec 21, 2019","%b %d, %Y")
d.strftime("%Y-%m-%d")
If you're not sure about the format of date and/or expecting multiple formats, you can use the below code snippet
from dateutil.parser import parse
d = parse("Dec 21, 2019")
d.strftime("%Y-%m-%d")
Example snippet for date conversion.
from datetime import datetime
oldformat = 'Dec 21, 2019'
datetimeobject = datetime.strptime(oldformat,'%b %d, %Y')
newformat = datetimeobject.strftime('%Y-%m-%d')
print(newformat)
Related
Please, How do I converts strings like this:
2021-01-15 14:22:56.692234+00:00 into datetime value as this :
Jan. 15, 2021, 3:07 p.m.
You can use datetime module to do the parsing.
from datetime import datetime
datetime.strptime('2019-01-04T16:41:24+0200', "%Y-%m-%dT%H:%M:%S%z")
Use the datetime module, specifically, strptime to convert a string into a datetime object, then strftime to print it out in your desired format.
from datetime import datetime
parsed = datetime.strptime(
'2021-01-15 14:22:56.692234+00:00',
'%Y-%m-%d %H:%M:%S.%f%z'
)
formatted = parsed.strftime('%b. %d, %Y, %-I:%M %p')
print(formatted)
This will give:
'Jan. 15, 2021, 2:22 PM'
There are no way to reproduce exactly p.m. but to replace it manually:
formatted = formatted.replace('AM', 'a.m.').replace('PM', 'p.m.')
print(formatted)
Which gives:
'Jan. 15, 2021, 2:22 p.m.'
Is there a way to guess datetime format of a given string in python?
e.g. desired usage would be:
> guess_format('2020-02-24 07:22')
'%Y-%m-%d %H:%M'
There's dateutil project which automates datetime string conversion to valid Datetime objects:
> from dateutil.parser import parse
> parse('2020-02-24 07:22')
datetime.datetime(2020, 2, 24, 7, 22)
but can it produce valid formatting strings?
The pydateinfer package provides the possibility to infer the datetime format string of a given date string.
Example:
>>> import dateinfer
>>> dateinfer.infer(['Mon Jan 13 09:52:52 MST 2014', 'Tue Jan 21 15:30:00 EST 2014'])
'%a %b %d %H:%M:%S %Z %Y'
The dates that I have in a data frame are formatted like this:
Apr 5, 2010.
How can I convert them to datetime?
Here's what I've tried so far:
date_time_str = 'Sep 28, 2019'
date_time_obj = datetime.datetime.strptime(date_time_str, '%m %d, %Y')
print(date_time_obj)
But I know this is wrong.
(I've tried looking for similar questions on here, but most people have their string dates formatted like 05-05-2010.)
The following code will produce a new datetime object with the values you would like.
from datetime import datetime
date_time_str = 'Sep 28, 2019'
date_time_obj = datetime.strptime(date_time_str, '%b %d, %Y')
print(date_time_obj)
print(type(date_time_obj)) # verify object type
I work with api in python3 in this api return date like this
'Jun 29, 2018 12:44:14 AM'
but i need just hours, minute ad second like this
12:44:14
are there a fonction that can format this
It looks like the output is a string. So, you can use string slicing:
x = 'Jun 29, 2018 12:44:18 AM'
time = x[-11:-3]
It's best to use negative indexing here because the day may be single-digit or double-digit, so a solution like time = x[13:21] won't work every time.
If you're inclined, you may wish to use strptime() and strftime() to take your string, convert it into a datetime object, and then convert that into a string in HH:MM:SS format. (You may wish to consult the datetime module documentation for this approach).
Use the datetime module. Use .strptime() to convert string to datetime object and then .strftime() to convert to your required string output. Your sample datetime string is represented as '%b %d, %Y %H:%M:%S %p'
Ex:
import datetime
s = 'Jun 29, 2018 12:44:14 AM'
print( datetime.datetime.strptime(s, '%b %d, %Y %H:%M:%S %p').strftime("%H:%M:%S") )
Output:
12:44:14
I am trying to write a program that asks for the user to input the date in the format mm/dd/yyyy and convert it. So, if the user input 01/01/2009, the program should display January 01, 2009. This is my program so far. I managed to convert the month, but the other elements have a bracket around them so it displays January [01] [2009].
date=input('Enter a date(mm/dd/yyy)')
replace=date.replace('/',' ')
convert=replace.split()
day=convert[1:2]
year=convert[2:4]
for ch in convert:
if ch[:2]=='01':
print('January ',day,year )
Thank you in advance!
Don't reinvent the wheel and use a combination of strptime() and strftime() from datetime module which is a part of python standard library (docs):
>>> from datetime import datetime
>>> date_input = input('Enter a date(mm/dd/yyyy): ')
Enter a date(mm/dd/yyyy): 11/01/2013
>>> date_object = datetime.strptime(date_input, '%m/%d/%Y')
>>> print(date_object.strftime('%B %d, %Y'))
November 01, 2013
You might want to look into python's datetime library which will take care of interpreting dates for you. https://docs.python.org/2/library/datetime.html#module-datetime
from datetime import datetime
d = input('Enter a date(mm/dd/yyy)')
# now convert the string into datetime object given the pattern
d = datetime.strptime(d, "%m/%d/%Y")
# print the datetime in any format you wish.
print d.strftime("%B %d, %Y")
You can check what %m, %d and other identifiers stand for here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
As a suggestion use dateutil, which infers the format by itself:
>>> from dateutil.parser import parse
>>> parse('01/05/2009').strftime('%B %d, %Y')
'January 05, 2009'
>>> parse('2009-JAN-5').strftime('%B %d, %Y')
'January 05, 2009'
>>> parse('2009.01.05').strftime('%B %d, %Y')
'January 05, 2009'
Split it by the slashes
convert = replace.split('/')
and then create a dictionary of the months:
months = {1:"January",etc...}
and then to display it do:
print months[convert[0]] + day + year