Only write dates/date times to XML if they're valid - fixes #14159

Pretty straight-forward - passing through jenkins as sanity.

This fixes #14159 - ensure a date/datetime is valid before writing it to XML, as I think we're past the start of the 17th century...