NiL.JS icon indicating copy to clipboard operation
NiL.JS copied to clipboard

DateTime auto convert to UTC

Open alexhome82 opened this issue 7 years ago • 4 comments

engine.DefineVariable("myCallObject").Assign(JSValue.Marshal(new MyCallObject));

public class MyCallObject {
    public void SetDateTime(JSValue dt) {
        var t = dt; <-------------------------------------- !!!! Time translates into UTC why?
    }
}
engine.Eval(@"
myCallObject.SetDateTime(new Date());
")

And how do I make the time come the same as in Javascript?

alexhome82 avatar Apr 14 '18 17:04 alexhome82

In your code you're looking at the JSValue so I'm not sure exactly what you wanted back out at that point. All times in javascript are UTC, but when you convert them to a string or request individual elements (date/hours/minutes) the browser looks at your local timezone and adjusts the output accordingly. So it depends what you want to do with the output. Do you want it as a .NET DateTime object?

I just ran your code, and I got this: image

This looks fine to me, as that is the local time here. What were you expecting to see? There are issues with daylight saving times in the existing code (there's a fixed version I wrote here: https://github.com/questmetrics/NiL.JS/blob/develop/NiL.JS/BaseLibrary/Date.cs ) but I'm not sure if this is related to your problem.

jsobell avatar Apr 16 '18 10:04 jsobell

must be 09.04.2018 00:00:00 image

alexhome82 avatar Apr 16 '18 11:04 alexhome82

Well it says your TimeZoneOffset is zero for some reason. Can you see what TimeZoneInfo.Local.GetUtcOffset(DateTime.Now) gives on your PC? I'm not sure why you get this, but you can try building my fork locally and see if it has the same issue on your PC.

jsobell avatar Apr 16 '18 14:04 jsobell

Приветствую. Тоже на этот баг напоролся.

    [TestMethod]
    public void DateStringify() {
      var dt  = new DateTime(2018, 05, 15, 10, 05, 02);  // {15.05.2018 10:05:02}
      var dt_j = JSC.JSObject.Marshal(dt);  //  {Tue May 15 2018 10:05:02 GMT+0200 (W. Europe Daylight Time)}
      var json = JSL.JSON.stringify(dt_j);  // "2018-05-15T08:05:02.000Z"
      var j_s = JSL.JSON.parse(json);
      var j_dt = new JSL.Date(new JSC.Arguments() { j_s });  // {Tue May 15 2018 08:05:02 GMT+0200 (W. Europe Daylight Time)}
      var dt_o = j_dt.ToDateTime();  // {15.05.2018 08:05:02}
      Assert.AreEqual(dt, dt_o);  // failed. Expected:<15.05.2018 10:05:02>. Actual:<15.05.2018 08:05:02>.
    }

Wassili-Hense avatar Jun 21 '18 13:06 Wassili-Hense