How to handle Date/Time values in JSRender on JSReports



  • I am processing from JSON feed a DateTime field using JSRender {{:DateOccurred}} in the output PDF.
    It shows 2017-01-31T00:00:00, How do I handle it just it show dd/MM/yyyy only?

    Many thanks


  • administrators

    you can make a jsrender helper that takes the datetime field and returns a string with your desired format.

    and inside your helper you can write your own logic to convert a date to a format or you can use a library like moment or date-fns



  • Revisiting this. Never did get it to work
    Following the example on https://www.jsviews.com/#helpers
    give me this exception event so I've copied and pasted the example

    Error occured - Error during rendering report: view.hlp(...) is not a function
    Stak - TypeError: view.hlp(...) is not a function
    at Function.eval [as fn] (eval at buildCode ([eval]:127512:10), <anonymous>:39:24)
    at renderWithViews ([eval]:126864:18)
    at Function.renderContent [as render] ([eval]:126719:13)
    at [eval]:477:19
    at evalmachine.<anonymous>:1:41
    at ContextifyScript.Script.runInNewContext (vm.js:18:15)
    at Object.exports.runInNewContext (vm.js:49:17)
    at module.exports ([eval]:397:8)
    at ScriptsManager.execute ([eval]:273021:34)
    at tryCatcher ([eval]:47244:23)
    at ret (eval at <anonymous> ([eval]:45923:1), <anonymous>:13:39)
    From previous event:
    at Promise.longStackTracesCaptureStackTrace [as _captureStackTrace] ([eval]:43056:19)
    at ret (eval at <anonymous> ([eval]:45923:1), <anonymous>:8:21)


  • administrators

    i linked the jsviews helpers page just for you to see the jsrender syntax to call a helper. helpers for an engine are defined in a more generic way (a simple function) in jsreport.

    here is link showing a basic example of this https://playground.jsreport.net/studio/workspace/H1hZ39rdz/4, you will need to update the formatDate logic to match your desired date format



  • Ahh the bottom window where you had your javascript what is that window? I need to combine the script with the HTML this is where the problem is for me.
    The combination of having the script in with the template. I've changed your example to show hows it's working. I need it working like this for our production service. which doesn't use the development studio.
    https://playground.jsreport.net/studio/workspace/H1hZ39rdz/5


  • administrators

    i think this section describes what is that window. that window is a place where you can define helpers functions for your template engine.

    I need to combine the script with the HTML this is where the problem is for me.
    The combination of having the script in with the template. I've changed your example to show hows it's working. I need it working like this for our production service. which doesn't use the development studio.

    why do you need to have that function in the html in order to make it work on your production server? it does not matter if you don't use the studio in production, you can develop the template + helpers using the studio in your local machine and then you can deploy to production what you have on local, then your template will have everything it needs in production no matter if you don't use studio there.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.