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
    Sep 11 '15 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
    Sep 11 '15 at 15:05

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.