Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

How To Calculate Seconds Since Date/Time?


  • Please log in to reply
4 replies to this topic

#1 fishberg5051

fishberg5051

    New Member

  • Members
  • Pip
  • 7 posts

Posted 30 November 2012 - 04:05 PM

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 :P 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

#2 fishberg5051

fishberg5051

    New Member

  • Members
  • Pip
  • 7 posts

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...

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 Nicky

Nicky

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationDenmark

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 fishberg5051

fishberg5051

    New Member

  • Members
  • Pip
  • 7 posts

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 elettrozero

elettrozero

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts

Posted 06 July 2013 - 06:08 AM

Good you solved it Posted Image

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

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.