5

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

2
  • 1
    From Remarks in Documentation For date and time values before 1970-01-01T00:00:00Z, this method returns a negative value. Commented Nov 21, 2018 at 23:42
  • Yes, ok no problem. I am working with negative value. Commented Nov 21, 2018 at 23:49

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Calling ToUniversalTime() is not necessary here. ToUnixTimeMilliseconds method first converts the current instance to UTC
@Alex Very true. Thanks.
2

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.

Comments

0

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.

1 Comment

Yes, I know. I am getting negative number because the date is before 1.1.1970. But if I get -1173751200000 instead -1173747600000, it works. Why epochconverter interprets right the negative duration?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.