Skip to content

HTML5 datetime-local valid format HTMLFormRenderer #5363

Open
@bobort

Description

@bobort

if style.get('input_type') == 'datetime-local' and isinstance(field.value, six.text_type):

When working with the browseable API form, DateTimeFields do not show initial data in the latest version of Chrome. I've not tested it in other browsers. The browser console provides this error for a datetime of 2017-10-17T16:03:37.497032:

The specified value "2017-10-17T16:03:37.497032" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".

Basically, the decimal portion of the seconds cannot exceed three (3) characters. I would add something like the following to around line 340 in renderers.py. It seems to work for me. Obviously, it does lose some accuracy for applications that require it, so maybe that should be handled some other way for those situations. Since the format of the datatime can be different, I just decided to truncate seconds completely. I don't really care about the seconds for my application.

        if style.get('input_type') == 'datetime-local' and isinstance(field.value, six.text_type):
            field.value = field.value.rstrip('Z')
            # to handle HTML 5 datetime-local formatting restrictions, the seconds decimal should be no longer
            #   than three characters:
            #     The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".
            # This will truncate the time to its minutes and completely avoid using seconds.  The
            # reason is that the format of the date may include a time zone or a Z
            split_value = field.value.split(":")
            if len(split_value) >= 2:
                field.value = "%s:%s" % (split_value[0], split_value[1][:2])

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions