mylim
mylim

Reputation: 313

UWP C# Windows 10 IoT datepicker & timepicker to update system date & time

I would like to know if there is any method to update/change system date & time using datepicker & timepicker? I can't find any sample code on it for reference. Please advise. Thanks.

updated:

I encountered exception when setting the date. Is there anything that I missed out?

System Management Capabilities

Upvotes: 0

Views: 1063

Answers (2)

Michael Xu
Michael Xu

Reputation: 4432

DateTimeSettings is included in Windows IoT Extension SDK 16299.The method SetSystemDateTime is used to set the system date and time.Please refer to below codes.

    //Change Date by DatePicker
    private void DatePickerChangeSetting_DateChanged(object sender, DatePickerValueChangedEventArgs e)
    {
        DateTimeSettings.SetSystemDateTime(e.NewDate.UtcDateTime);
    }

    //Change Time by TimePicker
    private void tpChangeSetting_TimeChanged(object sender, TimePickerValueChangedEventArgs e)
    {
        var currentDate = DateTime.Now.ToUniversalTime();

        var newDateTime = new DateTime(currentDate.Year,
                                       currentDate.Month, 
                                       currentDate.Day, 
                                       e.NewTime.Hours, 
                                       e.NewTime.Minutes, 
                                       e.NewTime.Seconds);

        DateTimeSettings.SetSystemDateTime(newDateTime);
    }

@Naikrovek, in the document linked, it is not error about the parameter DateTime/DateTimeOffset.In essence, the two can be transformed into each other.Please see Converting between DateTime and DateTimeOffsetc.In addition, the issue you mentioned about displaying the updated time on 16299 has been fixed, IoTCoreDefaultApp changed to with call NativeTimeMethods.GetLocalTime method.

In addition,i posted my reference setting and appxmanifest capability setting,you can install Windows IoT Extension for the UWP from here(download the windows 10 sdk and then install). enter image description here enter image description here

Upvotes: 3

Naikrovek
Naikrovek

Reputation: 171

If you have the IoT UWP extensions referenced by your project, and you have the System Management capability added to your appxmanifest, you can set the Date and Time via this method.

The documentation linked seems ambiguous to me; it mixes mentions of use of an offset versus the actual date and time. I suspect copy paste errors, and I am not sure if this method will actually do what you need.

Note that the default home application on IoT Core 16299 has a bug that will not always display the updated time, so don't rely on that screen to know if your clock is correct until this bug is fixed.

Upvotes: 0

Related Questions