0
Input String: 201801
Output String format: 01.2018

I tried using following but it's not working, I also looked up for string to date convesrion/coercion table in "Type Coercion Table" https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-types#type-coercion-table. I did not find something to help me with this.

as :date {format: "yyyyww"} as :string {format: "ww.yyyy"}

Appreciate if anyone has any ideas to share.

N Singh
  • 1
  • 4
  • Just to confirm - the input date format is the first WEEK of 2018 (Based on the format mask you are using)? I don't think you will be able to parse that as a date, because a date needs an exact day to be valid AFAIK. – Johnson Jul 26 '18 at 00:03
  • yes I agree, we can not format that as date. And yes the input date format is the first week of 2018. what is the other options we can try, any thoughts. – N Singh Jul 27 '18 at 07:02

1 Answers1

0

If you don't mind a bit of string hackery you could just move the various parts of the input string around:

%dw 1.0
%output application/json
%var inputDate = "201801"
---
{
    outputDate: inputDate[4..5] ++ "." ++ inputDate[0..3]
}

The output of that is

{
  "outputDate": "01.2018"
}

The above isn't null safe though, if there is a chance that the input date string is empty, or if it is shorter than 6 characters you will get a runtime error. You could work around that by checking the date in the DW, something like

outputDate: (inputDate[4..5] ++ "." ++ inputDate[0..3]) when inputDate != null and ( sizeOf inputDate ) == 6 otherwise null
Johnson
  • 451
  • 2
  • 13
  • Thank you so much for this, it worked smoothly. Sometimes solution lies in the simplest form. I was hellbent on trying to get it done somehow using :date format, which was not possible in this case. – N Singh Jul 30 '18 at 10:10