I am getting this error "Invalid conversion from runtime type Datetime to Date" while converting createddate from datetime format to date format by following way.

DateTime dT = pis1.CREATEDDATE;
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

Any suggestions!! TIA.

  • 3
    try myDate = dT.date();
    – highfive
    Commented May 9, 2014 at 5:16
  • With some surrounding code, this code works good for me when run in the Workbench. What is happening before your code? What is 'pis1'? I tested by defining pis1 as a 'User' data type and set it to [SELECT Id, CreatedDate FROM User where Id = :UserInfo.getUserId()]; After that your code works without a problem. Commented May 9, 2014 at 7:14
  • 1
    This works fine for me - is the apex class at a really early API version? I seem to recall createddate was originally a date and then it switched to be a datetime. Commented May 9, 2014 at 7:25
  • I think you are going to have to provide some of your code and if possible the debug log as this error will not be thrown using your code and the standard CreatedDate field for any objects. As others have suggested the code works fine. Maybe you misread the location of the error or there is something else going on.
    – Eric
    Commented May 10, 2014 at 0:14

1 Answer 1


This code works fine for me ,I'm not getting any error.If you want only date may be you can try like this

DateTime dt =system.now();
string sd = string.valueOf(dt.date());
String stime = String.valueOf(dt.time());

The String sd gives only date and the String stime gives only the time.

You must log in to answer this question.