MrScf
MrScf

Reputation: 2497

C# - Convert date to unix timestamp

I am trying to convert the date Saturday, 22. October 1932 00:00:00 to the unix timestamp -1173747600000.

My code here:

DateTimeOffset dt2 = new DateTimeOffset(new DateTime(1932, 10, 22)).ToUniversalTime();
long a = dt2.ToUnixTimeMilliseconds();

I am getting the timestamp -1173751200000. What are I doing wrong?

Epochconverter.com is calculating as expected the unix timestamp. See local time option and then Timestamp in milliseconds

Upvotes: 5

Views: 15447

Answers (3)

Gabriel Luci
Gabriel Luci

Reputation: 40928

You're forgetting the time zone. The only time zone where 22 October 1932 00:00 equals -1173747600000 is UTC-01:00

There isn't any way in .NET (that I can find) to create a DateTime in a time zone other than local or UTC, so you have to just subtract an hour (from UTC), which takes you to 21 October 1932 23:00 UTC:

var date = new DateTime(1932, 10, 21, 23, 0, 0, DateTimeKind.Utc);
var dt2 = new DateTimeOffset(date);
long a = dt2.ToUnixTimeMilliseconds();

That results in -1173747600000.

Upvotes: 4

Callie J
Callie J

Reputation: 31296

The first clue is the difference in the timestamps: they're 3600 seconds apart, or in other words one hour. My guess is that there's a daylight savings issue coming in to play.

You can see that this is being applied by DateTimeOffset by looking at the properties of the object. Using it in Powershell:

$t = [datetime]::Parse("1932-10-22")
new-object system.datetimeoffset($t)

gives output:

DateTime      : 22/10/1932 00:00:00
UtcDateTime   : 21/10/1932 23:00:00
LocalDateTime : 22/10/1932 00:00:00
Date          : 22/10/1932 00:00:00
Day           : 22
DayOfWeek     : Saturday
DayOfYear     : 296
Hour          : 0
Millisecond   : 0
Minute        : 0
Month         : 10
Offset        : 01:00:00
Second        : 0
Ticks         : 609618528000000000
UtcTicks      : 609618492000000000
TimeOfDay     : 00:00:00
Year          : 1932

DateTimeOffset.ToUniversalTimeMilliseconds() returns the unix-time from the UTC value of the datetime.

So you need to create the DateTimeOffset with the UTC timezone instead, so (again with PS, but it's trivial to convert to C#)

$t = [datetime]::Parse("1932-10-22")^C
$ofs = new-object System.Timespan(0)
new-object system.datetimeoffset($t, $ofs)

Gives:

DateTime      : 22/10/1932 00:00:00
UtcDateTime   : 22/10/1932 00:00:00
LocalDateTime : 22/10/1932 01:00:00
Date          : 22/10/1932 00:00:00
Day           : 22
DayOfWeek     : Saturday
DayOfYear     : 296
Hour          : 0
Millisecond   : 0
Minute        : 0
Month         : 10
Offset        : 00:00:00
Second        : 0
Ticks         : 609618528000000000
UtcTicks      : 609618528000000000
TimeOfDay     : 00:00:00
Year          : 1932

Now this gives an millisecond epoch timestamp of -1173744000000, which is different to the value you're expecting. I've checked a couple of sources, including epochconvertor.com and this does appear to be the correct time. The timestamp you've provided, -1173747600000, is 21 Oct 1932 at 23:00:00.

Upvotes: 2

Martin Heralecký
Martin Heralecký

Reputation: 5779

UNIX timestamp is the number of seconds that passed since 1. 1. 1970. That is why you get a negative number for a date that preceds it.

Upvotes: 0

Related Questions