Hi everyone,
I'm trying to figure out a way to do two things that seem to have been made more complicated in .NET MF.
1)Get the total number of seconds since a date/time (epoch: 1/1/1970)
2)Convert that integer to it's hex string equivalent?
I used to use the TimeSpan.TotalSeconds and the .ToString("X8") to do this on a windows machine but I see in .NET MF some of these features have been removed (which I understand why) but I must admit I got lazy doing it that way that I can't figure out how to do it without
I'm sure I'm overlooking something but I haven't had much luck searching the forums or google. Eventually I want to send some data over a cellular network where byte counts matter and trying to keep things as tight as possible.
Any help is certainly appreciated.
Cheers,
Fish
How To Calculate Seconds Since Date/Time?
Started by fishberg5051, Nov 30 2012 04:05 PM
4 replies to this topic
#1
Posted 30 November 2012 - 04:05 PM
#2
Posted 30 November 2012 - 04:20 PM
Sorry to post on my own questions but I finally found one answer I was looking for.
Maybe it'll help others...
Maybe it'll help others...
public static long GetEpochTime() { DateTime dtCurTime = DateTime.Now; DateTime dtEpochStartTime = new DateTime(1970, 1, 1); TimeSpan ts = dtCurTime.Subtract(dtEpochStartTime); long epochtime; epochtime = ((((((ts.Days * 24) + ts.Hours) * 60) + ts.Minutes) * 60) + ts.Seconds); return epochtime; }
#3
Posted 30 November 2012 - 04:24 PM
Good you solved it
You could also have done like this: return ts.Ticks / 10000000;
ntools
TCP Listener (Beta) · FTP Server (Alpha)
Netduino Plus Go Module · Xml Parser
http://ntools.codeplex.com/
#4
Posted 30 November 2012 - 04:31 PM
Thanks for the reply Nicky, that does seem pretty simple.
I found that formula written in "C" (2nd to last line) didn't even consider ticks :S
#5
Posted 06 July 2013 - 06:08 AM
Good you solved it
You could also have done like this: return ts.Ticks / 10000000;
Actually this solution doesn't work. But it worked fishberg5051 ones.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users