Values which return the number of ticks or seconds since an arbitrary date are known as Epoch Dates. The most common one is the Unix Epoch which is the number of seconds (milliseconds in some cases) since January 1, 1970 UTC.
To convert it, you do essentially what the post describes for Excel: add that many seconds to a DateTime
variable representing the base 1/1/1964 00:00:00.0 UTC
date.
Private Function SensorEpochToDate(value As Long) As DateTime
' remove the
Return New DateTime(1904, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).
AddSeconds(value).ToLocalTime()
End Function
You can remove the .ToLocalTime()
to keep it as UTC.
Testing:
' RDG is a random data generator
' pick a random date from 1/1/1970 to today
Dim randomDate = DateTime.SpecifyKind(RDG.GetDate(DateType.UnixEpoch), DateTimeKind.Utc)
Console.WriteLine(" Selected Date: {0} (UTC)", randomDate.ToString())
' convert/emulate a sensor date
Dim sensorDate = DateToSensorValue(randomDate)
Dim realDate = SensorEpochToDate(sensorDate)
Console.WriteLine("Round trip value: {0} (Local)", realDate.ToString())
Results:
Selected Date: 8/22/1994 7:23:00 PM (UTC)
Round trip value: 8/22/1994 5:23:00 PM (Local)
It might be nice to make it an extension method to make it easier to use, but as an integer type it would show up for too many variables for my taste.