0

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

3
  • momentjs Commented Nov 3, 2016 at 13:35
  • Have you tried logging dates? date2 is null Commented Nov 3, 2016 at 13:36
  • 1
    date2 is invalid date. You have passed 'DD-MM-YYYY', but it should be 'MM-DD-YYYY' (11.19.2016) Commented Nov 3, 2016 at 13:38

12 Answers 12

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)); 

Comments

0

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);

Comments

0

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);

Comments

0

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

Comments

0

Just change the first two lines as below

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

Comments

0

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); })()

Comments

0

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);

Comments

0
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.

Comments

0

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 

Comments

0

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

4 Comments

@MargubAlam, could you post the actual error message and in which environment did you execute that code ?
this error Error at line 7, character 1: Parse error. ')' expected - screencast.com/t/KQLEF0yF8E
@MargubAlam, check my updated answer, I had to figure out in which environment you executed that code, through google search I was able to know that it is called Google Tag Manager, just saw it for the very first time and followed your screen shots to create Custom JavaScript
Happy to help. If this answer or any other one solved your issue, please mark it as accepted. You can take a tour of Stack Overflow from here stackoverflow.com/tour
0

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.