from datetime import datetime
y='Monday, December 9, 2019'
I want to convert the above string to DD/MM/YYYY I tried
c=datetime.strptime(y,'%A, %B %-d,%Y')
so I can then easily convert it but it is giving me ValueError: '-' is a bad directive in format '%A, %B %-d,%Y I checked this question
'-' is a bad directive in format '%Y-%-m-%-d' - python/django but still gives error, is there a way to do this without using re ?
The correct format is '%A, %B %d, %Y' (noticed the removed -), and to change it to DD/MM/YYYY, the format is %d-%m-%Y'
from datetime import datetime
y='Monday, December 9, 2019'
#Fixed format
c=datetime.strptime(y,'%A, %B %d, %Y')
#Changed to represent DD/MM/YYYY
print(c.strftime('%d-%m-%Y'))
The output will be
09-12-2019
Related
I get an error when I try to convert a datetime string to a datetime object:
df['R_DATE'] = pd.to_datetime(df['R_DATE'], format='%a %b %d %H:%M:%S %Z %Y')
Error is:
...
File "pandas\_libs\tslibs\strptime.pyx", line 141, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data 'Mon Oct 18 00:00:00 EDT 2021'
does not match format '%a %b %d %H:%M:%S %Z %Y' (match)
From what I can tell format appears to match the datetime string value. I'm not sure if the timezone value (EDT) is causing issues.
nvm. found the answer I was looking for.
import dateutil
tzdict = {'EST': dateutil.tz.gettz('America/New_York'),
'EDT': dateutil.tz.gettz('America/New_York')}
df['R_DATE'] = df['R_DATE'].apply(dateutil.parser.parse, tzinfos=tzdict)
I am trying to convert it to datetime format. but i keep getting the following error.
convert_datetime= "Jun 25, 2021 10:35:50".replace(',','')
print(datetime.strptime(convert_datetime," %b %d %Y %H:%M:%S"))
Error:
raise ValueError("time data %r does not match format %r" %
ValueError: time data 'Jun 25 2021 10:35:50' does not match format ' %d %b %Y %H:%M:%S'
The error is in the formatted string you are trying to match with in the strptime function.
Change
" %b %d %Y %H:%M:%S"
to
"%b %d %Y %H:%M:%S"
. The space before the %b is causing the error because you do not have a space before Jun in convert_datetime variable.
Now your code modifies to:
convert_datetime= "Jun 25, 2021 10:35:50".replace(',','')
print(datetime.strptime(convert_datetime,"%b %d %Y %H:%M:%S"))
This should be working fine.
datetime.strptime(convert_datetime,"%b %d %Y %H:%M:%S")
You've an extra space at the beginning of the strp string.
strptime function of datetime module takes two arguments date_string and format
In your case, date_string is fine but format string is malformed. " %b %d %Y %H:%M:%S" should be "%b %d %Y %H:%M:%S".
As per documentation,
ValueError is raised if the date_string and format can’t be parsed by time.strptime() or if it returns a value which isn’t a time tuple. For a complete list of formatting directives, see strftime() and strptime() Behavior.
Python Official Documentation
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
How do i convert this datetime using python?
2017-10-16T08:27:16+0000
I tried to use strptime but getting ValueError: time data '2017-10-16T08:27:16+0000' does not match format 'The %d %B %Y at. %H:%M'
import datetime
datetime.datetime.strptime("2017-10-16T08:27:16+0000", "The %d %B %Y at. %H:%M")
'''
I want my output to look like this
The 16 october 2017 at. 08:27
'''
First parse the string correctly, then print it in the desired format:
import datetime
date = datetime.datetime.strptime("2017-10-16T08:27:16+0000", "%Y-%m-%dT%H:%M:%S%z")
print(date.strftime("The %d %B %Y at. %H:%M"))
https://blogs.harvard.edu/rprasad/2011/09/21/python-string-to-a-datetime-object/
You have to first strip your date using strptime() and then rebuild it using strftime()
import datetime
time = "2017-10-16T08:27:16+0000"
stripedTime = datetime.datetime.strptime(time, '%Y-%m-%dT%I:%M:%S%z')
rebuildTime = stripedTime.strftime('The %d %B %Y at. %H:%M')
print(rebuildTime)
I have next time value in unicode (<type 'unicode'>):
2017-08-09T15:02:58+0000.
How to convert it to friendly view (e.g. Day, Month of Year)?
This should do what you ask:
from datetime import datetime
a = '2017-08-09T15:02:58+0000'
datetime.strptime(a[:-5], '%Y-%m-%dT%H:%M:%S').strftime('%d, %b of %Y')
#09, Aug of 2017
strptime method throws error for timezone parameter that doesn't seem to interest you so I removed that part with a[:-5].
For the rest of the string you can just follow guidelines from datetime docs.
Using the same docs you can construct your datetime string using strftime() method like you wanted '%d, %b of %Y' or in plain words [day], [abbreviated month] of [Year]
try this
import datetime
today = datetime.date.today()
print today.strftime('We are the %d, %b %Y')
'We are the 22, Nov 2008'