Convert valueto HH:MM:SS

I have an int number of seconds, such as qwe=12345

I want to convert it to an equiv time string 03:25:45
(that’s 3 hours, 25 minutes, 45 seconds)

I’ve fiddled around with timespans, datetime, etc to no avail…any thoughts?

 DateTime tttt = new DateTime(2000, 1, 1, 0, 0, 0);
                Debug.Print("getting "+ tttt.ToString("HH:mm:ss"));

gives 00:00:00

Don’t forget to reassign the date variable. Try this:

DateTime tttt = new DateTime(2000, 1, 1, 0, 0, 0);
tttt = tttt.AddSeconds(12345);
Debug.Print("getting "+ tttt.ToString("HH:mm:ss"));

results in debug output of

getting 03:25:45

Thanks…that does the trick…not sure why my method did not work …what does it do?
This seems a bit strange, but maybe I shouldn’t ask too many questions!

also is there a more direct conversion (other than a 2 step, with add seconds)

In your case, it adds the seconds to the internal DateTime value and returns the result.

Much like String.Replace in desktop .NET.

You could also do this:

 tttt += new TimeSpan(3, 25, 45);

[EDIT] I retracted the comment about this overload not being available. Happily, it is! Check out MSDN for more details on what mathematical operators are supported with DateTime in NETMF.

I really can’t use addseconds, since I need to convert several different variables & adding keeps incrementing the total time & there does not seem to be a quick way to reset the time back to zero.
So I’m probably looking for more of a direct conversion, ithis is only for string output, converted values not kept for any purpose.

Why not reinitialize the DateTime variable? It doesn’t get any quicker.

You’ll need to explain that last statement a bit more I suspect…

ttt = new DateTime(2000, 1, 1, 0, 0, 0);
aaa = ttt.addseconds(123);
bbb = ttt.addseconds(987);
ccc = bbb.addseconds(555);

or something similar…

ttt = new DateTime(2000, 1, 1, 0, 0, 0);
aaa = ttt.addseconds(123);
Debug.Print("getting "+ aaa.ToString("HH:mm:ss"));
aaa = ttt.addseconds(987);
Debug.Print("getting "+ aaa.ToString("HH:mm:ss"));
aaa = aaa.addseconds(555);
Debug.Print("getting "+ aaa.ToString("HH:mm:ss"));

just keep the "source’ time unedited and use everything else.

Of course, without testing, here’s another throw away line:

Debug.Print("getting "+ ttt.addseconds(555).ToString("HH:mm:ss"));

I can do something like this …not actually permanently adding the seconds:

 Debug.Print("getting "+ tttt.AddSeconds(12345).ToString("HH:mm:ss"));
                Debug.Print("getting "+ tttt.AddSeconds(12777).ToString("HH:mm:ss"));

the numbers are replaced by variable names in the actual program

@ Brett–thanks, our message overlapped…you gave some other good ideas I might neeed in the next section I’m creating

here is a different question: (the original problem has been solved–thanks)

if I do:


all by itself, what does it do? It doesn’t generate an error…This led me on the path to nowhere. Seems like it should do something or cause an exception

It just creates a new DateTime and returns it to nowhere land. There’s nothing wrong with that but it’s not particularly useful :slight_smile:

AddSeconds is a Function so it is returning the modified data (result)… you are thinking of it as if it were a void PROCEDURE that returns nothing…

if you have Resharper installed for example it will give you a warning that this function’s return is not set anywhere and it is useless.

look at the return value…

As a side note - in C# there are no functions, only methods :wink:

Unless it is an ‘Anonymous Function’ :slight_smile:

1 Like

Ok I partialy agree :wink: What i meant was that there are no functions in C# and this is true. Anonymous Function (Lambda Expression for example) is represented in MISL as a method in a nested class. But since those were named ‘Functions’ I guess it’s ok to use this term as long as you know what realy happend under the hood.

@ Gralin - Those language designers and their terminology.

Unfortunately there are functions in C#, the C# language specification uses the term function in numerous places and of course the very fact the spec refers to anonymous functions, gives us a language construct known as a function albeit anonymous.

I 100% agree that IL does not have functions, but IL is not C#. If .NET languages relied on IL to define there domain terminology, that would mean that VB.NET does not have functions even though the very keyword used to declare a function is Function. What about F#, it compiles to IL, does that mean it not have functions?

1 Like

@ taylorza - I see your point and it sound valid. I guess i have to change my mind about this :slight_smile: