Skip to main content

parse_duration

Parse a duration string and return a datetime.timedelta.

The preferred format for durations in Django is '%d %H:%M:%S.%f'.

Also supports ISO 8601 representation (excluding years and months) and PostgreSQL's day-time interval format.

def parse_duration(
value: string
) - > datetime.timedelta

Parse a duration string and return a datetime.timedelta.

Parameters

NameTypeDescription
valuestringThe duration string to be parsed, supporting Django's preferred format, ISO 8601 (excluding years/months), and PostgreSQL interval formats

Returns

TypeDescription
datetime.timedeltaA timedelta object representing the parsed duration, or None if the input format is unrecognized