# Calculate time difference between two datetime fields (Hours:Min:Sec) in jscript MS CRM 2011

• ### Question

• `Hi All,I want to calculate time difference between two date field like ﻿create on date(record)and the current date in the form Hours:Min:Sec format.if hours is greater than 24 hours then calculations should be in days  .RegardsSrikanth Reddy      `

﻿

Thursday, January 2, 2014 8:55 AM

### All replies

• Hi,

You could use the approach detailed here - http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript/15289883#15289883

The principle is that a datetime object will return a number in milliseconds, and you can subtract one from the other.

Hope this helps,

Scott

Scott Durow
Rockstar365

Thursday, January 2, 2014 10:28 AM
• ```var calculateDaysBetween = function () {
var datetime1= Xrm.Page.getAttribute("new_FieldName1").getValue().getDay();
var datetime2= Xrm.Page.getAttribute("new_FieldName2").getValue().getDay();

// 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);
};```

Regards Faisal

Thursday, January 2, 2014 10:54 AM
• Try the approach specified in the following link

puja jain

Friday, January 3, 2014 11:07 AM