date2date module#

Convert date representations between different regional variants.

Converts between date formats called English YYYY-MM-DD hh:mm:ss, US-English MM/DD/YYYY hh:mm:ss, French DD/MM/YYYY hh:mm:ss, and standard DD.MM.YYYY hh:mm:ss. The routines take list of dates of different formats and return date strings in one of the four formats.

This module was written by Matthias Cuntz while at Department of Computational Hydrosystems, Helmholtz Centre for Environmental Research - UFZ, Leipzig, Germany, and continued while at Institut National de Recherche pour l’Agriculture, l’Alimentation et l’Environnement (INRAE), Nancy, France.

copyright:

Copyright 2015-2022 Matthias Cuntz, see AUTHORS.rst for details.

license:

MIT License, see LICENSE for details.

The following functions are provided

date2date(edate[, fr, format, timesep, full])

Convert date representations between different regional variants

date2en(edate, **kwargs)

Wrapper function for date2date() with English date format output

date2fr(edate, **kwargs)

Wrapper function for date2date() with French date format output

date2us(edate, **kwargs)

Wrapper function for date2date() with US-English date format output

en2date(edate, **kwargs)

Wrapper function for date2date with standard date format output (default)

en2fr(edate, **kwargs)

Wrapper function for date2date with French date format output

en2us(edate, **kwargs)

Wrapper function for date2date() with US-English date format output

fr2date(edate, **kwargs)

Wrapper function for date2date() with French input and standard output date formats

fr2en(edate, **kwargs)

Wrapper function for date2date() with French input and English output date formats

fr2us(edate, **kwargs)

Wrapper function for date2date() with French input and US-English output date formats

us2date(edate, **kwargs)

Wrapper function for date2date with standard date format output (default)

us2en(edate, **kwargs)

Wrapper function for date2date() with English date format output

us2fr(edate, **kwargs)

Wrapper function for date2date with French date format output

History
  • Written Feb 2015 by Matthias Cuntz (mc (at) macu (dot) de)

  • Removed usage of date2dec and dec2date, Nov 2016, Matthias Cuntz

  • Adapted docstrings to Python 2 and 3, Nov 2016, Matthias Cuntz

  • Written fr2ascii, Mar 2018, Matthias Cuntz

  • Added us and fr keywords, renamed eng to en, Mar 2018, Matthias Cuntz

  • Added two-digit year YY, Nov 2018, Matthias Cuntz

  • Removed bug from usage of en and old name eng, Jun 2019, Matthias Cuntz

  • Using numpy docstring format, May 2020, Matthias Cuntz

  • flake8, Dec 2021, Matthias Cuntz

  • Remove eng, Dec 2021, Matthias Cuntz

  • Renamed ‘ascii’ in function names to ‘date’, and to ‘standard’ in docstrings and comments, Dec 2021, Matthias Cuntz

  • New more versatile date2date function replacing ascii2ascii, Dec 2021, Matthias Cuntz

  • Wrapper functions between all standard formats, Dec 2021, Matthias Cuntz

  • More consistent docstrings, Jan 2022, Matthias Cuntz

  • Use input2array, array2input, Jun 2022, Matthias Cuntz

  • Negative years possible, Jun 2022, Matthias Cuntz

  • Fractional seconds, Jun 2022, Matthias Cuntz

date2date(edate, fr=False, format='', timesep=' ', full=False)[source]#

Convert date representations between different regional variants

Convert date notations between standard DD.MM.YYYY hh:mm:ss, English YYYY-MM-DD hh:mm:ss, American MM/DD/YYYY hh:mm:ss, and French DD/MM/YYYY hh:mm:ss format (‘%d.%m.%Y %H:%M:%S’, ‘%Y-%m-%d %H:%M:%S’, ‘%m/%d/%Y %H:%M:%S’, and ‘%d/%m/%Y %H:%M:%S’ in datetime.strftime/strptime format notation, respectively). The routine determines the input date format by the date separator ‘.’, ‘-’, or ‘/’. If the latter is detected, the American format is assumed. One can set fr=True so that ‘/’ detects the French date format instead. The separator between date and time can be space ‘ ‘ or ‘T’, such as YYYY-MM-DDThh:mm:ss.

