Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you override Date.Now or Date.Today for debugging purposes in an ASP.NET web application?

We have a very massive system where reports are run off dates from specific days through to today's date using various definitions of "GenerateSalesReport(DateStart, Date.Now)".

For debugging purposes, I want to simulate reports that occurred in the past so I need to change the object "Date.Now" to a specific date from the past on my development environment. Is it possible to override date.Now?

like image 288
digiguru Avatar asked Jan 19 '23 01:01

digiguru


1 Answers

That is one of the failings of DateTime.Now and related functions.

You should be passing in a DateTime to any function that relies on it. Any place you use DateTime.Now or DateTime.Today (and such) is a place where you should be passing in the date.

This allows you to test for different DateTime values, will make your code more testable and remove the temporal dependency.

like image 120
Oded Avatar answered Jan 30 '23 20:01

Oded