Time does't work in DateTime function

I test this code but the time display return => 00:00:00


public partial class Program
    {
        DateTime DT = DateTime.Now;
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            Debug.Print(DT.ToString("HH:mm:ss"));
        }
    }

I have spider main board and i use Gedgeteer (NETMF 4.2).

Now the time is: 00:31:50

In my opinion, if DateTime shows 00:31:50, this means that it works. What are you expecting?

1 Like

My firmware is 4.2.7.0.

I use USB for power my spider.

I want to retrieve the time for save on my SDCard.

If your device does not have a battery backup for RTC (usually a CR2032) then your RTC is 0 at reboot. Then RTC starts counting and after an hour without switching of the power supply, it should print 01:00:00.

If you want to set your RTC to a particular hour you can use ntp or any time server you can even imagine something by Rs232 or radio, or you can set it manually:


DT = new DateTime(2012,6,25,12,12,0); // This will set the clock to june 25 of 2012 at 12:12:00
RealTimeClock.SetTime(DT); //This will set the hardware Real-time Clock to what is in DT
Utility.SetLocalTime(RealTimeClock.GetTime());

1 Like

Thank you for your answers, I’ll try all it.

To synchronize your board datetime, you have several solutions :

  1. Manually set the DT in Main at startup, but this is not consistent, and you will have to sync your board launch with your clock. It has only interest if you dont care about exact date and only wnat to play with timspan (that is the same if you do nothing, as it will start at 00:00:00 as you already seen),

  2. Embed an http server and design an html page that retrieve the PC date in javascript (with autorefresh each second) and place a submit to sync your board with the PC Datetime. Not that in this case, if no RT Quartz and no battery is sold to the board, you will have to process the sync after eache boot, and before starting any treatment that require real DT. If RTClock is ok, then you will have to process only once.

  3. Use a class called Timeservice that you initialize at boot and that will retrieve time from an NTP Server on the net. It is the most integrated solution as it does not need to have RTClock (except if the NTP Server cant be joined when sync demand is sent by the board),<and it also manage timezones and daylight saving for you !

Look at this link for the class descriptionn : Microsoft.SPOT.Time Namespace | Microsoft Learn

Many samples are available on the forum or at least on google…

1 Like

Does TimeService work in 4.2 or at least with the Cerberus in 4.2 as I had trouble getting it going as it threw an exception when I tried to set it?

1 Like

I’m having no problems setting it in 4.2 using:


Microsoft.SPOT.Hardware.Utility.SetLocalTime(localTime);

1 Like

I’m talking about using the Microsoft.SPOT.Time


 TimeServiceSettings NTPTime = new TimeServiceSettings();

            NTPTime.AutoDayLightSavings = true;
            NTPTime.ForceSyncAtWakeUp = true;
            NTPTime.RefreshTime = 3600;

            NTPTime.PrimaryServer = Dns.GetHostEntry("time.windows.com").AddressList[0].GetAddressBytes();
            NTPTime.AlternateServer = Dns.GetHostEntry("time.nist.gov").AddressList[0].GetAddressBytes();

            try
            {
                TimeService.Settings = NTPTime; //blows up here
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }


1 Like

@ andre.marschalek -

Javascript is executed client side, so that if you instanciate a variable and set it to the current datetime, it will be the client datetime (the PC in fact). You can execute this script on form load, and put the result in an INPUT, so that when submitting, will be present in the POSTS.
Then, a submit button make you confirm and apply the given datetime to the device…

1 Like

And I Also have a javascript tha tplay with a “downcounter” to tell the user when the device will reboot in ‘N’ seconds, and use an autorefresh loop that could probably serve to refresh the datetime also…

1 Like

My purpose is not to say, you must do that, but to relate several solutions according that if you “not only” need to implement an Http server (which can be a solution for IP config and device specific params, you can also think about adding a page to manage Time sync, and even to manager NTP Settings in case of using an NTP Sync model.

TimeService may not be part of the Cerb-family firmware. But there is progress in using GCC to compile the Cerb-family firmware for custom firmwares. Please see this post for more details: http://www.tinyclr.com/forum/topic?id=10318

1 Like

All this have to be put in perspective of the project context and network availabilities of course, but something that can not be negociated as far as I’m concerned, is to forget using any proprietary implementation !

I prefer implementing a NTP Standard than building some unscalable codes to perform a task, however it is a bit more complicated, but so reusable !

1 Like

@ andre.marschalek -

This is the less complicated, this is not proprietary, and this is also what I deal with in my proposal N°3, that is the best practice when having access to an NTP Server !

So in fact, we agree on every things but we are running around the camp with the same speed and direction, waiting the moment we will meet each other :wall:

1 Like

@ andre.marschalek - Fine !

At the origin, my page was build to manage NTP params, and set them to the board, and I remembered that in some costumers cases, I wont have access to an NTP Server, so that I decided to add a couple of fields in the page to check between (checkbox) :

  • Set from local host (jscript),
    OR
  • Set from NTP (TimeService),

In the meantime, I did some things with DPWS and finally had the idea that NTP params or current DT should also be given by the server thru the device, and also add this functionnality.

WIth that flexibility, I cover 150% of my needs :lol:

1 Like