Margub Alam
Margub Alam

Reputation: 39

Get difference between 2 dates in javascript

var date1 = new Date("04.11.2016");
var date2 = new Date("19.11.2016");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays);

Trying get different between those Dates, but my date format is that "04.11.2016", Result show NaN

Upvotes: 0

Views: 1182

Answers (12)

DaiDH
DaiDH

Reputation: 11

const day = Math.abs(new Date("11 04 2016").valueOf() - new Date("11 19 2016").valueOf())/1000/60/60/24;
console.log(day)

Upvotes: 0

Abdul Mateen Mohammed
Abdul Mateen Mohammed

Reputation: 1894

Check this solution which uses a function called getDate to convert the date string of the format "04.11.2016" to a JavaScript Date object.

function getDate(dateStr) {
  var arr = dateStr.split('.');

  return new Date(arr[2], arr[1], arr[0]);
}

var start = getDate("04.11.2016");
var end = getDate("19.11.2016");
var timeDiff = Math.abs(end.getTime() - start.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 60 * 60 * 24))
console.log('Number of days: ' + diffDays);

In order to use this code to create a Custom JavaScript Variable in Google Tag Manager, you can modify the above code or the one which you choose to be inside a function - reference.

Custom JavaScript Variable in Google Tag Manager

Custom JavaScript Variable in Google Tag Manager

Upvotes: 0

Mohideen bin Mohammed
Mohideen bin Mohammed

Reputation: 20137

You can use moment.js,

d = moment('11.16.2016') // here date format was in "MM.DD.YYYY"
e = moment('11.04.2016') // here date format was in "MM.DD.YYYY"
getDiffbydays = e.diff(d,'days') // get diff by days you can use day
getDiffbyyears = e.diff(d,'year') // get diff by years you can use year 
getDiffbymonth = e.diff(d,'month') // get diff by months you can use month

Upvotes: 0

Atul Sharma
Atul Sharma

Reputation: 10645

new Date("19.11.2016");

this is Invalid Date. So, difference is be NaN .

change the format to mm/dd/yyyy and it will work.

Upvotes: 0

Rajesh
Rajesh

Reputation: 24915

JS expects date to in MM-DD-YYYY and not DD-MM-YYYY. Ideal way would be to use moment.js, but you can use something like this:

function createCustomDate(dateString){
  var dateArr = dateString.split(/[^0-9]/).reverse().join("-")
  return new Date(dateArr);
}

var dateStr1 = "04.11.2016";
var dateStr2 = "19.11.2016";
var date1 = createCustomDate(dateStr1);
var date2 = createCustomDate(dateStr2);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
console.log(diffDays);

Upvotes: 0

prasanth
prasanth

Reputation: 22490

new date("mm dd yyyy") format was wrong

(function () {
var date1 = new Date("11 04 2016");
var date2 = new Date("11 19 2016");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
console.log(diffDays);
})()

Upvotes: 0

Karpak
Karpak

Reputation: 1927

Just change the first two lines as below

var date1 = new Date(2016,11,4);
var date2 = new Date(2016,11,19);

Upvotes: 0

user5825492
user5825492

Reputation:

A date consists of a year, a month, a day, an hour, a minute, a second, and milliseconds.

Date objects are created with the new Date() constructor.

There are 4 ways of initiating a date:

new Date() new Date(milliseconds) new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds)

so you can split them and then use it

Upvotes: 0

WitVault
WitVault

Reputation: 24120

Your second date is incorrect. Parser is considering this format MM.DD.YYY and you have supplied out of range month.

var date1 = new Date("04.11.2016");
var date2 = new Date("09.11.2016");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays);

Upvotes: 0

srinivas.ln
srinivas.ln

Reputation: 309

Change the Month and Date order First should be month then date... MM/DD/YYYY

var date1 = new Date("11.04.2016");
var date2 = new Date("11.19.2016");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays);

Upvotes: 0

Anton Egorov
Anton Egorov

Reputation: 1374

The easiest way is to use moment.js library:

var date1 = moment('04.11.2016', 'MM.DD.YYYY'),
    date2 = moment('19.11.2016', 'MM.DD.YYYY'),
    diffDays = date2.diff(date1, 'days'); // you can wrap it in Math.abs()

The ugly js way:

var input1 = '04.11.2016',
    parts1 = input1.split('.'),
    date1 = new Date(parts1[2], parts1[1], parts1[0]),
    input2 = '19.11.2016',
    parts2 = input2.split('.'),
    date2 = new Date(parts2[2], parts2[1], parts2[0]),
    timeDiff = Math.abs(date2.getTime() - date1.getTime()),
    diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

Upvotes: 1

Bhavin Shah
Bhavin Shah

Reputation: 2482

var date1 = new Date("11/04/2016");
var date2 = new Date("11/19/2016");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays);

change the format of date.

it should be MM/DD/YYYY

Hope this helps.

Upvotes: 3

Related Questions