Date/Time Conversion

Download DateUtils

This utility class will convert different date time formats into the proper format to create a Python datetime object. It will use the current locate to determine differences that can not be determined in any other way such as 01/01/2010 meaning either dd/mm/yyyy or mm/dd/yyyy.

The methods are all classmethods.

A string value is passed into the DateUtils.toDatetime() method. If you know the format of your date or only want to allow a specific format that format can be passed into the format keyword argument.

Examples:

from dateutils import DateUtils

DateUtils.toDatetime('2010/04/05')
Out[2]: datetime.datetime(2010, 4, 5, 0, 0)

from dateutils import DateUtils

DateUtils.toDatetime('2010/04/05 23:20:59')
Out[3]: datetime.datetime(2010, 4, 5, 23, 20, 59)

from dateutils import DateUtils

DateUtils.toDatetime('20100405232059', format="yyyymmddHHMMSS")
Out[6]: datetime.datetime(2010, 4, 5, 23, 20, 59)

Exceptions

  1. A ValueError will be raised if the value does not match the format or if the format can not be determined.

  2. A KeyError will be raised if the the date time format is not supported.