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)
Related
I'm trying to webscrape a flight data website using beautifulsoup in python but the timestamp is in unix timestamp how can i convert to regular datetime format. There are several such columns to be converted.
#scheduled_departure
result_items[0]['flight']['time']['scheduled']['departure']
and the output is shown as 1655781000. how can I convert it to Tue, Jun 21, 2022 8:40 AM
import time
print(time.strftime("%a, %b %d, %Y %H:%M %p", time.localtime(1655781000)))
There is only one Unix time and it is created by using the UTC/GMT time zone. This means you might have convert time zones to calculate timestamps.
import datetime
from pytz import timezone
local_datetime = datetime.datetime.fromtimestamp(1655781000)
local_time_str = datetime.datetime.strftime(local_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Local time: {local_time_str}')
other_timezone = 'Asia/Kolkata' # Replace your interest timezone here
remote_datetime = local_datetime.astimezone(timezone(other_timezone))
remote_time_str = datetime.datetime.strftime(remote_datetime, "%a, %d %b %Y %H:%M:%S %p")
print(f'Time at {other_timezone }: {remote_time_str}')
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
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
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'