Convert multiple types of dates - python

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")

Related

How to modify format of time in AM and PM in Python Pandas?

I have code in Python Pandas like below:
print("Date: ", datetime.now().strftime("%d-%m-%Y %I:%M:%S"))
And it generates result: Date: 14-09-2021 08:36:23
Nevertheless, I would like to have something like Date: 14-09-2021 20:36:23.
So at AM I would like to have time like 08:22:13 and so on and at PM I prefer to have format of time like: 20:22:13.
How can I modify my code above to achieve result as I need ?
%I indicates 12 hour format. I believe to solve your issue you have to switch %I to %H (24 hour format).

Python Arrow timezone parsing

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

Extract timestamp from a given string using python

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.

Identify that a string could be a datetime object

If I knew the format in which a string represents date-time information, then I can easily use datetime.datetime.strptime(s, fmt). However, without knowing the format of the string beforehand, would it be possible to determine whether a given string contains something that could be parsed as a datetime object with the right format string?
Obviously, generating every possible format string to do an exhaustive search is not a feasible idea. I also don't really want to write one function with many format strings hardcoded into it.
Does anyone have any thoughts on how this can be accomplished (perhaps some sort of regex?)?
What about fuzzyparsers:
Sample inputs:
jan 12, 2003
jan 5
2004-3-5
+34 -- 34 days in the future (relative to todays date)
-4 -- 4 days in the past (relative to todays date)
Example usage:
>>> from fuzzyparsers import parse_date
>>> parse_date('jun 17 2010') # my youngest son's birthday
datetime.date(2010, 6, 17)
Install with:
$ pip install fuzzyparsers
You can use parser from dateutil
Example usage:
from dateutil import parser
dt = parser.parse("Aug 28 1999 12:00AM")

validate date : python

I want to know how to convert different format dates to expected format in python .
ex : i want to get this format : 2/29/2012
['2012-02-01 // 2012-02-28', '2/15/2012', '2/13/2012', '2/14/2012', '2/23/2012', '2/18/2012', '2/29/2012']
How to check today date in the range '2012-02-01 // 2012-02-28'
Share your suggestions
Use the datetime library in python. You can just compare two different datetime.datetime objects. And you can separate the year, month, date thing to put it in anyform you want.
Check this link for all the library details.
http://docs.python.org/library/datetime.html
Hope that helped.
The dateutil python library parses a wider variety of date formats than the standard datetime module.

Categories