0

I have been stuck in a problem actually i am trying to covert a datetime format in salesforce date time field format

The datetime format which i am getting is 2020-09-13 10:37 CDT I tried removing CDT from the end like this but it is not working

String startDate = String.valueOf(assetActivity.get('actstart')
                .getValue()).removeEnd('CDT');
            
            Datetime dt = datetime.parse(startDate);
            system.debug('dt'+dt);
            ts.Start_Time__c = dt;

It is always throwing error of invalid date/time please help me out Thanks in advance

1 Answer 1

0

Note the time needs to be transformed to GMT based on the timezone:

String[] dateTimeSplit = String.valueOf(assetActivity.get('actstart')
                .getValue()).split(' ');
String[] dateSplit = dateTimeSplit[0].split('-');
String[] timeSplit = dateTimeSplit[1].split(':'); //you will have to write a function that converts the time to GMT based on the dateTimeSplit[2] literal
Datetime dt = DateTime.newInstanceGmt( Integer.valueOf(dateSplit[0]), Integer.valueOf(dateSplit[1]), Integer.valueOf(dateSplit[2]), Integer.valueOf(timeSplit[0]), Integer.valueOf(timeSplit[1]), 0);
system.debug('dt'+dt);
ts.Start_Time__c = dt;

See DateTime newInstanceGmt method

12
  • Thanks for your response but it didn't work for me i tried this code String[] dateTimeSplit = String.valueOf(assetActivity.get('actstart') .getValue()).split(' '); system.debug('dateTimeSplit'+dateTimeSplit); Datetime dt = datetime.parse(dateTimeSplit[0] + 'T' + dateTimeSplit[1]); system.debug('dt'+dt); ts.Start_Time__c = dt; It is giving this error now Invalid date/time: 2020-09-15T05:42 Commented Sep 15, 2020 at 11:29
  • That will not work because you need to append the timezone difference to GMT. For CDT, it is '-05:00'. If your times will come in GMT time, then you have to append a 'Z'
    – Ideasforce
    Commented Sep 15, 2020 at 11:30
  • If you see your ternary operator it will always return '-5:00' and that is not a datetime format Commented Sep 15, 2020 at 11:34
  • I missed a parenthesis when calculating the trailing timezone adjustment, but just realized timezone literals do not work with parse method. Editing my original response..
    – Ideasforce
    Commented Sep 15, 2020 at 11:41
  • Thanks for response but now i am getting this error Method does not exist or incorrect signature: void valueOf(String, Integer) from the type Integer Commented Sep 15, 2020 at 12:01

You must log in to answer this question.

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