Datetime parsing

     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?

Answer

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);

Attribution
Source : Link , Question Author : Venkatsforce , Answer Author : crmprogdev

Leave a Comment