ShowAll Questionssorted byDate Posted
Mohan Raj 33

# how to here changed the date to date time period

My Formula field:
Total Time difference(Date/Time) =
Date dCloseDate = System.CloseDate()
DateTime dt = datetime.newInstance(dCloseDate.year(), dCloseDate.month(), dCloseDate.day());
Calculating_Date__c - dCloseDate;
I have a three field in the Opportunity field the Close Date, Total Time Difference and Calculating Date respectively.
Here I try to get the difference between the close date and Calculating Date.
but the close date is a date field and the Calculating date is a date/time field and the Total Time Difference is a Date/Time field.
So I have struggle to complete the task in getting difference in date and date/time fields so please help to solve this task.
For answers thanks in advance.

Best Answer chosen by Mohan Raj 33
Apoorv Saxena 4
Hi Mohan,

Try this :

ABS( Calculating_Date__c -DATETIMEVALUE(dCloseDate))*24

Please let me know if this helps!

Thanks,
Apoorv

### All Answers

sridharb
Hi Mohan,
First Convert your close date filed to date time as following
Datetime newInstance(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integersecond)

Then use this calculate the datetime.

Regards
Sri
Akhil Anil
Hi Mohan,

You can do that with a snippet like this

Datetime dt = CalculatingDate; //Capture the value of Calculatingdate to a datetime variable
Date mydate = Date.newinstance(dt.year(), dt.month(), dt.day()); //Convert datetime to date
totaltimedifference = Closedate - mydate; // Find the difference in dates

Hope that helps !
Mohan Raj 33
@Akhil Anil Thanks for the reply sir. I got this error on " Error: Syntax error. Found 'Datetime' " on your code.can you know how to rectify this error sir.Thanks Mohan
Mohan Raj 33
@sridhar bachu 5 thanks for the reply. I am new in the formula field tasks so can you send any link to the examples of like this task sir.Thank you Mohan
karthikeyan perumal
Hello Mohan,

2 datetime difference is alsways number, not date time. so change you date time field to  number data type use code below like

i hope you are using this in formula field  not in apex code?

Calculating_Date__c -DATETIMEVALUE(dCloseDate)

hoope this will help you.

Thanks
karthik

Mohan Raj 33
@karthikeyan perumal sir here I got some answer's in negative value (logically there is no negative values in time) and I want the total amont of time difference is also calculated to the hours(include the date difference also in hours).
Just like if I give close date 10/10/2016 and the calculating date is 12/10/2016 then the answer is in hour is difference of the two dates(in hours) and the difference of that two date's hours difference (plus this hour) in positive value(like  48 (for two days of 10 and 12) + two date's hours difference ). Thank you Mohan
Apoorv Saxena 4
Hi Mohan,

Try this :

ABS( Calculating_Date__c -DATETIMEVALUE(dCloseDate))*24

Please let me know if this helps!

Thanks,
Apoorv
This was selected as the best answer
Mohan Raj 33
Yes, Thank You very much for you reply.
And I have a doubt if I try to calulate the current date (today) in instant of close date then what changes I have done this code.
And I also want to how the negative values are not come in now could you explain it's help to clear to understand me plz thank you.
Apoorv Saxena 4
Hi Mohan,

You can use either of these :

ABS( Calculating_Date__c -DATETIMEVALUE(TODAY()))*24

or you can use NOW(), like:

ABS( Calculating_Date__c -NOW())*24

If you feel that your question was answered then do flag the appropriate answer as the solution to your query.

Thanks,
Apoorv