Added due date on case which is a date field. Trying to get this as dateTime and here is my code:

case c = [select id, caseNumber, Due_Date__c, status, type from case limit 1];
Date i = (c.Due_Date__c);
system.debug(i);// returns 2015-08-07 00:00:00
DateTime t = dateTime.newInstance(i, '11:59:59');
//DateTime t = DateTime.parse(i, 11, 59, 59);
//DateTime Dt = DateTime.newInstance(i, 11, 59, 59);

Getting Error: "Variable does not exist: datetime"

Update: crmprogdev solution below works perfectly. I found another solution for the same using time.newinstance.

case c = [select id, due_date__c from case where due_date__C != null limit 1];               
date d = c.due_date__C;
DateTime dt = datetime.newInstance(d, Time.newInstance(23,59,59,59));
  • 1
    You are receiving this error because you have the wrong signature for dateTime.newInstance. See this reference. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Berg
    Commented Sep 11, 2015 at 14:58
  • 1
    @Berg, Date i won't return the 11:59:59. If he wants that, he'll need to specify it when he creates the new datetime instance.
    – crmprogdev
    Commented Sep 11, 2015 at 15:05

3 Answers 3


I think the simplest method for you to use will be something like the following:

Integer d = Due_Date__c.day();
Integer mo = Due_Date__c.month();
Integer yr = Due_Date__c.year();

DateTime DT = DateTime.newInstance(yr, mo, d);

If you want to keep the 11:59:59 in your local time zone, change the last line as follows:

DateTime DT = DateTime.newInstance(yr, mo, d, 11, 59, 59);

The most simple way to convert a date to a date time, with a time component of 00:00:00.0, is to simply assign a Date to a Datetime variable, or to cast a Date to a Datetime. For example, the following works:

Date today = Date.today();
Datetime todayDateTime = today; // Implicit cast
System.debug('Today is ' + todayDateTime);
System.debug('Today is also ' + (Datetime) today); // Explicit cast

The output would be like:

...|DEBUG|Today is 2017-10-18 00:00:00
...|DEBUG|Today is also 2017-10-18 00:00:00

I don't know which API version introduced this capability, but it is there from v39 at least.

NOTE: This results in a Datetime value in UTC, not in the current user's time zone.

UPDATE: Having experienced various issues with our managed package over the years due to such implicit (and basically invisible to the developer) conversion aligning to UTC, I can say that this approach should be avoided.

  • 2
    +1 for the update, potentially saving countless devs headaches.
    – Link14
    Commented Mar 7, 2023 at 0:30
  • Explicit cast worked like a charm for my case, thanks Commented May 4, 2023 at 15:29
  • What approach should be avoided exactly? The explicit cast looks fine to me.
    – Semmel
    Commented Sep 22, 2023 at 16:15
  • The explicit cast creates a Datetime of midnight in UTC, not in the user's time zone, so you can find the date changes unexpectedly depending on the user's time zone. All the other methods work in the user's time zone.
    – Phil W
    Commented Sep 22, 2023 at 16:20

I've struggled with Date to DateTime (and vice-versa) multiple times (just forgetting the back and forth). Here are some gists I've saved in case they help anyone else. https://gist.github.com/cemerson/fda71148b5bbe5627a5dd6bb96164e2d

You must log in to answer this question.

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