I have .net core 5.0 application and try to get DateTime from appsettings.json
appsettings.json:
"TimeModel": {
"RestartDuration": "27.10.2021 12:30:00"
}
Code:
services.Configure<TimeModel>(
configuration.GetSection("TimeModel"));
Error:
System.InvalidOperationException: Failed to convert configuration value at 'TimeModel:RestartDuration' to type 'System.DateTime'.
---> System.FormatException: 27.10.2021 12:30:00 is not a valid value for DateTime.
---> System.FormatException: String '27.10.2021 12:30:00' was not recognized as a valid DateTime.
There are several ways you could do it:
DateTime.Parse(configuration.GetValue<string>("Date"))configuration.GetValue<DateTime>("TimeModel")In all cases, you need to make sure the date is in the format: 2021-10-27T12:30:00 by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With