Output format is given either by the calling name of the function (e.g. date2en()) or by the format keyword. It allows ‘’, ‘en’, ‘us’, and ‘fr’ for standard, English, American, and French date format, respectively. One can also give any format string understood by datetime.strftime().

Parameters:
  • edate (array_like) – Date strings in any of three formats standard, English, or American/French.

  • fr (bool, optional) – Input dates with ‘/’ separators are interpreted as American format MM/DD/YYYY hh:mm:ss if False (default). Input dates with ‘/’ separators are interpreted as French format DD/MM/YYYY hh:mm:ss if True.

  • format (str, optional) – Output format. Can be any of ‘’ (default), ‘en’, ‘us’, and ‘fr’ for standard DD.MM.YYYY hh:mm:ss, English YYYY-MM-DD hh:mm:ss, American MM/DD/YYYY hh:mm:ss, and French DD/MM/YYYY hh:mm:ss format. format can also be any format string understood by datetime.strftime().

  • timesep (str, optional) – Separator string between date and time if format is ‘’, ‘en’, ‘us’, or ‘fr’. Default is space ‘ ‘, but ISO 8601 uses ‘T’, for example.

  • full (bool, optional) – Output dates are as long as input dates if False (default), e.g. [YYYY-MM-DD, YYYY-MM-DD hh:mm]. Output dates are all in full format DD.MM.YYYY hh:mm:ss if True; missing time info on input is 00 on output.

Returns:

Date representations in chosen format. Output type will be the same as the input type.

Return type:

array_like

Notes

Year, month and day must be given while second, minute and hour can be missing (assumed zero).

Years are supposed to be 4-digit years. If they have only 1 or 2 digits, then every year that is above the current year of the century will be taken as being in 1900, i.e. 90 will be taken as 1990, while all other years are taken in the 21st century, i.e. 20 will be 2020. 3-digit years will throw a ValueError.

Negative years are not supported. The function uses datetime if a format string for datetime.strftime() is given. This limits the minimum year to the limit of datetime in this case, which is year ‘0001’.

Examples

>>> edate = ['2014-11-12 12:00', '01.03.2015 17:56:00',
...          '1990-12-01', '04.05.1786']
>>> print(", ".join(date2date(edate)))
12.11.2014 12:00, 01.03.2015 17:56:00, 01.12.1990, 04.05.1786
>>> print(", ".join(date2date(edate, full=True)))
12.11.2014 12:00:00, 01.03.2015 17:56:00, 01.12.1990 00:00:00, 04.05.1786 00:00:00
>>> print(", ".join(date2date(edate, format='en')))
2014-11-12 12:00, 2015-03-01 17:56:00, 1990-12-01, 1786-05-04
>>> print(", ".join(date2date(edate, format='en', full=True)))
2014-11-12 12:00:00, 2015-03-01 17:56:00, 1990-12-01 00:00:00, 1786-05-04 00:00:00
>>> print(", ".join(date2date(edate, format='en', full=True, timesep='T')))
2014-11-12T12:00:00, 2015-03-01T17:56:00, 1990-12-01T00:00:00, 1786-05-04T00:00:00
>>> print(list(date2date(edate, format='%Y%m%d%H%M%S')))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> print(date2date(tuple(edate)))
('12.11.2014 12:00', '01.03.2015 17:56:00', '01.12.1990', '04.05.1786')
>>> print(date2date(np.array(edate)))
['12.11.2014 12:00' '01.03.2015 17:56:00' '01.12.1990' '04.05.1786']
>>> print(date2date(edate[0]))
12.11.2014 12:00
>>> print(", ".join(date2date(edate, format='us')))
11/12/2014 12:00, 03/01/2015 17:56:00, 12/01/1990, 05/04/1786
>>> print(", ".join(date2date(date2date(edate, format='en'),
...                           format='us', full=True)))
11/12/2014 12:00:00, 03/01/2015 17:56:00, 12/01/1990 00:00:00, 05/04/1786 00:00:00
>>> print(", ".join(date2date(edate, format='fr')))
12/11/2014 12:00, 01/03/2015 17:56:00, 01/12/1990, 04/05/1786
>>> print(", ".join(date2date(edate, format='fr', full=True)))
12/11/2014 12:00:00, 01/03/2015 17:56:00, 01/12/1990 00:00:00, 04/05/1786 00:00:00

