function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
naveen reddy 19naveen reddy 19 

How to the datetime to specified format

How to convert the datetime to specific format

Given date:
DateTime.Now()==> 2016-08-14 18:30:00

Expected date:
 2014-01-01T00:00:00.015-04:00

Thanks,
Naveen
Best Answer chosen by naveen reddy 19
Prateek Singh SengarPrateek Singh Sengar
Correction it should be

DateTime.Now().format('yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX');

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Naveen,
Did you tried the fromat method?

DateTime.Now().format("yyyy-MM-dd\'T\'HH:mm:ss.SSSZ");
Prateek Singh SengarPrateek Singh Sengar
Correction it should be

DateTime.Now().format('yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX');
This was selected as the best answer
naveen reddy 19naveen reddy 19
Hi Prateek,

  Can you please also help me in converting this format to AST, as the expected datetime is in AST and we getting time in GMT.
Expected:
2014-01-01T00:00:00.015-04:00

Above code gives the follwing format.
DateTime.Now().format('yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX');==>2016-08-16T10:09:38.813+05:30.

Really appreciate your help.
Thanks in advance.
Prateek Singh SengarPrateek Singh Sengar
hi Naveen,
The format() method accepts timezone as second parameter. Just add the timezone as a parameter

DateTime.Now().format('yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX','AST')