Answered by:
Calculate difference (in days) between two date fields

Question
-
Hi all
Hopefully a simple request for a bit of javascript to calculate the difference in days between two events.
In essence I have a new_from_date field and a new_to_date field and I want to calculate (i.e subtract) the difference (in days) in these two dates "on the fly" to pop into a third field called new_calculated_days.
Any snippets would be most appreciated.
Regards
Jimbo
Jim
Wednesday, May 19, 2010 11:11 AM
Answers
-
Hi,
Use the below script
var fromDate=crmForm.all.new_from_date.DataValue; var toDate=crmForm.all.new_to_date.DataValue; var ONE_DAY = 1000 * 60 * 60 * 24 var date1_ms = fromDate.getTime() var date2_ms = toDate.getTime() var difference_ms = Math.abs(date1_ms - date2_ms) var days=Math.round(difference_ms/ONE_DAY) alert(days);
Regards,
Nishant Rana
http://nishantrana.wordpress.com- Marked as answer by Jimbo64 Wednesday, May 19, 2010 12:20 PM
Wednesday, May 19, 2010 11:37 AM
All replies
-
try following code..
//Set 1 day in milliseconds
var one_day=1000*60*60*24var days = new_from_date.getTime()-new_to_date.getTime()/ (one_day);
hope this helps..
- Proposed as answer by Nishant RanaMVP Wednesday, May 19, 2010 11:37 AM
Wednesday, May 19, 2010 11:24 AM -
Hi,
Use the below script
var fromDate=crmForm.all.new_from_date.DataValue; var toDate=crmForm.all.new_to_date.DataValue; var ONE_DAY = 1000 * 60 * 60 * 24 var date1_ms = fromDate.getTime() var date2_ms = toDate.getTime() var difference_ms = Math.abs(date1_ms - date2_ms) var days=Math.round(difference_ms/ONE_DAY) alert(days);
Regards,
Nishant Rana
http://nishantrana.wordpress.com- Marked as answer by Jimbo64 Wednesday, May 19, 2010 12:20 PM
Wednesday, May 19, 2010 11:37 AM -
Thanks for the quick reply Mayank.
Am I correct in thinking I would place this on the onchange event of either of the date fields?
Again due to my weakness in java, what java line do I need to do to populate the var days result into my new_calculated_days field?
Much appreciated
Jimbo
Wednesday, May 19, 2010 11:37 AM -
Hi,
Yes you need to place it in on change event of both the fields. You also need to check if the fields are not null before calculating the difference.
This is how you would set the value.
crmForm.all.new_calculated_days.DataValue=days;
Regards,
Nishant Rana
http://nishantrana.wordpress.comWednesday, May 19, 2010 12:08 PM -
That seems to work a treat thanks. I need to look now at the null problem
Wednesday, May 19, 2010 12:20 PM -
Hi.
Currently I'm working on MS Dynamics. Thanks for the post so far. I'm also having problem with the null. Have you got any solutions? Any reply would be much appreciated.
Thank you in advance.
Tuesday, June 8, 2010 4:33 AM -
//function to calculate days between var calculateDaysBetween = function (datetime1, datetime2) { /// <summary> /// Calculate the days between two dates /// </summary> /// <param name="datetime1" type="DateTime"> /// The first / early date to be calculated /// </param> /// <param name="datetime2" type="DateTime"> /// The second / later date to e calculated /// </param> /// <returns type="int" /> // The number of milliseconds in one day var oneDay = 1000 * 60 * 60 * 24; // Convert both dates to milliseconds var date1Ms = datetime1.getTime(); var date2Ms = datetime2.getTime(); // Calculate the difference in milliseconds var differenceMs = Math.abs(date1Ms - date2Ms); // Convert back to days and return return Math.round(differenceMs / oneDay); }; //Ef
Just sharing here for future reference..here datetime1 & datetime2 are two Date Object.
Unsupported anyway... |_| |\| _\~ |_| |^ |^ () /? ~|~ [- |) /\ |\| `/ \/\/ /\ `/
- Edited by ViN.k.S Wednesday, December 12, 2012 5:02 AM edit
Wednesday, December 12, 2012 5:00 AM