'formatDate': function () {
var format = "DD-MM-YYYY";
return function (today, render) {
var today = new Date(today);
return '<b>' + render(dateConvert(today,format)) + '</b>'
}
},
Now when I run this function:
aN-aN-NaN
Not sure what I have missed, any ideas why I’m seeing aN-aN-NaN and not the converted date?
When the page loads and the template is executed yes, this is the function I have so when {{submitted}} is rendered the date will be there - I have well over 20+ {{submitted}} dates that are converted.
I’ve modified dateConvert()
I generally took the skeleton to work with and slightly customised it for this instance. DD-MM-YYYY is one of the cases.
[quote=“computerbarry, post:7, topic:294435, full:true”]
When the page loads and the template is executed yes, this is the function I have so when {{submitted}} is rendered the date will be there - I have well over 20+ {{submitted}} dates that are converted.[/quote]
But didn’t your console.log show that when the function was executed, that it didn’t get the correct today value?
I think what the issue was is when using this sort of function within Mustache, we need to pass the type of object being processed, if that’s correct. Maybe I’m wrong and it’s something else?
Example:
When working with numbers (number)
'double': function () {
return function (number, render) {
return render(number) * 2;
}
}
When working with strings (text)
'wrapped': function () {
return function (text, render) {
return '<b>' + render(text) + '</b>'
}
}
And our function, even though this is a date, its a string (text)
'formatDate': function () {
var format = "DD-MM-YYYY";
return function (text, render) {
var text = new Date(today);
return '<b>' + render(dateConvert(today,format)) + '</b>'
}
},
I’m now seeing the date rendered as:
22-02-2018
Which has me thinking, how exactly are the today and text variable working together?
And what I mentioned above, does this make sense, meaning, it’s correct?
I’ve just realised every item is printing the same date.
This is because of the early console variables I was testing:
var current = '2018-02-22T12:00:00';
var today = new Date(current);
Once I remove these I now get the error:
ReferenceError: today is not defined
'formatDate': function () {
var format = "DD-MM-YYYY";
return function (text, render) {
var text = new Date(today);
return '<b>' + render(dateConvert(today,format)) + '</b>'
}
},
If I change it to
'formatDate': function () {
var format = "DD-MM-YYYY";
return function (text, render) {
var text = new Date(text);
return '<b>' + render(dateConvert(text,format)) + '</b>'
}
},
It’s just a matter of tidying up and giving an appropriate format for the dateConvert function now.
formatDate: function () {
var format = "DD-MM-YYYY";
return function (text, render) {
this.submitted = dateConvert(new Date(this.submitted), format);
...
I now feel confident about my Mustache knowledge to answer previous questions.
The function parameters never change with Mustache, it’s always text, render that are used, where text is the unrendered content, and the render function is the Mustache code that converts the unrendered content into the final output.
That was because the today variable was the unrendered text, and wasn’t the submitted date.
I’ve been working on this since Friday
Thanks a lot @Paul_Wilkins !
Change my day into a productive one
I’ll digest the information, I think I now understand what the issue was after you have explained, thanks for the detailed information, really helps.
And you’ve now learnt some new yourself - Mustache