DateTime.format()
Signature
String format (DateTime d1, String format)
Description
Return the DateTime passed as a formatted String.
Parameters
Name | Type | Description |
d1 | DateTime | A DateTime to be formatted. |
format | String | A String containing a format describing how the date should be formatted.
e.g. "yyyy-MM-dd HH:mm:ss", "EEE, yyyy-MM-dd HH:mm:ss.SSS Z". SSS (3 S) is required for formating millisecond.
i.e. "yyyy-MM-dd HH:mm:ss.SSS" is correct, but "yyyy-MM-dd HH:mm:ss.S" is not valid. |
Returns
Type | Description |
String | A String representation of the supplied DateTime in the supplied format. |
Example
String result = DateTime.format (Date.now (), "yyyy-MM-dd HH:mm:ss");
Result is: result contains: 2004-03-11 14:59:04.
String result = DateTime.format (Date.now (), "EEE, yyyy-MM-dd hh:mm:ss.SSS aa Z");
Result is: Wed, 2004-03-11 02:59:04.000 PM -0800.
Notes: The format pattern is compatible with the one in java.text.SimpleDateFormat. 3 S (i.e. SSS) has
to be used for formating millisecond correctly.