How to scrub dates
September 4, 2025 ยท View on GitHub
Contents
How to do it
The easiest way to scrub a date is by calling
verify(
"created at 03:14:15",
options=Options().with_scrubber(DateScrubber.get_scrubber_for("00:00:00")),
)
which will produce
created at <date0>
Adding custom date formats
If you encounter a date format that isn't supported by the built-in formats, you can easily add your own custom date scrubber using the add_scrubber method:
from approvaltests.scrubbers.date_scrubber import DateScrubber
# Add a custom date format
DateScrubber.add_scrubber("2025-07-20", r"\d{4}-\d{2}-\d{2}", display_message=False)
# Now you can use it in your tests
verify(
"Event scheduled for 2025-07-20",
options=Options().with_scrubber(DateScrubber.get_scrubber_for("2025-07-20")),
)
Global scope: Custom scrubbers are available globally once added
Contributing Back:
If you think the format you want to scrub would be useful for others, please add it to https://github.com/approvals/ApprovalTests.Python/issues/124.
Supported formats
| Example Date | Regex Pattern |
|---|---|
| Tue May 13 16:30:00 | [a-zA-Z]{3} [a-zA-Z]{3} \d{2} \d{2}:\d{2}:\d{2} |
| Wed Nov 17 22:28:33 EET 2021 | [a-zA-Z]{3} [a-zA-Z]{3} \d{2} \d{2}:\d{2}:\d{2} [a-zA-Z]{3,4} \d{4} |
| Tue May 13 2014 23:30:00.789 | [a-zA-Z]{3} [a-zA-Z]{3} \d{2} \d{4} \d{2}:\d{2}:\d{2}.\d{3} |
| Tue May 13 16:30:00 -0800 2014 | [a-zA-Z]{3} [a-zA-Z]{3} \d{2} \d{2}:\d{2}:\d{2} -\d{4} \d{4} |
| 13 May 2014 23:50:49,999 | \d{2} [a-zA-Z]{3} \d{4} \d{2}:\d{2}:\d{2},\d{3} |
| May 13, 2014 11:30:00 PM PST | [a-zA-Z]{3} \d{2}, \d{4} \d{2}:\d{2}:\d{2} [a-zA-Z]{2} [a-zA-Z]{3} |
| 23:30:00 | \d{2}:\d{2}:\d{2} |
| 2014/05/13 16:30:59.786 | \d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}.\d{2}\d |
| 2020-9-10T08:07Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}Z |
| 2020-09-10T08:07:89Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}:\d{2}Z |
| 2020-09-10T01:23:45.678Z | \d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{2}:\d{2}.\d{3}Z |
| 2023-07-16 17:39:03.293919 | \d{4}-\d{1,2}-\d{1,2}(?:T |
| 20210505T091112Z | \d{8}T\d{6}Z |
| Tue May 13 16:30:00 2014 | (Mon |
| 2021-09-10T08:07:00+03:00 | \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}+\d{2}:\d{2} |
| 20250527_125703 | [12]\d{3}[01]\d[0-3]\d_[0-2]\d[0-5]\d[0-5]\d |
| 2020-02-02 | \d{4}-\d{2}-\d{2} |