Convert DateTime to UNIX timestamp And UNIX timestamp to DateTime

Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970
More Info

So the following two methods convert a DateTime to UNIX timestamp & vice-versa.


internal static string DateTimeToUnixTimeStamp(DateTime date)
{
	return (date - new DateTime(1970, 1, 1, 0, 0, 0).ToLocalTime()).TotalSeconds.ToString();
}

internal static DateTime UnixTimeStampToDateTime(string totalSeconds)
{
	return (new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(Convert.ToDouble(totalSeconds)).ToLocalTime());
}

Advertisements