Is there a Python module to parse a date like the 'date' command in Linux?

Tim Williams tjandacw at gmail.com
Mon May 22 16:21:23 EDT 2023


On Mon, May 22, 2023 at 12:41 PM Mats Wichmann <mats at wichmann.us> wrote:

> On 5/20/23 13:53, Chris Green wrote:
> > I'm converting a bash script to python as it has become rather clumsy
> > in bash.
> >
> > However I have hit a problem with converting dates, the bash script
> > has:-
> >
> >      dat=$(date --date "$1" +"%Y/%m/%d")
> >
> > and this will accept almost anything reasonably sensible that can be
> > interpreted as a date, in particular it accepts things like "tomorrow",
> > "yesterday" and "next thursday".
> >
> > Is there anything similar in Python or would I be better off simply
> > using os.system() to run date from the python program?
> >
>
> in the standard library, datetime
>
> as an addon module, dateutil  (install as python-dateutil)
>
> Don't know if either are exactly what you want, but do take a look.
>
> --
> https://mail.python.org/mailman/listinfo/python-list


In particular,check out dateutil.parser.
parser — dateutil 2.8.2 documentation
<https://dateutil.readthedocs.io/en/stable/parser.html>

parser
<https://dateutil.readthedocs.io/en/stable/parser.html#module-dateutil.parser>

This module offers a generic date/time string parser which is able to parse
most known formats to represent a date and/or time.

This module attempts to be forgiving with regards to unlikely input
formats, returning a datetime object even for dates which are ambiguous. If
an element of a date/time stamp is omitted, the following rules are applied:


More information about the Python-list mailing list