Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
JavaScript
var obj = { 
  day: '', 
  month: '',
  year: ''
};
var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

var countLeapYear  = function () {
  var y = this.year;
  if(this.month <= 2){
    y--;
  }
  return y/4-y/100+y/400;
};

var bound1 = countLeapYear.bind(obj);
var bound2 = getDifferenceDay.bind(obj);
function getDifferenceDay(Day1, Day2){
  var a = Day1.year*365 + Day1.day;
  for(var i=0; i<Day1.month - 1; i++ ){
    a += months[i];
    a += countleap(Day1);
  }
  var b = Day2.year*365 + Day2.day;
   for(var i=0; i<Day1.month - 1; i++ ){
    a += months[i];
    a += countleap(Day1);
  }
  return (b - a);
}

var Day1 = '1, 2, 2000';
var Day2 = '1, 2, 2004';
var obj1 = new getDifferenceDay();
console.log(obj1.getDifferenceDay(Day1, Day2));


What I have tried:

I tried to calculate days between two dates without any javascript built-in method
Posted
Updated 15-Nov-18 10:18am
v2
Comments
W Balboos, GHB 15-Nov-18 14:37pm    
Did you run it? Does it work? What is it that you want us to do?

Not using built-in methods? Does that mean for the date difference or does it mean anything having to do with dates?

Please revise your question to a specific question.
Member 14056052 16-Nov-18 13:41pm    
I need to calculate the difference between two dates in javascript without using built-in library
Richard Deeming 16-Nov-18 8:54am    
Well, it's clearly not going to work. Your getDifferenceDay function expects two objects with day, month, and year properties; you're passing in two strings, which don't have those properties.

The function getDifferenceDay returns a number. You're calling it as a constructor, and then trying to call the getDifferenceDay method on that returned value, which won't work. Just call the function directly.

It's also extremely messy. What is the purpose of the obj variable? What are the two bind calls meant to be doing?
Member 14056052 16-Nov-18 13:59pm    
Thanks for replying. I tried this one but i am getting NaN
var Day1 = {
day1: '',
month1: '',
year1: ''
};
var Day2 = {
day2: '',
month2: '',
year2: ''
};
var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

var countLeapYear = function () {
var y = this.year1;
if(this.month <= 2){
y--;
}
return y/4-y/100+y/400;
};


var getDifferenceDay = function (Day1, Day2){
var a = Day1.year1*365 + Day1.day1;
for(var i=0; i<Day1.month1 - 1; i++ ){
a += months[i];
a += countleap(Day1);
}
var b = Day2.year2*365 + Day2.day2;
for(var i=0; i<this.month1 - 1; i++ ){
a += months[i];
a += countleap(Day2);
}
return (b - a);
}

var Day1 = '1, 2, 2000';
var Day2 = '1, 2, 2004';
console.log(getDifferenceDay(Day1, Day2));

Can you please help me out if possible
Richard Deeming 16-Nov-18 14:05pm    
Once again:

The getDifferenceDay function expects two parameters. In your updated function, the first parameter needs to have properties called day1, month1, and year1; the second parameter needs to have properties called day2, month2, and year2.

You are passing in two strings.

The string class[^] does not contain any of those properties.

Instead of:
var Day1 = '1, 2, 2000';

you need:
var Day1 = { day1: 1, month1: 2, year1: 2000 };


Similar change for Day2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900