miscset.dt.format

miscset.dt.format(dt, fmt='dt', simplified=True)[source]

Format a datetime object.

Use a python datetime.datetime object and convert it to a string, wrapping the datetime.datetime.strftime() method. For convenience, enable the interpretation of a simpler syntax for common and default string formatted system time.

Parameters
  • dt (datetime.datetime) – A datetime object to format to a string.

  • fmt (str) –

    A datetime string format to apply, one of:

    • ”dt” aka date and time -> YYYY-MM-DD HH:MM:SS

    • ”d” aka date -> YYYY-MM-DD

    • ”t” aka time -> HH:MM:SS

    • ”f” aka file name -> YYYY-MM-DD_HH-MM-SS

    • ”n” aka numbers -> YYYYMMDDHHMMSS

  • simplified (bool) – True to expand the fmt from a simplified encoding; otherwise use the formats supported by the datetime.datetime.strftime() method, see details at the documentation for format codes.

Returns

The datetime object converted to a string.

Return type

str

Example code:

import datetime
now = datetime.datetime.now()
print(f"from:  {repr(now)}")

import miscset
nowstr = miscset.dt.format(now)
print(f"to:    {nowstr}")
nowstr = miscset.dt.format(now, "n")
print(f"or to: {nowstr}")

Result:

from:  datetime.datetime(2022, 1, 7, 20, 12, 31, 76735)
to:    2022-01-07 20:12:31
or to: 20220107201231