0

I got function which is converting UNIX time to DaTime which is working great anyhow now I need function to convert from DateTime to UNIX timestamp but something is wrong with this because when I am passing DateTime somehow the last 3 digits are not shown for instance I have this timestamp: "1300124700345" which is equal to: 14.03.2011 5:45 PM. When i pass that to my function I am retrieving: "1300124700" - 3 last digits dissaperd.

static readonly DateTime UnixEpoch = 
       new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

public static long ToUnixTime(DateTime date)
{
    return Convert.ToInt64((date - UnixEpoch).TotalSeconds);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Notification
  • 13
  • 1
  • 1
  • 7
  • In addition to the information you can find in the above link, time is ...fidgety, and not really accurate. Have a look at some of Jon Skeets blogs on the issue ... it's rather amusing. – Noctis Sep 01 '14 at 07:47

2 Answers2

0

Unix counts the seconds since 01.01.1970. Your function is right, it's returning TotalSeconds.

1300124700345 seconds are about 40000 years, which would be wrong.

Stephan
  • 4,187
  • 1
  • 21
  • 33
0

Also, you can have a look at this page: Epoch converter , which will give you a nice easy way to test your data.

At this: epoch on wikipedia to read some of the amusing bits about how it behaves and how accurate it is.

And use this :

var epoch = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;

to get what you want.

Noctis
  • 11,507
  • 3
  • 43
  • 82