I am using the settings plugin and I have it working to store some booleans. Now I wanted to add managing a DateTime object. I added the following to Settings.cs:
private const string TimeRemainingKey = "time_remaining"; private static readonly DateTime TimeRemainingDefault = DateTime.Now; public static DateTime TimeRemaining { get { return AppSettings.GetValueOrDefault(TimeRemainingKey, TimeRemainingDefault); } set { AppSettings.AddOrUpdateValue(TimeRemainingKey, value); } }
Originally I used the following in my code:
Settings.TimeRemaining = new DateTime().AddMinutes(30);
When I added some logging I had this:
DateTime dt = new DateTime(); Debug.WriteLine(dt.ToString()); dt = dt.AddMinutes(30); Debug.WriteLine(dt.ToString()); Settings.TimeRemaining = dt; Debug.WriteLine(Settings.TimeRemaining.ToString());
It prints out:
1/1/0001 12:00:00 AM
1/1/0001 12:30:00 AM
1/1/0001 12:00:00 AM
Why does this behavior occur?
The settings plugin converts the DateTime to UTC so it looks like in your timezone when it converts 1/1/0001 12:30:00 AM to UTC it gets 1/1/0001 12:00:00 AM. As a result when the value is read back from settings you get 1/1/0001 12:00:00 AM.
If you set the Kind
for your date the plugin should work correctly:
Settings.TimeRemaining = DateTime.SpecifyKind(new DateTime().AddMinutes(30), DateTimeKind.Utc);
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