To convert String to Date in salesforce

The below simple code is used to convert String to Date data type using Apex

Sample Code:

String todate = '12/27/2013';
Date dt = Date.valueOf(todate);

Cheers!!!

6 comments:

  1. it occur runtime error Invalid date

    ReplyDelete
  2. Use Date.parse(todate) instead

    ReplyDelete
  3. Use :

    String todate = '12/27/2013';
    Date dt = Date.parse(todate);

    ReplyDelete
  4. showing error

    ReplyDelete
  5. String strDate1 = '2017-07-25 00:00:00';
    String strDate2 = '2017-07-21 00:00:00';

    Date dt1 = Date.valueOf(strDate1);
    Date dt2 = Date.valueOf(strDate2);

    Integer noOfDays = dt2.daysBetween(dt1);
    System.debug('No. Of Days : '+noOfDays);

    ReplyDelete