ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

ToUnixTimestamp

Converts a System.DateTime object to Unix timestamp.

Source

/// <summary>
/// Converts a System.DateTime object to Unix timestamp
/// </summary>
/// <returns>The Unix timestamp</returns>
public static long ToUnixTimestamp(this DateTime date)
{
	DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0);
	TimeSpan unixTimeSpan = date - unixEpoch;

	return (long)unixTimeSpan.TotalSeconds;
}

Example

var currentUnixTimestamp = DateTime.Now.ToUnixTimestamp();

Author: Koen Rouwhorst

Submitted on: 11 apr. 2010

Language: C#

Type: System.DateTime

Views: 8927