Add one hour to summer dst - c#

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;

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();

Compare DateTimeOffset with daylight saving time

Here the DateTimeOffset instances represent the same time, but one instance has DST and other doesn't.
I would like to compare instances without DST, so the result would be True.
using System;
var today = DateTime.Today;
// Today YYYY-MM-01
var firstDayMonth = new DateTimeOffset(
new DateTime(today.Year, today.Month, 1, 0, 0, 0, today.Kind).AddHours(12));
// Add year offset from 2000
DateTimeOffset withYearOffset = firstDayMonth.AddYears(2000 - firstDayMonth.Year);
// Add month offset from 1
var withMonthOffset = withYearOffset.AddMonths(1 - withYearOffset.Month);
// Add day offset from 1
var calculated_dt_2000_1_1_12_0_0 = withMonthOffset.AddDays(1 - withMonthOffset.Day);
// 01.01.2000 12:00:00 +03:00
Console.WriteLine(calculated_dt_2000_1_1_12_0_0);
var dt_2000_1_1_12_0_0 = new DateTimeOffset(new DateTime(2000, 1, 1).AddHours(12));
// 01.01.2000 12:00:00 + 02:00
Console.WriteLine(dt_2000_1_1_12_0_0);
// False
Console.WriteLine(calculated_dt_2000_1_1_12_0_0.Equals(dt_2000_1_1_12_0_0));
Converting to UTC solves the problem.
var calculated_dt_2000_1_1_12_0_0 = withMonthOffset.ToUniversalTime().AddHours(firstDayMonth.Offset.Hours);
var dt_2000_1_1_12_0_0_withOffset = new DateTimeOffset(new DateTime(2000, 1, 1).AddHours(12));
var dt_2000_1_1_12_0_0 = dt_2000_1_1_12_0_0_withOffset.ToUniversalTime().AddHours(dt_2000_1_1_12_0_0_withOffset.Offset.Hours);

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.

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?
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

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

Categories

Resources