+2  A: 

Congratulations... Your solution is perfect ;)

ŁukaszW.pl
+11  A: 

That's basically it. These are the methods I use to convert to and from Unix epoch time:

public static DateTime ConvertFromUnixTimestamp(double timestamp)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds(timestamp);
}

public static double ConvertToUnixTimestamp(DateTime date)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = date.ToUniversalTime() - origin;
    return Math.Floor(diff.TotalSeconds);
}
Dave Swersky
Uhoh, I didn't think about UTC, good point!
Slider345
+2  A: 

The only thing I see is that it's supposed to be since Midnight Jan 1, 1970 UTC

TimeSpan span= DateTime.Now.Subtract(new DateTime(1970,1,1,0,0,0, DateTimeKind.Utc));
return span.TotalSeconds;
Chad
A: 

You can create a startTime and endTime of DateTime, then do endTime.Subtract(startTime). Then output your span.Seconds.

I think that should work.

Eclyps19
+3  A: 

You probably want to use DateTime.UtcNow to avoid timezone issue

TimeSpan span= DateTime.UtcNow.Subtract(new DateTime(1970,1,1,0,0,0)); 
David
A: 

That approach will be good if the date-time in question is in UTC, or represents local time in an area that has never observed daylight saving time. The DateTime difference routines do not take into account Daylight Saving Time, and consequently will regard midnight June 1 as being a multiple of 24 hours after midnight January 1. I'm unaware of anything in Windows that reports historical daylight-saving rules for the current locale, so I don't think there's any good way to correctly handle any time prior to the most recent daylight-saving rule change.

supercat
A: 

Depending of your needs, it might be of interest to include or exclude leap seconds. I'm not sure if the DateTime takes them in consideration.

Julien Bérubé