Skip to main content

TimeField

This class represents a database time field that validates input and converts it into a Python datetime.time object. It utilizes localized input formats for parsing and provides a default error message for invalid time entries.

Attributes

AttributeTypeDescription
widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = TimeInputThe default form widget class used to render this field, which defaults to TimeInput.
input_formatslist = formats.get_format_lazy("TIME_INPUT_FORMATS")A list of formats used to attempt converting a string to a valid datetime.time object.
default_error_messagesdict = {"invalid": _("Enter a valid time.")}A dictionary of error message keys and their corresponding localized text used when validation fails.

Methods


to_python()

@classmethod
def to_python(
value: any
) - > datetime.time

Validate that the input can be converted to a time. Return a Python datetime.time object.

Parameters

NameTypeDescription
valueanyThe raw input value to be converted, which can be a string, a time object, or an empty value

Returns

TypeDescription
datetime.timeA Python time object representing the input value, or None if the input is empty

strptime()

@classmethod
def strptime(
value: string,
format: string
) - > datetime.time

Parses a time string into a datetime.time object using a specific format string.

Parameters

NameTypeDescription
valuestringThe time string to be parsed
formatstringThe strptime-compatible format string used to interpret the input value

Returns

TypeDescription
datetime.timeThe time component extracted from the parsed datetime string