5

How can we format today's date in the following date/time format?

2015-04-29T06:17:44.000-07:00

it represents yyyy-mm-ddThh:mm:sss-UTC

any suggestions will be appreciated.

Thanks in advance.

1
  • in what? apex, VF, formula field?
    – cropredy
    May 12 '15 at 22:33
6

You can use the DateTime.format(String) method and a Java SimpleDateFormat as the parameter.

System.debug(Datetime.now().format('yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'));

gives:

2015-05-12T15:43:36.358-0700

This will be for the current users local time zone. Note:

"If the time zone cannot be determined, GMT is used."

If you want more control over the timezone you can use the DateTime.format(String, String) method and specify the TimeZone.

3
  • 1
    I'm pretty sure there's a typo here.
    – sfdcfox
    May 12 '15 at 22:41
  • Ahem, whoops. Fixed now. May 12 '15 at 22:42
  • @DanielBallinger Thanks a lott. This solved my issue
    – sfdc5000
    May 12 '15 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.