I'm attempting to convert a C# DateTime variable into a format that can be passed into a JavaScript function using NewtonSoft.Json.
Currently, I am using the following code:
var jsonSettings = new JsonSerializerSettings();
jsonSettings.DateFormatString = "dd/MM/yyy hh:mm:ss";
string modelFromDate = JsonConvert.SerializeObject(@Model.FromDate, jsonSettings);
However, it doesn't seem to be working as expected since Chrome Dev Tools throws an error stating
modelFromDate is not defined
when running this code:
$(".go").on("click", function () {
if (new Date(modelFromDate) < new Date(1994, 1, 1)) {
alert("invalid date");
}
});
This issue occurs in my ASP.NET Core MVC project.