I'm trying to convert a string to DateTime object, The string:
Saturday 8th of August 2020 07:48:11 AM CDT
I'm using arrow package
arrow.get('Saturday 8th of August 2020 09:23:34 AM CDT', 'dddd Mt[h] of MMMM YYYY HH:mm:ss A ZZZ')
I'm getting the following error
arrow.parser.ParserError: Could not parse timezone expression "CDT"
I couldn't find any way to convert the CDN part into timezone.
Has said in the documentation, some abbreviations are ambiguous. You can use for example America/Chicago instead of CDT
Related
Has anyone converted this kind of times before?
2020-10-12T01:00:00-07:00 to 2020-10-12T09:00:00-07:00
equals
Monday, October 12, 2020 at 10:00 AM – 6:00 PM UTC+02
to datetime objects?
2020-10-12T01:00:00-07:00
<--date--> <-time-><zone>
This means 1am on October 12th, 2020, in the time zone 7 hours west of UTC (running through the middle of the US, basically).
It's actually one of the ISO8601 formats, used for date/time data interchange.
I believe the dateutil.parser() library can handle this in Python.
I have the following string: January 1, 2020 / 12:04 AM / a month ago. How do I convert this into 1/1/2020 0:04:00? The code should ignore the a month ago. How do I do this?
You could use dateutil. It is a third part extension of datetime module. You can add it with
python -m pip install python-dateutil
from dateutil.parser import parse
data = 'January 1, 2020 / 12:04 AM / a month ago'
resp = parse(data, fuzzy_with_tokens=True)
print(resp[0]) # the first index is datetime object
The parser is relatively powerful. Here is documentation to Parser.
dateutil is one among many that can help you solve your problem. Good summary of tools such as maya, arrow etc are found Stackabuse thanks to #WasabiMonster
using strptime you can do it and below is the sample,i was using in my code
datetime.datetime.strptime(
... "April 18, 2019, 09:09:09", "%B %d, %Y, %H:%M:%S"
I tried multiple packages to extract timestamp from a given string, but no package gives correct results. I did use dateutils, datefinder, parsedatetime, etc. for this task. They extract some datetimes which are in certain formats but not all formats, sometimes they extract some unwanted numbers also as timestamps.
Is there any python package which extracts datetime from a given string.
Assume, I have 2 strings like these:
scala> val xorder= new order(1,"2016-02-22 00:00:00.00",100,"COMPLETED")
and
Fri, 10 Jun 2011 11:04:17 +0200 (CEST)
and want to extract only datetime. Is there any function which extracts both formats of datetimes from above strings. In other cases formats may be different, still it should pick out datetime strings
You can use the datetime function strptime() as follows
dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
You can create your own formatting and use the function as well.
I created a small python package datetime_extractor to pull out timestamps from a given strings. It can extract many datetime formats from given strings. Hope it will be useful.
pip install datetime-extractor
from datetime_extractor import DateTimeExtractor
import re
samplestring1 = 'scala> val xorder= new order(1,"2016-02-22 00:00:00.00",100,"COMPLETED")'
DateTimeExtractor(samplestring1)
Out: ['2016-02-22 00:00:00.00']
samplestring2 = 'Fri, 10 Jun 2011 11:04:17 +0200 (CEST)'
DateTimeExtractor(samplestring2)
Out: ['10 Jun 2011 11:04:17']
#Allan & #Manmeet Singh, Let me know your comments.
I'm working with data that comes from different places and need to convert dates into the same format. Below are few examples of what I have:
Thu Dec 03 07:27:23 GMT 2015
3-Dec-15
2015-12-04T06:58:54Z
23-Sep-2015 07:03:37 UTC
The desired output format should be the same for all dates, like this:
12/03/2007
12/03/2015
12/04/2015
09/23/2015
Any suggestions how to achieve that with Python? Thanks in advance!
Yes, the dateutil library provides date format detection with the parse function :
from dateutil.parser import parse
parse(text).strftime("%m/%d/%Y")
I'm making a news aggregator using Python and Scrapy and cannot find an answer for exactly what I'm trying to do.
I am scraping a line of text from an article, a publish time, like so:
item['published'] = hxs.select('//div[#class="date"]/text()').extract()
This is what I'm getting back (there is no ISO date on the site, as there are some of the others I'm scraping for this project):
Last Updated: Tuesday, March 11, 2014
I need to put these dates and times into a format that I can also convert other sources' publish times and so that I can order them chronologically later via that key in the JSON feed.
So with a date in that format, how can I convert it to a usable form? I'd like in the end to have all the ISO dates and those written-out text formats converted to something like this:
Published: 2:15 p.m., March 15, 2014.
I think you want to use dateutil.parser.parse. Here's the documentation. It handles a variety of formats. On debian-style OSes, it's available in the package python-dateutil.
If this answer doesn't fully answer your question, please comment and I'll try to updated it appropriately.
Edit: jrennie's solution above is way cleaner than mine.
This works. I use strptime in order to get a solution. Note, since there is no hh:mm data in the original string, I can't output any hh:mm data like you did in your example.
Step by step solution:
>>> import time
>>> t = "Last Updated: Tuesday, March 11, 2014"
>>> t = t.rsplit(' ',4)[1:5] # Get a list of the relevant date fields
['Tuesday,', 'March', '11,', '2014']
>>> t = ' '.join(t) # Turn t into a string so we can use strptime
'Tuesday, March 11, 2014'
>>> t = time.strptime(t, "%A, %B %d, %Y") # Use strptime
time.struct_time(tm_year=2014, tm_mon=3, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=70, tm_isdst=-1)
One liner:
import time
t = "Last Updated: Tuesday, March 11, 2014"
time.strptime(' '.join(t.rsplit(' ',4)[1:5]), "%A, %B %d, %Y")
This results a struct_time. You may end up wanting convert these to datetimes, depending on how you wish to manipulate them.
Today a good way to do that is to use the dateparser project from the scrapy team: https://github.com/scrapinghub/dateparser