Adam
Adam

Reputation: 111

Is it possible to change the way DateTime.Now reacts in my solution by hard coding a date in the Web Config file?

I need my solution to be processing on a specific date, but internal policy won't allow me to change the Server dates, hoping I could do this in the Web Config ?

Upvotes: 0

Views: 83

Answers (1)

Andrey Korneyev
Andrey Korneyev

Reputation: 26886

DateTime is .NET base class.

It doesn't knows anything about web.config existance and surely doesn't get any settings from it.

So answer is: no, you can't change DateTime behaviour by modifying your web.config.

Probably you shouldn't use DateTime.Now in this situation, and pass some date to your class/method if you want to change this date manually.

If you want to get this date from web.config, you add into <appSettings> section something like

<add key ="MyDate" value="20150702" />

And then just use some wrapper instead of DateTime.Now

public DateTime MyDate
{
    get 
    {
        var date = WebConfigurationManager.AppSettings["My_Date_Key"];
        return date == null ? DateTime.Now : 
                              DateTime.ParseExact(x, "yyyyMMdd", 
                                                  CultureInfo.InvariantCulture);
    }
}

Upvotes: 5

Related Questions