JavaScript function to Convert date difference in Milliseconds

JavaScript function to Convert date difference in Milliseconds

JavaScript is one of the most popular Client-side programming language. It is designed for creating network-centric applications. The concept behind Client side script is to reduce load from the Server. Many Operations is more healthy to operate in Client side. This activity helps to improve performance & speed.

Are you looking for a JavaScript function using which you can Convert date difference in Milliseconds. In the below Function I am passing the date upto which you want to calculate the difference in milliseconds. Here date difference I am calculating from January 1, 1970.

Convert date difference in Milliseconds

function getDateInMilliseconds(d) {
var str = new String(d);
var length = str.length;
var month = 0;
var day = 0;
var year = 0;
var dateObj;
var ms = 0;

if (length == 8) {
month = str.substr(0,1);
day = str.substr(2,1);
year = str.substr(4,4);
}
else if (length == 9) {
var regExp = new RegExp("\\d{2}/\\d{1}/\\d{4}");
var expResult = regExp.test(str);
if (expResult) {
month = str.substr(0,2);
day = str.substr(3,1);
year = str.substr(5,4);
} else {
month = str.substr(0,1);
day = str.substr(2,2);
year = str.substr(5,4);
}
}
else if (length == 10) {
month = str.substr(0,2);
day = str.substr(3,2);
year = str.substr(6,4);
}

dateObj = new Date(year, (month - 1), day);
ms = dateObj.getTime();
return ms;
}