Take the 2-minute tour ×

Looking for a code sample that takes a .NET CF DateTime object, and sets the device's date and time to that date (and/or time zone). The purpose of this is to get the current "real" time off of a server via web service and set the device's time correctly.

Anyone done this before?

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

You can use Opennetcf's DateTimeHelper class.

Alternatively you can p/invoke SetLocalTime.

share|improve this answer
add comment

Regarding kgiannakakis' answer: Definitely the way to go, but if you don't want to require the entire OpenNETCF runtime DLL just to set the time, download the source code to the 1.4 version of the library (there are newer ones, but the source is not freely available above v1.4) and just pull out the DateTimeHelper Class (you may need to pull some dependencies too). And then compile it directly into your code... It will save you from unnecessary external files.

share|improve this answer
add comment

P/Invoking SetSystemTime() is a better choice for this, especially if the device is in a different time zone than the server. However, there seems to be a bug in Windows Mobile 5 where it fails to properly deal with Daylight Savings Time, so sometimes the time set on the device with SetSystemTime is off by an hour.

share|improve this answer
You need to call SetLocalTime() twice. Add a 500 ms delay between the calls and then it properly sets the DST value. –  Bryan May 6 '10 at 20:23
Really? This works? –  MusiGenesis May 7 '10 at 1:09
add comment

Your Answer


