I have a string as,

field name : start Date: 2016-11-14 08:00 AM ( string type)

how to convert this to Date time. I tried Date.parseDate and below option also.

String val = recruiters[j].Start_Time_Formula__c; 
                            system.debug('val ::'+ val);
                            DateTime date1 = (DateTime)Json.deserialize('"'+val+'"', DateTime.class);
                            system.debug('date1 ::' + date1);

but I am getting error as,

System.JSONException: Invalid format: "2016-11-14 08:00 AM" is malformed at " 08:00 AM": (System Code)

I referred the below link and got with the above error.

Convert String DateTime to User Locale Date Time

any help ?

3 Answers 3


You must use an ISO 8601 date, specifically something like:


Which is October 30th, 2016, at 3:27 pm (+2 seconds) in Greenwich Mean Time.

To parse dates in the user's locale, use the appropriate form for the user. For example, in the United States, we would write:

10/30/2016 3:27 pm

Carefully note the order of month, day, and year, and pay attention to the separator (/). You need to input the date exactly as salesforce shows dates to you, because it is locale dependent.

  • Hi, I have two fields in this case one is text which holds 08:00 AM and other fields is a formula fields ( whichi is combination of today date with text field) which gives the result as 2016-11-14 08:00 AM. I am trying to compare this with salesforce Datetimeformat which is like 2016-11-14 22:52:59. As my formula field format is different, its not allowing me to compare. how to handle it
    – KSL
    Commented Nov 14, 2016 at 23:17
  • Solved it. convert the date format as per your suggestion and used date.parse method.. Thanks
    – KSL
    Commented Nov 14, 2016 at 23:43

If you have datetime in string as: 2017-10-26T12:48:47.000+0000, then you can use below approach. Usually, when we make query in code or dev console or workbench, we get datetime in this format. So, if you want to just copy that value and assign it to date value you can use Datetime.valueOf() by replacing T with space.

String strDate1  = '2017-07-25 00:00:00';
String strDate2 = '2017-07-21 00:00:00';

Datetime dt1 = Datetime.valueOf(strDate1);
Datetime dt2 = Datetime.valueOf(strDate2);

// This is just to show it works fine.
Integer noOfDays = dt2.Date().daysBetween(dt1.Date());
System.debug('No. Of Days : '+noOfDays);

I think you have an issue with the hyphens used instead of slashes, if you fix that, the DateTime.parse method will consume your string without issue. Note that the format will be dependent on the user (dd/mm/yyyy vs mm/dd/yyyy) I referenced this during testing Datetime format ignores am/pm? the below code compiles fine.

String testDateString = '11-11-2014 08:00 AM'.replace('-','/');
DateTime testDate = DateTime.parse(testDateString);
String convertedDate = testDate.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');

After you have the datetime in the second line, you can do the comparison you require.

You must log in to answer this question.

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