Convert seconds to a date string and vice versa

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Martha Amer
    New Member
    • Aug 2010
    • 3

    Convert seconds to a date string and vice versa

    Hello,

    I have a device which stores time in its flash memory as the number of seconds since 01/01/1970. One of my roles is to get/set its time. I would like to know the most efficient code to convert the time collected from the device to a readable date, then convert the updated date back to number of seconds since 01-01-1970 before sending it to the device.

    Thanks!

    Mart
  • arie
    New Member
    • Sep 2011
    • 64

    #2
    I don't know if it's the most efficient way, but something like this should work:
    Code:
    CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
    DateTime zeroPoint;
    long seconds = numberOfSecondsReturnedFromTheDevice;
    DateTime myDate;
    try
    {
       zeroPoint = DateTime.Parse("1/01/1970 00:00:00", culture);
       myDate = zeroPoint.Add(new System.TimeSpan(seconds*1000)); //timespan in miliseconds.
    }
    catch(Exception ex)
    {
       MessageBox.Show("Parsing of date failure: " + ex.Message);
    }

    Comment

    • Martha Amer
      New Member
      • Aug 2010
      • 3

      #3
      Thank you very much for your help!

      Mart

      Comment

      Working...