I want to extract time from time picklist(12 PM,12 AM,4PM ,6:15 PM are picklist values) in apex like

  • 12 PM as 12:00:00

  • 12 AM as 00:00:00

  • 4 PM as 16:00:00

  • 6:15 PM as 18:15:00

2 Answers 2


Use Datetime.parse(String datetime)


It works like

Datetime dt = DateTime.parse('10/14/2011 11:46 AM');

Now you have just a time and there is no such function on the Time object. As a workaround use a fake date and extract the time component with DateTime.time()

Time t = DateTime.parse('10/14/2011 11:46 AM').time();

In your case

Time t = DateTime.parse('10/14/2011 ' + YouObject__c.YourPicklist__c).time();
  • I have just time as picklist values not Date Time
    – sfdc
    Mar 18, 2015 at 21:54
  • If you have a field of type picklist, it's value is type string. DateTime will be output and not input type.
    – Uwe Heim
    Mar 18, 2015 at 21:59
  • If you want to convert ALL possible pv, you need schema methods salesforce.stackexchange.com/questions/4992/… and then the above
    – Uwe Heim
    Mar 18, 2015 at 22:03
  • Updated my answer
    – Uwe Heim
    Mar 18, 2015 at 22:09


Here is a method you could use to convert those picklist values into a time string. You can tweak as you like if you actually want to get a Time out of it.

public string convertTime(string t)
    //Add the minutes if they are not provided
        t = t.replace(' ','');
        t = t.replace('PM',':00 PM').replace('AM',':00 AM');

    //If AM or PM not included, add AM as default
    if(!t.contains('AM') && !t.contains('PM'))
        t += ' AM';

    //Turn the time into a DateTime
    string dtime = '01/01/15 '+t;
    //Parse it and extract the time
    string result = string.valueOf(DateTime.Parse(dtime).time()).replace('.000Z','');
    return result;

Good luck!


You must log in to answer this question.

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