function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandy GaliSandy Gali 

Datetime format conversion in apex

Hello All,
             I have an input field in my visualforce page which uses the html 5 input element as type="datetime-local". The value that I capture from the field is of the format "2016-04-09T23:58" . I want to use and save this field on to the database in salesforce and I cant assign the same value to the field directly. Even to parse salesforce expects a different format something like this '09/04/2016 5:10 PM'.

If I can do something like Datetime dt = DateTime.parse('24/11/2011 1:46 PM') then I can assign it to a field in Salesforce but there is nothing where I can use 2016-04-09T23:58 directly. I will have to convert 2016-04-09T23:58 to 09/04/2016 5:10 PM and do it.

Is there any better way to achieve this  or am I missing something here 

Regards,
Sandy
Mahesh DMahesh D
Hi Sandy,

You can look into the below example:
 
string Dt = '11/28/2014 10:10';

String[] str = dt.split(' ');
String[] newdates = str[0].split('/');
String[] newTimes = str[1].split(':');

if(str.size()>2)
{
String newampm = str[2];
System.debug('------------newampm--------------'+newampm);

String newmydate=Integer.valueOf(newdates[0])+'/'+Integer.valueOf(newdates[1])+'/'+Integer.valueOf(newdates[2])+' '+Integer.valueOf(newTimes[0])+':'+Integer.valueOf(newTimes[1])+' '+newampm;
//DateTime myDate = Datetime.parse(newmydate);
DateTime myDate =datetime.newInstance(Integer.valueOf(newdates[2]), Integer.valueOf(newdates[0]), Integer.valueOf(newdates[1]), Integer.valueOf(newTimes[0]), Integer.valueOf(newTimes[1]), Integer.valueOf(0)); //Datetime.parse(newmydate);

System.debug('--------------------------'+myDate);

}
else
{

    string newampm;
    integer newhour;
    if(integer.valueOf(newTimes[0])>integer.valueOf('12'))
    {
        newhour=integer.valueOf(newTimes[0])-integer.valueOf('12');
        newampm='PM';
    }
    else
    {
         newhour=integer.valueOf(newTimes[0]);
         newampm='AM';
    }
String newmydate=Integer.valueOf(newdates[0])+'/'+Integer.valueOf(newdates[1])+'/'+Integer.valueOf(newdates[2])+' '+Integer.valueOf(newhour)+':'+Integer.valueOf(newTimes[1])+' '+newampm;
DateTime myDate =datetime.newInstance(Integer.valueOf(newdates[2]), Integer.valueOf(newdates[0]), Integer.valueOf(newdates[1]), Integer.valueOf(newhour), Integer.valueOf(newTimes[1]), Integer.valueOf(0)); //Datetime.parse(newmydate);
System.debug('--------------------------'+myDate);
 
}

Regards,
Mahesh
Prashant BhattPrashant Bhatt
Hi, 
Try this.

<apex:outputText value="{0,date,MM'/'dd'/'yyyy,G 'at' HH:mm:ss z}">
<apex:param value="{!fieldThat you are getting}" />
</apex:outputText>


Link-https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_outputText.htm
 
Sandy GaliSandy Gali
@Mahesh D,
                  I was trying to do something similar to what you suggested but I felt thats lot of code.
A better way to do it was to append :00 to the end of the date string and then do Datetime.valueOf(datestring)