If you really want to use the DateTime.Now and return your own value into it, then something like this can do the job... But it gets hard to know if the code is executing through your method or System.DateTime's method. The long term solution would be to implement your own replacement.
class Program
{
static void Main(string[] args)
{
var foo = DateTime.Now;
}
}
public static class DateTime
{
public static System.DateTime Now => new System.DateTime(1900, 1, 1);
}
This is also going to give you many issues accessing the real DateTime and after playing with it for just a few minutes it looks completely unworkable.
A better solution would be a separate class like this (one that I'm using until we move to an interface):
public static class SystemTime
{
public static DateTime Now => MockedNow ?? DateTime.Now;
public static DateTime UtcNow => MockedUtcNow ?? DateTime.UtcNow;
public static DateTime Today => MockedToday ?? DateTime.Today;
public static DateTime? MockedNow { get; set; }
public static DateTime? MockedUtcNow { get; set; }
public static DateTime? MockedToday { get; set; }
public static void ResetMocks()
{
MockedNow = null;
MockedToday = null;
MockedUtcNow = null;
}
}