+ Start a Discussion

Conver ISO format to salesforce date format

I am calling an API and in respose it returns me JSON data. In this i am abstracting a field value which is in ISO format : 2013-12-14T09:49:25-05:00

I need to store this value on an SFDC Object record. please let me know the formating for this.

I am doing this (DateTime.valueOf(responseMap.get('last_reported_at'))).format('YYYY-MM-DDThh:mm:ss-hh:mm') .

Let me know ulternate for this. It will be good if we can do using DateTime format method of salesforce.

I am getting this error : Invalid date/time: 2014-08-25T15:18:41.938Z
James LoghryJames Loghry
From the Datetime documentation, here is the expected format:

The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.

string year = '2008';
string month = '10';
string day = '5';
string hour = '12';
string minute = '20';
string second = '20';
string stringDate = year + '-' + month
 + '-' + day + ' ' + hour + ':' + 
minute +  ':' + second;

Datetime myDate = 

Note, the format is different than what you're stating in your post above.

You could also try using the parse method, which takes a different format:


Constructs a Datetime from the String datetime in the local time zone and in the format of the user locale.
public static Datetime parse(String datetime)

Type: String
Return Value
Type: Datetime

The returned date is in the GMT time zone.

This example uses parse to create a Datetime from a date passed in as a string and that is formatted for the English (United States) locale. You may need to change the format of the date string if you have a different locale.
Datetime dt = DateTime.parse(
              '10/14/2011 11:46 AM');
String myDtString = dt.format();
    '10/14/2011 11:46 AM');

All the info you could ever want on the Datetime object is here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm
string s = '"2013-08-20T05:06:44.160Z"'; Datetime dt = (DateTime)JSON.deserialize(s,DateTime.class); system.debug('datetime'+dt);
Shubham NandwanaShubham Nandwana

Hi shiv@SFDC,
You can easily get year/month/day/hour/min (using methods d.year() / d.month() .....) from ISO format and then use to create a DateTime.newInstance method to create a DateTime object and store it in sobject field.
Adding a sample code for reference.
DateTime myDateTime = DateTime.newInstance(1993, 6, 6, 3, 3, 3); 
String formatted = myDateTime.formatGMT('EEE, MMM d yyyy HH:mm:ss');
 String expected = 'Sun, Jun 6 1993 10:03:03';
 System.assertEquals(expected, formatted);
Choose this as best answer if it resolves your query or ask anything if needed.

Shubham Nandwana.
AppPerfect Corp.
Salesforce Development & Operations Experts
Amsyara AAmsyara A
format('YYYY-MM-DDThh:mm:ss-hh:mm') . Let me know ulternate for this. It will be good if we can do using DateTime format method of salesforce
iso certification (https://ias-malaysia.com)