I need to pass one of my model value to the function in jsrender template. I tried using @ for accessing the C# variable, but its not working. Below is my code
<script type="text/x-jsrender" id="TemplateDate">>
{{:~formatTemplateDate(Model.EstimatedCompletionDate)}}
</script>
This is my helper function.
$.views.helpers({
formatTemplateDate: function (dateEstimated) {
"use strict";
if (dateEstimated !== null) {
if (!isSafari) {
var options = {
year: "numeric",
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit"
};
return dateEstimated.toLocaleTimeString("en-us", options);
} else {
return dateEstimated;
}
} else {
return null;
}
}
});
This is the error i get in the page
Error: n.toLocaleDateString is not a function.
Thanks in advance.
Dinesh.
I have found the fix. We cannot pass the @Model.EstimatedCompletion datetime variable value to the helper function. Need to convert it to string and in the helper function need to convert it back to Datetime(). below is the modified code.
<script type="text/x-jsrender" id="TemplateDate">>
{{:~formatTemplateDate('@Model.EstimatedCompletionDate')}}
</script>
In my helper function, am converting it back to date type, var formatedDate = new Date(date);
$.views.helpers({
format: function (date) {
"use strict";
var formatedDate = new Date(date);
if (!isSafari) {
var options = {
year: "numeric",
month: "short",
day: "numeric"
};
return formatedDate.toLocaleDateString("en-us", options);
} else {
return formatedDate;
}
}
});
Thanks,
Dinesh.