Friday, November 18, 2011

XQuery to change date format

Now i am working in OSB and struggling hard to learn the xquery

My exact requirement was to convert the date from current-date to the following format

i.e if i will talk in terms of exact requirement

MY aim is to convert the date format fn:current-date()

2011-11-18+05:30 yyyy-dd-mm+05:30 to the following format dd/mm/yyyy

So here is the code that i have written from the same.

fn:substring( fn:replace(fn:string(fn:current-date()),'(\d{4})-(\d{2})-(\d{2})','$2/$3/$1'),1,10)

fn:current-date() will fetch me the result 2011-11-18+05:30

First of all i have converted it to string using following function

(fn:string(fn:current-date())

Then i am using the replace functionality of xquery.

It work as follows

i have taken the first four string of the time \d{4}) then i have taken the second two string including -(\d{2})

again we are taking the next two character along with the - as follow \d{2})

These values are by default stored in $1,$2 and $3 variable so i have arranged them as per my requirement.

Now after doing the following replacement i will get my result but it will cotaint some extra string +05:30

we need to remove that hence i have used the substring function which will remove the extra data.

1 comment:

Unknown said...

Very informative article. Thank you.