SB2055
SB2055

Reputation: 12852

DateTime from C# an hour late?

My client application requires, from the server, "how many seconds between some value and 1970".

I'm testing this with the following code:

var span = (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime()).TotalSeconds;

return span;

But if I convert the result from this unix time, I get something that's an hour later than now, so my client application is not behaving as expected.

What's going on?

Upvotes: 0

Views: 865

Answers (3)

punkologist
punkologist

Reputation: 761

try:

TimeSpan span = DateTime.Now.Subtract(new DateTime(1970,1,1,0,0,0));

Then use span.TotalSeconds

Upvotes: -1

fcuesta
fcuesta

Reputation: 4520

Try with UTC times:

(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).TotalSeconds;

Upvotes: 6

user1088520
user1088520

Reputation:

Is this what you're looking for?

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

return span;

Upvotes: 2

Related Questions