I'm having some trouble with date marshaling as well. In my scenario I'm using JavaScript as a hook into how the application performs certain actions. I would like to expose a clean interface that leverages the native functionality in JavaScript including Date. This is important because I'm prioritizing the JavaScript syntax over the work it would take to accomplish it on the .NET side.
Here is my ideal script:
Even if I use a .NET DateTime.Now to compare my DateTime variables I still get false on both outputs.
The only solution I've found is to use method to compare the dates.
Has anyone else run into this? Why doesn't the compare operations work with both objects are .NET DateTime objects? Any better solutions? Any ideas, thoughts, insights would be much appreciated.
Regards,
Jamin
Here is my ideal script:
JScriptEngine engine = new JScriptEngine();
DateTime yesterday = DateTime.Now.AddDays( -1 );
DateTime tomorrow = DateTime.Now.AddDays( 1 );
engine.AddHostObject( "yesterday", yesterday );
engine.AddHostObject( "tomorrow", tomorrow );
StringBuilder scriptBuilder = new StringBuilder();
scriptBuilder.AppendLine( "var now = new Date();" );
scriptBuilder.AppendLine( "var todayAfterYesterday = now > yesterday;" );
scriptBuilder.AppendLine( "var todayAfterTomorrow = now > tomorrow;" );
engine.Execute( scriptBuilder.ToString() );
Console.WriteLine( "TodayAfterYesterday: " + engine.Script.todayAfterYesterday );
Console.WriteLine( "TodayAfterTomorrow: " + engine.Script.todayAfterTomorrow );
This example returns false for both outputs.Even if I use a .NET DateTime.Now to compare my DateTime variables I still get false on both outputs.
JScriptEngine engine = new JScriptEngine();
DateTime yesterday = DateTime.Now.AddDays( -1 );
DateTime tomorrow = DateTime.Now.AddDays( 1 );
engine.AddHostObject( "yesterday", yesterday );
engine.AddHostObject( "tomorrow", tomorrow );
engine.AddHostObject( "now", DateTime.Now );
StringBuilder scriptBuilder = new StringBuilder();
scriptBuilder.AppendLine( "var todayAfterYesterday = now > yesterday;" );
scriptBuilder.AppendLine( "var todayAfterTomorrow = now > tomorrow;" );
engine.Execute( scriptBuilder.ToString() );
Console.WriteLine( "TodayAfterYesterday: " + engine.Script.todayAfterYesterday );
Console.WriteLine( "TodayAfterTomorrow: " + engine.Script.todayAfterTomorrow );
This returns false to both outputs as well.The only solution I've found is to use method to compare the dates.
JScriptEngine engine = new JScriptEngine();
DateTime yesterday = DateTime.Now.AddDays( -1 );
DateTime tomorrow = DateTime.Now.AddDays( 1 );
engine.AddHostObject( "DateTimeComparer", new DateTimeComparer() );
engine.AddHostObject( "yesterday", yesterday );
engine.AddHostObject( "tomorrow", tomorrow );
engine.AddHostObject( "now", DateTime.Now );
StringBuilder scriptBuilder = new StringBuilder();
scriptBuilder.AppendLine( "var todayAfterYesterday = DateTimeComparer.DifferenceInDays( now, yesterday );" );
scriptBuilder.AppendLine( "var todayAfterTomorrow = DateTimeComparer.DifferenceInDays( now, tomorrow );" );
engine.Execute( scriptBuilder.ToString() );
Console.WriteLine( "TodayAfterYesterday: " + engine.Script.todayAfterYesterday );
Console.WriteLine( "TodayAfterTomorrow: " + engine.Script.todayAfterTomorrow );
This example does return 1 and -1.Has anyone else run into this? Why doesn't the compare operations work with both objects are .NET DateTime objects? Any better solutions? Any ideas, thoughts, insights would be much appreciated.
Regards,
Jamin