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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s