+ Start a Discussion
Arup SarkarArup Sarkar 

How to set a date to have a time component 23:59:59

Hi:
We have a field in a object To_Date__c which is of type Date, that value is passed on to a Controller to a "Task" object. Since it is a date object the value which I am seeing in System.debug message is for example if the user selected today is 2011-12-07 00:00:00

We have a SOQL based on task mentioned below. Now if someone creates a Task now the CreatedDate in the task object will be 2011-12-07 13:58:45. Hence, the below mentioned newly created task will not be picked by the query.

Requirement:
============
I would like to have the To_Date in the Apex class to be set to 2011-12-07 23:59:59

Options:
==========
How can I achieve my requirement.
a) Update the field type in the object (To_Date__c) to datetime, have a calculated field (To_Date_Calc__c) map to this field, and have a trigger set the value of To_Date__c to always 2011-12-07 23:59:59
OR
b) Change it in the Apex class.

 

    public Date From_Date{get;set;}

 

select Id, OwnerId, AccountId, Activity_Type__c, Date__c, Location__c, CreatedById,
                                Contacts__c,Employees__c,Description, Comments__c, Product_Alts__c, Products_Equity__c,
                                Products_Fixed_Income__c, Products_Multi_Asset__c, Asset_Class_Formula__c, Owner.Name,
                                Other_Information__c, Account.Name
                                from Task
                                where (CreatedDate >= :From_Date and CreatedDate <= :To_Date)
                                and Type__c = 'Call Report'
                                order by Owner.Name, Date__c

Best Answer chosen by Admin (Salesforce Developers) 
Arup SarkarArup Sarkar

I was able to figure out a solution. I did the following

 

Public DateTime To_Date{get;set;}

 

To_Date = To_Date.addHours(23);

To_Date = To_Date.addMinutes(59);

To_Date = To_Date.addMinutes(59);

 

So now in System.debug message I am seeing 2011-12-07 23:59:59

All Answers

Arup SarkarArup Sarkar

I was able to figure out a solution. I did the following

 

Public DateTime To_Date{get;set;}

 

To_Date = To_Date.addHours(23);

To_Date = To_Date.addMinutes(59);

To_Date = To_Date.addMinutes(59);

 

So now in System.debug message I am seeing 2011-12-07 23:59:59

This was selected as the best answer
bob_buzzardbob_buzzard

You can't set a time component in a date field - it doesn't have that concept.  When you debug a date, the formatting shows the time as midnight for the particular date, but that isn't really stored anywhere.

 

CreatedDate, however, is actually a DateTime field, thus you should simply be able to change the type of your from/to date attributes to datetime and use them as-is.