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
John_SFDCJohn_SFDC 

How to convert the String to Date time.

I have the String '11/12/15 10:42' to Date time format.

I have Date_take__C field on Case object.I am Using EMail to case functionality .where i am getting the Case Description from the email body,from the email body i  have captured the '11/12/15 10:42' in to string vairable.But now am not able to assign to the  Date_take__C case field.

Thanks in advance.
Vijay NagarathinamVijay Nagarathinam
Hi,

Use the below code to convert the string to datet ime.
 
System.debug(DateTime.valueOfGmt(('2013/08/06 06:30:22').replaceAll('/','-')));
The console output is 
 
08:19:40:061 USER_DEBUG [5]|DEBUG|2013-08-06 06:30:22

Let me know if you know need any help regarding this,

Thanks,
Vijay
John_SFDCJohn_SFDC
Thanks vijay.But i have the value in String varible.like String s='11/12/15 10:42';.how we can convert it?
Vijay NagarathinamVijay Nagarathinam
Use the below statement to convert,
 
String S='11/12/15 10:42'
Datetime convertedDate = s.replaceAll('/','-');
System.debug('Date time =============+>'+convertedDate);

Let me know if you need any help regading this.

Thanks,
Vijay
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Muneendar,

This code may help you.

String strTest = '11/12/15 10:42';
String[] arrTest = strTest.split('/');
DateTime d = DateTime.newInstance(Integer.valueOf(arrTest[2].substring(0,2)),Integer.valueOf(arrTest[1]),Integer.valueOf(arrTest[0]));

let me know, if it helps you or need any help :)
shiva.sfdc.backup@gmail.com
 
John_SFDCJohn_SFDC
am getting the :System.TypeException: Invalid integer:
11
 
Richard Jimenez 9Richard Jimenez 9
Hi Muni,

Try this:
 
String strDateTime = '11/12/15 10:42';
String[] strDateArray = strDateTime.split(' ')[0].split('/');

Date dteDate = Date.newInstance(integer.valueOf('20' + strDateArray[2]),
                                integer.valueOf(strDateArray[1]),
                                integer.valueOf(strDateArray[0]));

It assumes the data string format is always 'dd/mm/yy hh:mm'.
The year format is only 2 characters in length, so it assumes it is the year 2015 when the date is created.

I would recommend including some string validation to ensure the supplied string is in the appropriate format and raise custom exceptions if it is not with a description of the issue to aid debugging later if the date string supplied does not meet the expected format.

Hope that helps,
Richard Jimenez