0

I have a String as DateTime like this 2019-05-21 00:00:00.000.

This is the code that I use in Dataweave 2 to transform String to DateTime:

SourceDate: payload.Source_date as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"}

But it returns this error:

"Cannot coerce String (2019-05-21 00:00:00.000) to DateTime, caused by: Text '2019-05-21 00:00:00.000' could not be parsed at index 10

I need to use 'T' and Z to use the TimeZone automatically.

What could be the problem?

gtx911
  • 1,189
  • 4
  • 25
  • 46
  • "I need to use 'T' and Z to use the TimeZone automatically." - then you'll need those to be in the string that you're parsing. Currently your value has a space between the date and the time, and no Z at the end. Where does the value come from, and are you able to change its format? – Jon Skeet May 23 '19 at 10:22
  • It cames from a CSV file, Which is the format it expect? – gtx911 May 23 '19 at 10:24
  • You're specifying the format it's expecting: `yyyy-MM-dd'T'HH:mm:ss.SSSZ`. Basically you've got to make the format you specify match the data you've got... or modify that data yourself. – Jon Skeet May 23 '19 at 10:36

1 Answers1

2

You can use LocalDateTime which will use the current timezone:

%dw 2.0
output application/json
---
SourceDate: payload.Source_date as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss.SSS"}

And you can add the timezone:

SourceDate: payload.Source_date as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss.SSS"} >> "GMT+1"
Ryan Carter
  • 11,441
  • 2
  • 20
  • 27
  • The problem is that it set the same date `2019-05-21 00:00:00.000`. It doesn't adds one hore more if I am using it from GMT+1 for example. – gtx911 May 23 '19 at 10:32
  • 1
    updated answer if that helps {format: "yyyy-MM-dd HH:mm:ss.SSS"} >> "GMT+1" – Ryan Carter May 23 '19 at 10:38