Hello,
I'm trying to create/parse date from a JS evaluate and return them as strings back into c# code as such:
```
using (var engine = new V8ScriptEngine())
{
resultEval = engine.Evaluate(expression);
}
```
where my `expression` is
`new Date()`
and it return an "Microsoft.ClearScript.V8.V8ScriptItem" object with an "UnderlyingSystemType" saying:
'((Microsoft.ClearScript.ScriptItem)resultEval).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException'
Now, I feel like this *should* work, but it's not. Any ideas? I don't think anything went wrong during the build/reference process. Other JS libraries such as Math work flawlessly.
Also, this is only an issue when trying to create a `new` date. Calling `Date()` works, but it only displays today's date.
Comments: BTW, you could also avoid string formatting and parsing altogether: ``` C# public static class DateTimeUtil { private static readonly DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); public static double ToEpochOffset(this DateTime dateTime) { return (dateTime.ToUniversalTime() - _epoch).TotalMilliseconds; } public static DateTime FromEpochOffset(double offset) { return (_epoch + TimeSpan.FromMilliseconds(offset)).ToLocalTime(); } } ``` and then: ``` C# // export .NET types engine.AddHostType(typeof(DateTimeUtil)); engine.AddHostType(typeof(Console)); // create .NET DateTime var clrNow = DateTime.Now; Console.WriteLine(clrNow); // .NET DateTime -> JavaScript Date engine.Script.clrNow = clrNow; engine.Execute(@" jsNow = new Date(clrNow.ToEpochOffset()); Console.WriteLine(jsNow.toString()); "); // JavaScript Date -> .NET DateTime dynamic jsNow = engine.Script.jsNow; var clrNow2 = DateTimeUtil.FromEpochOffset(jsNow.valueOf()); Console.WriteLine(clrNow2); ``` Cheers!
I'm trying to create/parse date from a JS evaluate and return them as strings back into c# code as such:
```
using (var engine = new V8ScriptEngine())
{
resultEval = engine.Evaluate(expression);
}
```
where my `expression` is
`new Date()`
and it return an "Microsoft.ClearScript.V8.V8ScriptItem" object with an "UnderlyingSystemType" saying:
'((Microsoft.ClearScript.ScriptItem)resultEval).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException'
Now, I feel like this *should* work, but it's not. Any ideas? I don't think anything went wrong during the build/reference process. Other JS libraries such as Math work flawlessly.
Also, this is only an issue when trying to create a `new` date. Calling `Date()` works, but it only displays today's date.
Comments: BTW, you could also avoid string formatting and parsing altogether: ``` C# public static class DateTimeUtil { private static readonly DateTime _epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); public static double ToEpochOffset(this DateTime dateTime) { return (dateTime.ToUniversalTime() - _epoch).TotalMilliseconds; } public static DateTime FromEpochOffset(double offset) { return (_epoch + TimeSpan.FromMilliseconds(offset)).ToLocalTime(); } } ``` and then: ``` C# // export .NET types engine.AddHostType(typeof(DateTimeUtil)); engine.AddHostType(typeof(Console)); // create .NET DateTime var clrNow = DateTime.Now; Console.WriteLine(clrNow); // .NET DateTime -> JavaScript Date engine.Script.clrNow = clrNow; engine.Execute(@" jsNow = new Date(clrNow.ToEpochOffset()); Console.WriteLine(jsNow.toString()); "); // JavaScript Date -> .NET DateTime dynamic jsNow = engine.Script.jsNow; var clrNow2 = DateTimeUtil.FromEpochOffset(jsNow.valueOf()); Console.WriteLine(clrNow2); ``` Cheers!