First of all, you don't need to ToString()
and Reparse the DateTime
. If you want just the Date
you can use the DateTime.Date
property.
Then, this should be simple enough (using DateTime.Now
as a reference point):
public static double GetTimestamp(DateTime value)
{
return new TimeSpan(DateTime.UtcNow.Ticks - value.Ticks).TotalSeconds;
}
In case the parsing you did in your question did refer to extracting the date from the DateTime
, you can use the following:
public static double GetTimestamp(DateTime value)
{
return new TimeSpan(DateTime.UtcNow.Ticks - value.Date.Ticks).TotalSeconds;
}
EDIT : You appear to want some kind of posix time conversion which can be achieved like this :
private static readonly DateTime POSIXRoot = new DateTime(1970, 1, 1, 0, 0, 0, 0);
public static double GetPosixSeconds(DateTime value)
{
return (value - POSIXRoot).TotalSeconds;
}
public static DateTime GetDateTime(double posixSeconds) {
return POSIXRoot.AddSeconds(posixSeconds);
}