DateTime from C# an hour late? - c#

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?

Try with UTC times:
(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).TotalSeconds;

Is this what you're looking for?
var span = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
return span;

try:
TimeSpan span = DateTime.Now.Subtract(new DateTime(1970,1,1,0,0,0));
Then use span.TotalSeconds

Related

Convert unixtime to DateTime immediately

There was a problem converting unixtime to DateTime.
I am passing in parameter 1663869600 this is September 22, 22. But in the code, after instrumentation, I get the date 1/20/1970 6:11:09 AM.
Why is that ?
I will convert the date in the following ways:
DateTime start = DateTimeOffset.FromUnixTimeMilliseconds(request.StartTime).DateTime;
var startUtc = DateTime.SpecifyKind(start, DateTimeKind.Utc);
and
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var startUtc = dtDateTime.AddMilliseconds(request.StartTime).ToLocalTime();
Unix time is number of seconds (not milliseconds) since the epoch (as #Gus correctly mentioned). So the solution is to use FromUnixTimeSeconds instead of FromUnixTimeMilliseconds and AddSeconds instead of AddMilliseconds.
DateTime start = DateTimeOffset.FromUnixTimeSeconds(1663869600).DateTime;
var startUtc = DateTime.SpecifyKind(start, DateTimeKind.Utc);
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var startUtc2 = dtDateTime.AddSeconds(1663869600).ToLocalTime();

Why is my function not returning the correct value in seconds?

How come that s returns 7942 when it should be 13 minutes aka roughly 780 seconds?
var s = Math.Round((DateTime.Now - FromUnixTime(1589414482)).TotalSeconds);
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var date = DateTime.Now;
DateTime FromUnixTime(long unixTime)
{
return epoch.AddSeconds(unixTime);
}
DateTime.Now returns a local time. Unix Epoch times are based on UTC. That's your difference.

Add one hour to summer dst

I have the follow code to get the local time in ms:
var dtNow = DateTime.Now;
var time = TimeSpan.FromMilliseconds((dtNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()).TotalMilliseconds);
long end_time = Convert.ToInt64(time.TotalMilliseconds);
The time object indicate to correct hour (11:20:00) but the ms object indicate on 12:20:00, Why its happend and how i can fix it?
Before the summer dst Its works perfecr.
Thanks!
Because your dtNow = DateTime.Now; is local and with (dtNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()) you're converting the time to local again .ToLocalTime()
Try:
var dtNow = DateTime.UtcNow;

C# version of Javascript Date.getTime()

What is the best way in c# to get the same result of javascript date.gettime() call?
The getTime() method returns the number of milliseconds since midnight of January 1, 1970 and the specified date.
You can use this solution:
private int GetTime()
{
var time = (DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1));
return (int)(time.TotalMilliseconds + 0.5);
}
Since JavaScript time is with respect to UTC, I think you will need something like this:
var st = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var t = (DateTime.Now.ToUniversalTime() - st);
// t.TotalMilliseconds
Now you can use the TotalMilliseconds property of the Timespan.
The correct implementation (assuming the current time) is as follows:
DateTime utcNow = DateTime.UtcNow;
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long ts = (long)((utcNow - epoch).TotalMilliseconds);
The Java and JavaScript Date.getTime() methods return the number of milliseconds since 1 Jan 1970 00:00:00 GMT.
Since .NET
represents dates in Ticks (1 Tick = 0.1 nanoseconds or 0.0001 milliseconds) since 1 Jan 0001 00:00:00 GMT, we must use a
conversion formula where 621355968000000000 is the offset between the base dates in Ticks and 10000 the number of Ticks per
Millisecond.
Ticks = (MilliSeconds * 10000) + 621355968000000000
MilliSeconds = (Ticks - 621355968000000000) / 10000
I guess this will do the trick :)
public double MilliTimeStamp(DateTime TheDate)
{
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = TheDate.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
(DateTime.Now - new DateTime (1970, 1, 1)).TotalMilliseconds
Now in C# you can use built-in function:
new DateTimeOffset(Your_DateTime_Variable_Here).ToUnixTimeMilliseconds()
So the sample code would be:
var dateToUse = DateTime.Now;
var javaGetTimeValue = new DateTimeOffset(dateToUse).ToUnixTimeMilliseconds()
The currently accepted answer returns an int which is incorrect. It has to be Int64 or long. This is just rewriting the correct answer provided by Matt Johnson-Pint (and edited by Adaptabi) as one line. Please accept Matt Johnson-Pint's answer. I checked it against actual javascript new Date().getTime() in the console to verify it returns the same number.
long JavascriptGetTime()
{
return (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
Here is an extension method based off Enigma State's answer
public static Int64 GetJavascriptTimeStamp(this DateTime dt)
{
var nineteenseventy = new DateTime(1970, 1, 1);
var timeElapsed = (dt.ToUniversalTime() - nineteenseventy);
return (Int64)(timeElapsed.TotalMilliseconds + 0.5);
}
To use it for the current time:
var timeStamp = DateTime.Now.GetJavascriptTimeStamp();
private static ulong GetTime()
{
const long INIT_DATA_TICKS = 621355968000000000; // 1.1.1970 in ticks
const double ROUNDINGS_FIX = 0.5;
TimeSpan dTicks = new TimeSpan(DateTime.UtcNow.Ticks - INIT_DATA_TICKS);
return (ulong)(dTicks.TotalMilliseconds + ROUNDINGS_FIX);
}
DateTime.Now.ToUniversalTime() use intern DateTime.UtcNow

PHP mktime() and microtime() equivalent in C#

What is the equivalent of PHP mktime and microtime in C#?
Here's for mktime (you'll have to verify timezones, though):
static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}
static int ConvertToUnixTimestamp(DateTime date)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = date - origin;
return (int)diff.TotalSeconds;
}
microtime is basically the same, but you don't have to cast to int.
There are no direct equivalents, but they can easily be implemented... Since the UNIX timestamp is the number of seconds since January 1st 1970, it's easy to calculate :
public readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);
// equivalent to PHP mktime :
public int GetUnixTimestamp(DateTime dt)
{
TimeSpan span = dt - UnixEpoch;
return (int)span.TotalSeconds;
}
For microtime, you can use the DateTime.Tick property (1 tick = 100 nanoseconds, so 10 ticks = 1 microsecond)

Categories

Resources