2-digit year

>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> print(", ".join(date2date(edate)))
12.11.2014 12:00, 01.03.2015 17:56:00, 01.12.1990
>>> print(", ".join(date2date(edate, format='en')))
2014-11-12 12:00, 2015-03-01 17:56:00, 1990-12-01
>>> print(", ".join(date2date(edate, format='us')))
11/12/2014 12:00, 03/01/2015 17:56:00, 12/01/1990
>>> print(", ".join(date2date(edate, format='us', full=True)))
11/12/2014 12:00:00, 03/01/2015 17:56:00, 12/01/1990 00:00:00
>>> print(", ".join(date2date(edate, format='fr', full=True)))
12/11/2014 12:00:00, 01/03/2015 17:56:00, 01/12/1990 00:00:00
date2en(edate, **kwargs)[source]#

Wrapper function for date2date() with English date format output

That means date2date(edate, format=’en’, **kwargs); but format given in call will overwrite format=’en’.

Examples

>>> edate = ['2014-11-12 12:00', '01.03.2015 17:56:00',
...          '1990-12-01', '04.05.1786']
>>> print(", ".join(date2en(edate)))
2014-11-12 12:00, 2015-03-01 17:56:00, 1990-12-01, 1786-05-04
>>> print(", ".join(date2en(edate, full=True)))
2014-11-12 12:00:00, 2015-03-01 17:56:00, 1990-12-01 00:00:00, 1786-05-04 00:00:00
>>> print(date2en(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> print(", ".join(date2en(edate, full=True)))
2014-11-12 12:00:00, 2015-03-01 17:56:00, 1990-12-01 00:00:00
date2fr(edate, **kwargs)[source]#

Wrapper function for date2date() with French date format output

That means date2date(edate, format=’fr’, **kwargs); but format given in call will overwrite format=’fr’.

Examples

>>> edate = ['2014-11-12 12:00', '01.03.2015 17:56:00',
...          '1990-12-01', '04.05.1786']
>>> print(", ".join(date2fr(edate)))
12/11/2014 12:00, 01/03/2015 17:56:00, 01/12/1990, 04/05/1786
>>> print(", ".join(date2fr(edate, full=True)))
12/11/2014 12:00:00, 01/03/2015 17:56:00, 01/12/1990 00:00:00, 04/05/1786 00:00:00
>>> print(date2fr(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> print(", ".join(date2fr(edate, full=True)))
12/11/2014 12:00:00, 01/03/2015 17:56:00, 01/12/1990 00:00:00
date2us(edate, **kwargs)[source]#

Wrapper function for date2date() with US-English date format output

That means date2date(edate, format=’us’, **kwargs); but format given in call will overwrite format=’us’.

Examples

>>> edate = ['2014-11-12 12:00', '01.03.2015 17:56:00',
...          '1990-12-01', '04.05.1786']
>>> print(", ".join(date2us(edate)))
11/12/2014 12:00, 03/01/2015 17:56:00, 12/01/1990, 05/04/1786
>>> print(", ".join(date2us(edate, full=True)))
11/12/2014 12:00:00, 03/01/2015 17:56:00, 12/01/1990 00:00:00, 05/04/1786 00:00:00
>>> print(date2us(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> print(", ".join(date2us(edate, full=True)))
11/12/2014 12:00:00, 03/01/2015 17:56:00, 12/01/1990 00:00:00
en2date(edate, **kwargs)[source]#

Wrapper function for date2date with standard date format output (default)

That means date2date(edate, **kwargs).

Examples

>>> edate = ['2014-11-12 12:00', '01.03.2015 17:56:00',
...          '1990-12-01', '04.05.1786']
>>> edate = date2date(edate, format='en')
>>> print(", ".join(en2date(edate)))
12.11.2014 12:00, 01.03.2015 17:56:00, 01.12.1990, 04.05.1786
>>> print(", ".join(en2date(edate, full=True)))
12.11.2014 12:00:00, 01.03.2015 17:56:00, 01.12.1990 00:00:00, 04.05.1786 00:00:00
>>> print(en2date(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> edate = date2en(edate)
>>> print(", ".join(en2date(edate, full=True)))
12.11.2014 12:00:00, 01.03.2015 17:56:00, 01.12.1990 00:00:00
en2fr(edate, **kwargs)[source]#

Wrapper function for date2date with French date format output

That means date2date(edate, format=’fr’, **kwargs); but format given in call will overwrite format=’fr’.

Examples

>>> edate = ['2014-11-12 12:00', '01.03.2015 17:56:00',
...          '1990-12-01', '04.05.1786']
>>> print(", ".join(en2fr(edate)))
12/11/2014 12:00, 01/03/2015 17:56:00, 01/12/1990, 04/05/1786
>>> print(", ".join(en2fr(edate, full=True)))
12/11/2014 12:00:00, 01/03/2015 17:56:00, 01/12/1990 00:00:00, 04/05/1786 00:00:00
>>> print(en2fr(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> print(", ".join(en2fr(edate, full=True)))
12/11/2014 12:00:00, 01/03/2015 17:56:00, 01/12/1990 00:00:00
en2us(edate, **kwargs)[source]#

Wrapper function for date2date() with US-English date format output

That means date2date(edate, format=’us’, **kwargs); but format given in call will overwrite format=’us’.

Examples

>>> edate = ['2014-11-12 12:00', '01.03.2015 17:56:00',
...          '1990-12-01', '04.05.1786']
>>> print(", ".join(en2us(edate)))
11/12/2014 12:00, 03/01/2015 17:56:00, 12/01/1990, 05/04/1786
>>> print(", ".join(en2us(edate, full=True)))
11/12/2014 12:00:00, 03/01/2015 17:56:00, 12/01/1990 00:00:00, 05/04/1786 00:00:00
>>> print(en2us(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> print(", ".join(en2us(edate, full=True)))
11/12/2014 12:00:00, 03/01/2015 17:56:00, 12/01/1990 00:00:00
fr2date(edate, **kwargs)[source]#

Wrapper function for date2date() with French input and standard output date formats

That means date2date(edate, fr=True, **kwargs); but fr given in call will overwrite fr=True.

Examples

>>> edate = ['12/11/2014 12:00', '01/03/2015 17:56:00',
...          '01/12/1990', '04/05/1786']
>>> print(", ".join(fr2date(edate)))
12.11.2014 12:00, 01.03.2015 17:56:00, 01.12.1990, 04.05.1786
>>> print(", ".join(fr2date(edate, full=True)))
12.11.2014 12:00:00, 01.03.2015 17:56:00, 01.12.1990 00:00:00, 04.05.1786 00:00:00
>>> print(fr2date(list(edate), format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> print(fr2date(tuple(edate)))
('12.11.2014 12:00', '01.03.2015 17:56:00', '01.12.1990', '04.05.1786')
>>> print(fr2date(np.array(edate)))
['12.11.2014 12:00' '01.03.2015 17:56:00' '01.12.1990' '04.05.1786']
>>> print(fr2date(edate[0]))
12.11.2014 12:00

2-digit year

>>> edate = ['12/11/14 12:00', '01/03/15 17:56:00', '01/12/90']
>>> print(", ".join(fr2date(edate)))
12.11.2014 12:00, 01.03.2015 17:56:00, 01.12.1990
>>> print(", ".join(fr2date(edate, full=True)))
12.11.2014 12:00:00, 01.03.2015 17:56:00, 01.12.1990 00:00:00
fr2en(edate, **kwargs)[source]#

Wrapper function for date2date() with French input and English output date formats

That means date2date(edate, fr=True, format=’en’, **kwargs); but format and fr given in call will overwrite fr=True and format=’en’.

Examples

>>> edate = ['12/11/2014 12:00', '01/03/2015 17:56:00',
...          '01/12/1990', '04/05/1786']
>>> print(", ".join(fr2en(edate)))
2014-11-12 12:00, 2015-03-01 17:56:00, 1990-12-01, 1786-05-04
>>> print(", ".join(fr2en(edate, full=True)))
2014-11-12 12:00:00, 2015-03-01 17:56:00, 1990-12-01 00:00:00, 1786-05-04 00:00:00
>>> print(fr2en(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['12/11/14 12:00', '01/03/15 17:56:00', '01/12/90']
>>> print(", ".join(fr2en(edate, full=True)))
2014-11-12 12:00:00, 2015-03-01 17:56:00, 1990-12-01 00:00:00
fr2us(edate, **kwargs)[source]#

Wrapper function for date2date() with French input and US-English output date formats

That means date2date(edate, fr=True, format=’us’, **kwargs); but format and fr given in call will overwrite fr=True and format=’us’.

Examples

>>> edate = ['12/11/2014 12:00', '01/03/2015 17:56:00',
...          '01/12/1990', '04/05/1786']
>>> print(", ".join(fr2us(edate)))
11/12/2014 12:00, 03/01/2015 17:56:00, 12/01/1990, 05/04/1786
>>> print(", ".join(fr2us(edate, full=True)))
11/12/2014 12:00:00, 03/01/2015 17:56:00, 12/01/1990 00:00:00, 05/04/1786 00:00:00
>>> print(fr2us(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['12/11/14 12:00', '01/03/15 17:56:00', '01/12/90']
>>> print(", ".join(fr2us(edate, full=True)))
11/12/2014 12:00:00, 03/01/2015 17:56:00, 12/01/1990 00:00:00
us2date(edate, **kwargs)[source]#

Wrapper function for date2date with standard date format output (default)

That means date2date(edate, **kwargs).

Examples

>>> edate = ['11/12/2014 12:00', '01.03.2015 17:56:00',
...          '12/01/1990', '1786-05-04']
>>> print(", ".join(us2date(edate)))
12.11.2014 12:00, 01.03.2015 17:56:00, 01.12.1990, 04.05.1786
>>> print(", ".join(us2date(edate, full=True)))
12.11.2014 12:00:00, 01.03.2015 17:56:00, 01.12.1990 00:00:00, 04.05.1786 00:00:00
>>> print(us2date(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> edate = date2en(edate)
>>> print(", ".join(us2date(edate, full=True)))
12.11.2014 12:00:00, 01.03.2015 17:56:00, 01.12.1990 00:00:00
us2en(edate, **kwargs)[source]#

Wrapper function for date2date() with English date format output

That means date2date(edate, format=’en’, **kwargs); but format given in call will overwrite format=’en’.

Examples

>>> edate = ['11/12/2014 12:00', '01.03.2015 17:56:00',
...          '12/01/1990', '1786-05-04']
>>> print(", ".join(us2en(edate)))
2014-11-12 12:00, 2015-03-01 17:56:00, 1990-12-01, 1786-05-04
>>> print(", ".join(us2en(edate, full=True)))
2014-11-12 12:00:00, 2015-03-01 17:56:00, 1990-12-01 00:00:00, 1786-05-04 00:00:00
>>> print(us2en(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> print(", ".join(us2en(edate, full=True)))
2014-11-12 12:00:00, 2015-03-01 17:56:00, 1990-12-01 00:00:00
us2fr(edate, **kwargs)[source]#

Wrapper function for date2date with French date format output

That means date2date(edate, format=’fr’, **kwargs); but format given in call will overwrite format=’fr’.

Examples

>>> edate = ['11/12/2014 12:00', '01.03.2015 17:56:00',
...          '12/01/1990', '1786-05-04']
>>> print(", ".join(us2fr(edate)))
12/11/2014 12:00, 01/03/2015 17:56:00, 01/12/1990, 04/05/1786
>>> print(", ".join(us2fr(edate, full=True)))
12/11/2014 12:00:00, 01/03/2015 17:56:00, 01/12/1990 00:00:00, 04/05/1786 00:00:00
>>> print(us2fr(edate, format='%Y%m%d%H%M%S'))
['20141112120000', '20150301175600', '19901201000000', '17860504000000']
>>> edate = ['14-11-12 12:00', '01.03.15 17:56:00', '90-12-01']
>>> print(", ".join(us2fr(edate, full=True)))
12/11/2014 12:00:00, 01/03/2015 17:56:00, 01/12/1990 00:00:00