date formatting

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

date formatting

kotharv
Hello

I gave a date box which auto populates and I need to be formatted,

substring(instance('admission')/admission_date/date, 1, 8)

I am trying to use nested function as below

format-date(substring(instance('admission')/admission_date/date, 1, 8),'[Y]-[M01]-[D01]')

buy says a invalid xpath, is there any other way.

Reply | Threaded
Open this post in threaded view
|

Re: date formatting

Alessandro  Vernet
Administrator
What is the format of the date you have in `instance('admission')/admission_date/date`? It isn't already a date in the ISO format, i.e. "2017-06-05"?

Alex
--
Follow Orbeon on Twitter: @orbeon
Follow me on Twitter: @avernet
Reply | Threaded
Open this post in threaded view
|

Re: date formatting

kotharv
Hi Alex,

when populate it from admission instance, just displays as,

201705202017052020170520

I trimmed it to 8 and need to format it, I am not sure about the instance displays as such.
Reply | Threaded
Open this post in threaded view
|

Re: date formatting

Alessandro  Vernet
Administrator
Got it. So, if you want to have the date stored somewhere in the ISO format, you'll want to use something along those lines, I imagine as the calculated or initial value of a control:

for $d in
    substring(instance('admission')/admission_date/date
return concat(
    substring($d, 1, 4), '-',
    substring($d, 5, 2), '-',
    substring($d, 7, 2)
)

Then, once you have the date in ISO format, you can use a control that understands it as a date, and it will format it appropriately. Would this work for you?

Alex
--
Follow Orbeon on Twitter: @orbeon
Follow me on Twitter: @avernet
Reply | Threaded
Open this post in threaded view
|

Re: date formatting

kotharv
Hello Alex,

it evaluates to invalid xpath, is there any link that describes the for expression.

regards,
Vijay
Reply | Threaded
Open this post in threaded view
|

Re: date formatting

Alessandro  Vernet
Administrator
Hi Vijay,

Strange: if I go to the XPath sandbox:

- As input, I put:

<gaga>201705202017052020170520</gaga>

- As XPath, I put:

for $d in
    /gaga
return concat(
    substring($d, 1, 4), '-',
    substring($d, 5, 2), '-',
    substring($d, 7, 2)
)

- Then as output I get the expected result of `2017-05-20`.

I am thinking that the version of Orbeon Forms you're using might not support `for` in XPath. In case, try the following:

concat(
    substring(substring(instance('admission')/admission_date/date, 1, 4), '-',
    substring(substring(instance('admission')/admission_date/date, 5, 2), '-',
    substring(substring(instance('admission')/admission_date/date, 7, 2)
)

You'll let us know if this help!

Alex
--
Follow Orbeon on Twitter: @orbeon
Follow me on Twitter: @avernet
Reply | Threaded
Open this post in threaded view
|

Re: date formatting

kotharv
Hi Alex,

I am using 3.9, its works with out 'for'..i was trying for as it simply replaces the whole xpath.

no worries the version will the issue.

Thanks a lot!!
Reply | Threaded
Open this post in threaded view
|

Re: date formatting

Alessandro  Vernet
Administrator
Perfect Vijay. And in case you need more arguments to upgrade ;), starting with 2016.2, you can even use `let` instead of `for`, which is clearer and can avoid problems in cases like this one, where you want to name something rather than do a loop.

https://doc.orbeon.com/xforms/xpath/compatibility.html#support-for-let-expressions

Alex
--
Follow Orbeon on Twitter: @orbeon
Follow me on Twitter: @avernet