Identify Format of date in python - python

How do I get the date format for the given date input in python?
Note:
The input is given by the user which is not predefined format .They may
give any kind of input format ..
The below example is working for dd-mm-yyyy format .But this is not in
my case.Date format is not predefined.
datetime.datetime.strptime('24052010', "%d%m%Y").date()
Expected :
Input 1: 21–02–2019 ,Output: DD-MM-YYYY .
Input 2: 02/21/2019 ,Output : MM/DD/YYYY

I think such function cannot be done because some dates (for example 01/01/2019) cannot be interpreted in one way. This can be both MM/DD/YYYY and DD/MM/YYYY. So you can only check if the date is in such format or not (you can use the answers to this question: How do I validate a date string format in python?).

You can use the module dateutil. It has a robust parser that will try to make sense of any date.
>>> from dateutil import parser
>>> parser.parse("21-02-2019")
datetime.datetime(2019, 2, 21, 0, 0)
>>> parser.parse("02/21/2019")
datetime.datetime(2019, 2, 21, 0, 0)
This isn't exactly what you wanted: you get the date not the format. But if you have the date, do you really need the format?
To meet J Kluseczka's point about some dates being ambiguous (like "01/10/2019") you can specify your assumption:
>>> parser.parse("01/10/2019")
datetime.datetime(2019, 1, 10, 0, 0)
>>> parser.parse("01/10/2019",dayfirst=True)
datetime.datetime(2019, 10, 1, 0, 0)
dateutil isn't part of the standard library but it is well worth the trouble of downloading.

Related

Convert date into a specific format in python irrespective of the input format is

I have a problem statement to convert the dates into a specific format. However, the input can be of any format.
For example,
Input
Desired_output
2020/11/20
2020-11-20
20201120
2020-11-20
20202011
2020-11-20
11/20/2020
2020-11-20
202020Nov
2020-11-20
I'm able to solve where is a delimiter present in between year, month and date using the Dateutil package. But it is not able to solve where is no clear delimiter.
Is there any way to solve this problem?
I don't think there is an easy way to universally parse dates that don't follow the standard formats. You'll have to specify the format in which the date is written for it to be parsed, which you can easily do using strptime (see here for a reference on datetime formatting).
A package like dateutil can be helpful as well. You should take a look, however, at the accepted date formats. In general, dateutil is more convenient, but strptime gives you more control and ability to parse more date formats.
Four out of the five examples you mentioned can be parsed using dateutil as follows:
from dateutil import parser
parser.parse("2020/11/20") # Output: datetime.datetime(2020, 11, 20, 0, 0)
parser.parse("11/20/2020") # Output: datetime.datetime(2020, 11, 20, 0, 0)
For the two other examples, the dayfirst and yearfirst arguments are needed to let dateutil parse them correctly:
parser.parse("20201120", yearfirst=True)
parser.parse("20202011", yearfirst=True, dayfirst=True)
# Output for both: datetime.datetime(2020, 11, 20, 0, 0)
Finally, I'm not aware of a way to parse the date "202020Nov" using dateutil; however, it can be parsed using strptime as follows:
from datetime import datetime
datetime.strptime("202020Nov", "%Y%d%b")
# Output: datetime.datetime(2020, 11, 20, 0, 0)
All the best.

How to convert string date to dateobject and get the year when string date value is in format 2021-01-22T11:36:52.387000+01:00 with datetime?

I tried this:
timestamp = "2021-01-22T11:36:52.387000+01:00"
timestampObject = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
But gave me error:
ValueError: unconverted data remains: .150000+01:00
What is the rest reprisenting and how do I convert the rest? Also what does the 'T' mean?
Because you also have to supply a format specifier to take care of the trailing microseconds and timezone specifier, like the error is telling you, see Conversion of datetime string with microseconds and ...milliseconds. Probably you need '.fZ'. See the datetime doc.
Also, the 'T' just stands for 'Time'; it separates the date-field from the time-field, for ease in parsing (with sed/perl/grep/regex/etc.). Makes it easy if you wanted to a) locate datetimes within a log or b) throw away/separate the time part from the date part.
The string format you have is actually a datetime in ISO format. Luckily datetime has a function for handling that, you don't have to worry about supplying a format specifier for the trailing time objects...
Do you want only the date?
>>> datetime.datetime.fromisoformat("2021-01-22T11:36:52.387000+01:00").date()
datetime.date(2021, 1, 22)
Or do you want datetime?
>>> datetime.datetime.fromisoformat("2021-01-22T11:36:52.387000+01:00")
datetime.datetime(2021, 1, 22, 11, 36, 52, 387000, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600)))
This worked for me:
timestampObject = datetime.fromisoformat(
"2021-01-22T11:36:52.387000+01:00" ).date()
print('timestampObject.year: ', timestampObject.year)
timestampObject.year: 2021

