miscset.dt.format¶
- miscset.dt.format(dt, fmt='dt', simplified=True)[source]¶
Format a datetime object.
Use a python
datetime.datetimeobject and convert it to a string, wrapping thedatetime.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
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, 4, 3, 43, 28, 636641) to: 2022-01-04 03:43:28 or to: 20220104034328