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
    Commented May 12, 2015 at 22:33

1 Answer 1

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
    Commented May 12, 2015 at 22:41
  • Ahem, whoops. Fixed now. Commented May 12, 2015 at 22:42
  • @DanielBallinger Thanks a lott. This solved my issue
    – sfdc5000
    Commented May 12, 2015 at 23:32

You must log in to answer this question.

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