Convert Snort string to Python timestamp

I'm using Snort, which generates timestamps in MM-DD/time format, such as:
06/18-19:31:05.688344
I want to convert this to a Python timestamp, including the current year. What's the most pythonic way?
Use the datetime module's strptime function.
>>> import datetime
>>> datetime.datetime.strptime(str(datetime.datetime.now().year) +
... '/' + '06/18-19:31:05.688344', '%Y/%m/%d-%H:%M:%S.%f')
datetime.datetime(2015, 6, 18, 19, 31, 5, 688344)
Check out snorts configuration. In the output section you can setup "seconds" as an output field. This is the timestamp in unix format which is better to work with. If u still need to convert it :
datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')

Parsing non-zero padded timestamps in Python

I want to get datetimes from timestamps like the following :3/1/2014 9:55 with datetime.strptime, or something equivalent.
The month, day of month, and hour is not zero padded, but there doesn't seem to be a formatting directive listed here that is able to parse this automatically.
What's the best approach to do so? Thanks!
strptime is able to parse non-padded values. The fact that they are noted as being padded in the formatting codes table applies to strftime's output. So you can just use
datetime.strptime(datestr, "%m/%d/%Y %H:%M")
strptime isdo not require 0-padded values. See example below
datetime.strptime("3/1/2014 9:55", "%m/%d/%Y %H:%M")
output: datetime.datetime(2014, 3, 1, 9, 55)
The non-pattern way is use dateutil.parse module, it lets to parse the common date formats, even if you don't know what it is using currently
Ex:
>>> import dateutil.parser
>>>
>>> utc_time = '2014-08-13T00:00:00'
>>> verbose_time = '13-Aug-2014'
>>> some_locale = '3/1/2014 9:55'
>>> dateutil.parser.parse(utc_time)
datetime.datetime(2014, 8, 13, 0, 0)
>>> dateutil.parser.parse(verbose_time)
datetime.datetime(2014, 8, 13, 0, 0)
>>> dateutil.parser.parse(some_locale)
datetime.datetime(2014, 3, 1, 9, 55)
Just in case this answer helps someone else -- I came here thinking I had a problem with zero padding, but it was actually to do with 12:00 vs 00:00 and the %I formatter.
The %I formatter is meant to match 12-hour-clock hours, optionally zero-padded. But depending on your data source, you might get data that says that midnight or midday is actually zero, eg:
>>> datetime.strptime('2015/01/01 0:12am', "%Y/%m/%d %I:%M%p")
ValueError: time data '2015/01/01 0:12am' does not match format '%Y/%m/%d %I:%M'
What strptime actually wanted was a 12, not a zero:
>>> datetime.strptime('2015/01/01 12:12am', "%Y/%m/%d %I:%M%p")
datetime.datetime(2015, 1, 1, 0, 12)
But we don't always control our data sources! My solution for this edge case was to catch the exception, try parsing it with a %H, with a quick check that we are in the edge case we think we are in.
def get_datetime(string):
try:
timestamp = datetime.strptime(string, "%m/%d/%Y %I:%M%p")
except ValueError:
# someone used zero for midnight?
timestamp = datetime.strptime(string, "%m/%d/%Y %H:%M%p")
assert string.lower().endswith('am')
assert timestamp.hour == 0
return timestamp
You can see the strftime document here,but in fact they aren't all working well in all platforms,for instance,%-d,%-m don't work on win7 by python 2.7,so you can accomplish like this
>>> date_str = '{d.year}-{d.month}-{d.day}'.format(d=datetime.datetime.now())
>>> print(date_str)
2016-5-23

Converting legacy string dates to dates

We have some legacy string dates that I need to convert to actual dates that can be used to perform some date logic. Converting to a date object isn't a problem if I knew what the format were! That is, some people wrote 'dd month yy', othes 'mon d, yyyy', etc.
So, I was wondering if anybody knew of a py module that attempts to guess date formats and rewrites them in a uniform way?
Any other suggestions?
Using dateutil:
In [25]: import dateutil.parser as parser
In [26]: parser.parse('25 December 2010')
Out[26]: datetime.datetime(2010, 12, 25, 0, 0)
In [27]: parser.parse('Dec 25, 2010')
Out[27]: datetime.datetime(2010, 12, 25, 0, 0)
The typical approach is to define a list of formats (strptime formats, specifically), and try them in turn, until one works. As an example, see this recipe:
http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/
strptime accepts quite a number of formats. If you can enumerate all the possibilities you'll see, you should be able to hack together a variant of that recipe that'll do what you want.

Categories

Resources