5
     String date = '';
     String time = '';

How to concatenate the two strings to StartDatetime(Datetime datatype) field?

I am using two strings date and time seperately ,If i fill date & time then i want to combine the two fields and store in one field (i.e) Startdatetime__c (Datatype as DateTime) how to fix this one?

This is my REST request { "sdate" : "2/12/2013", "stime" : "12:05:00" }. How do I convert the sdate & stime into a DateTime field StartDatetime__c?

  • 2
    can you make it clear? – Mohith Shrivastava Dec 16 '13 at 12:08
  • Thanks @Mohit Kumar ,I am using two strings date and time seperately ,If i fill date & time then i want to combine the two fields and store in Onefield (i.e) Startdatetime__c (Datatype as DateTime) how to fix this one? – Venkatsforce Dec 16 '13 at 12:27
  • any example of the format you're using in those strings ? – Samuel De Rycke Dec 16 '13 at 12:32
  • This is my REST request { "sdate" : "2/12/2013", "stime" : "12:05:00" } StartDatetime__c = 2/12/2013 12:05 how to store the sdate&stime in single field as StartDatetime__c this field contain the datatype as Datetime. – Venkatsforce Dec 16 '13 at 12:54
1

Here's an example from some code I used in a trigger on how to do this

StrtTmDt = sClsMnth + '/' + sClsDay + '/' + sClsYear + ' ' + strtTm;

So, in your case,

String date = ''; //would need to be formatted as 'dd/mm/yy'

String time = ''; //would need to be formatted as ' hh:mm aa` where aa is AM or PM

note the blank space between the ' and the hh. You need this separation. SF will automatically add :00 seconds for you so there's no need to add them unless you actually are recording seconds or microseconds for that matter.

If properly formatted as above, you could then do the following:

String dt = date + time;

DtTm = DateTime.parse(dt);
  • And if your time doesn't have a space, you can add it when composing dt: String dt = date + ' ' + time; – tomlogic Dec 16 '13 at 18:18
  • Yes, just as I did in the original code I showed above where I added separate strings for day, month, year and time. ;) – crmprogdev Dec 16 '13 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.