We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Date.now() not working in template

Hello, 

I want to use the Date.now() functionality in the template to compare a date with the current date, but it looks like the date object is always null, and when I use Date.now() the template renders as #TemplateName (as in an error raised probably?)

<##script type="text/x-jsrender" id="Status">
    ${if(DateFinished == null)}
    <##span>DateFinished is null!
    ${else if(Date == null)}                   <!--CONDITION IS ALWAYS TRUE!-->                
    <##span>Date is null!
   ${else if(DateFinished != null  && Date.now() > DateFinished)}    <!--THIS LINE BREAKS THE TEMPLATE, AND THE RESULT IS ALWAYS #Status-->
    <##span>Date is null!
    ${else}
    <##span>in last else statement
    ${/if}
<##/script>


P.s. How can I add code fragments into a forum post (with markup, and without have to use the <## I do above

3 Replies

MS Mydeen S N Syncfusion Team April 10, 2019 01:23 AM

Hi Thijs 
  
Greetings from Syncfusion support. 
  
Your requirement for using Date.now can be achieved by using the Custom Helpers in the template. Please find the sample custom helper and template in below code  
  
Custom helper:  
  
<script>  
window.processData = function(value){  
     
    if(value >=  5){  
        return '<span >Higher value</span>'  
    } else {  
        return  '  <span >'+ Date.now()+'</span>'  
    }  
}  
</script>  
  
template:  
<script id="template" type="text/x-jsrender" >  
   <div>${processData(data.EmployeeID)}</diV>  
</script>  
  
  
  
In the above example custom helper window.processData  we can process the data processing within the helper function and return the desired result to the template output and in the template section we can call the custom helper as in the above snippet and pass the desired arguments with data prefix (data.EmployeeID) to the helper for process and get desired output.  
  
Please get back to us, if you need any further assistance.  
  
  
Regards,    
Mydeen S N   
 



TH Thijs April 24, 2019 01:33 PM

Hello Mydeen,

Thx, this works!

The data prefix does not work (anymore) for me, I can only use the properties directly? is there something I can do to make the data prefix work? and is there a possibility that I can pass the entire data object as a parameter to the function?

Or use multiple parameters in the helper function like:

${getStatusFlag(StartDate, FinishedDate)}

window.getStatusFlag = function (date1, date2) {
     //do actions
}

in this example both dates are concatenated in the first parameter (date1) of the function

Kind regards,
Thijs


MS Mydeen S N Syncfusion Team April 25, 2019 03:12 AM

Hi Thijs, 
 
Thanks for the update. 
 
We have validated the defect you have initiated with us. Thank you for taking the time to report this issue and helping us improve our product. At Syncfusion, we are committed to fixing all validated defects (subject to technological feasibility and Product Development Life Cycle ) and including the defect fix in our upcoming weekly patch release. If you wish to receive this fix in a specific prior release product version please contact Syncfusion Support (backwards compatibility subject to technological feasibility and our Support SLA) You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link  
 
 
Regards, 
Mydeen S N 


Loader.
Live Chat Icon For mobile
Up arrow icon