Factor out and export Date function (parses and returns the value of the Date header)

This factors out the Date header parsing function so that it can be called from elsewhere.

Most lines in the diff are the result of unindenting code.