[4.2] Smarter before/after rules with date_format

See #3603 - basically this makes the before/after rules smarter by taking into account the corresponding date_format rule if it is passed.

This is a potentially backwards breaking change, so I targetted master instead of 4.1.