Set The System DateTime on Windows IoT

Menu > Project Add Reference > Universal Windows > Extensions > ensure ‘Windows IOT Extensions for UWP’ is checked

Double click Package.appxmanifest > Capabilities > ensure ‘System Management’ is checked

	//----- SET THE SYSTEM CLOCK -----
	DateTime DateTime1 = new DateTime(2007, 1, 12, 0, 0, 0);				//Set the required DateTime
	DateTimeOffset DateTimeOffset1 = new DateTimeOffset(DateTime1, TimeZoneInfo.Local.BaseUtcOffset);   //The TimeSpan sets the difference from UTC which is required for DateTimeOffset
	if (TimeZoneInfo.Local.IsDaylightSavingTime(DateTime1))			//Handle currently in daylight saving time
		DateTimeOffset1 = DateTimeOffset1.AddHours(-1);
	Windows.System.DateTimeSettings.SetSystemDateTime(DateTimeOffset1);                         //Set the system DateTime

	//DateTime DateTime2 = DateTime.Now;			//Read the system DateTime

Setting from Date Picker

	private async void TimePicker1_TimePicked(object sender, TimePickedEventArgs e)
	{
		try
		{
			//----- GET THE TIME ENTERED -----
			TimeSpan TimeSpan1 = TimePicker1.Time;
			int Hour = TimeSpan1.Hours;
			int Minute = TimeSpan1.Minutes;

			//----- SET THE SYSTEM CLOCK -----
			DateTime DateTime1 = new DateTime(
				DateTime.Now.Year,
				DateTime.Now.Month,
				DateTime.Now.Day,
				Hour,
				Minute,
				0);
			DateTimeOffset DateTimeOffset2 = new DateTimeOffset(DateTime1, TimeZoneInfo.Local.BaseUtcOffset);   //The TimeSpan sets the difference from UTC which is required for DateTimeOffset
			if (TimeZoneInfo.Local.IsDaylightSavingTime(DateTime1))			//Handle currently in daylight saving time
				DateTimeOffset2 = DateTimeOffset2.AddHours(-1);
			Windows.System.DateTimeSettings.SetSystemDateTime(DateTimeOffset2);                         //Set the system DateTime
		}
		catch (Exception ex)
		{
			System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
		}
	}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *