0

I'm need to pass datetime value in 2021-11-03T22:45:00+05:30 format to external system. But I'm not sure how to get datetime in this format.

I tried multiple things, but it's not returning in this format.

system.debug('Created Date is ' + string.valueOfGmt(acc.CreatedDate));  
//system.debug('Created Date is ' + acc.CreatedDate.format('YYYY-MM-ddThh:mm:ss')); // this gives error.

2 Answers 2

2

T part is not processed in format(), you need to exclude in from function params (or escape it like \'T\' as @Derek F mentioned). Also you'll need to use HH instead of hh if you do not specify AM/PM part (docs). XXX is stand for time zone in format -08:00.

System.debug('Created Date is ' + acc.CreatedDate.format('yyyy-MM-dd\'T\'HH:mm:ssXXX')); 
0

Apex borrows Java's SimpleDate format.

The YYYY-MM-ddThh:mm:ss part is almost correct, you just need to escape the 'T', and add the bit for the time zone, which according to Java's documentation you should add 'XXX' (which should give you the sign, two digit hour, colon, and two digit minutes)

'YYYY-MM-dd\'T\'HH:mm:ssXXX' is what you're looking for

3
  • It is not correct, T is not a part of Date and Time Patterns, you'll need to separate formatting in two different parts to pass T as part of String. Nov 3, 2021 at 12:48
  • @MariiaIllarionova Yep, I should have tried it myself before posting. I have made a correction.
    – Derek F
    Nov 3, 2021 at 12:53
  • 1
    This is also HH and XXX, because XX will show result without colon) Nov 3, 2021 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .