The current implementation can be greatly improved:

The root cause of these issues is that Lit does not have a default converter that converts a string to a Date object. This is where custom attribute converters come in! A custom attribute converter can clean up the implementation of date-display by providing a means to convert a string attribute to a Date object property.

In this step, you'll define a custom attribute converter that can convert a date-string attribute into a Javascript Date object property and back again.

Defining the custom attribute converter

Permalink to "Defining the custom attribute converter"

You'll notice a new, empty file called date-converter. in the editor panel. Use it to define a function that returns a converter.

date-converter.

Permalink to "date-converter."

Simple attribute converters.

If your toAttribute is just toString, then you can use a simple attribute converter instead of a complex attribute converter.

A simple attribute converter is a function that is equivalent to the fromAttribue method of a complex attribute converter object.

Congratulations! You've just defined a custom attribute converter! For more information, see Providing a custom converter.

In the next step, you'll put that shiny new converter to work.