How to calculate years and months between two dates in Javascript? - javascript

Is there a way to calculate the count of years (considering leap years also) and months between two different dates in Javascript?

Here is the best way I know to get years and months:
// Assumes Date From (df) and Date To (dt) are valid etc...
var df= new Date("01/15/2010");
var dt = new Date("02/01/2012");
var allMonths= dt.getMonth() - df.getMonth() + (12 * (dt.getFullYear() - df.getFullYear()));
var allYears= dt.getFullYear() - df.getFullYear();
var partialMonths = dt.getMonth() - df.getMonth();
if (partialMonths < 0) {
allYears--;
partialMonths = partialMonths + 12;
}
var total = allYears + " years and " + partialMonths + " months between the dates.";
var totalMonths = "A total of " + allMonths + " between the dates.";
console.log(total);
console.log(totalMonths);
return {jaren: allYears, maanden: partialMonths};

This might be a helpful source:
http://www.merlyn.demon.co.uk/js-date1.htm#DYMD

The following snippet contains my solution. It's based on the idea that a currentDate - pastDate already is the result.
If you don't want negative results, you have to check that years and month is not less than 0.
const pastDate = '1989-02-10'
const period = new Date(new Date().getTime() - new Date(pastDate).getTime())
const years = period.getFullYear() - 1970 // at 1970 the date calendar starts
const months = period.getMonth()
console.log('years: ', years, ', month: ', months)

You will find a complete javascript function here with validation.
Edit: Link is dead - here is a simple JS line that calculates the difference in months between two dates:
return dateTo.getMonth() - dateFrom.getMonth() +
(12 * (dateTo.getFullYear() - dateFrom.getFullYear()));
That is assuming that you have the dates in two variables called dateTo and dateFrom.

Have a look at the JavaScript Date object.

Related

How to make this function not return a negative date diffence?

The function is trying to fund the difference between to dates, but I am struggling to not return a negative number if the date is past a certain point. I have tried a few work around like using ABS but it can cause problems in future areas.
var DateCalc = {};
DateCalc.totalDaysLeft = 0;
DateCalc.calculate = function(dateToParse) {
DateCalc.init(dateToParse);
return DateCalc.stringify(DateCalc.years(), DateCalc.months(), DateCalc.days());
};
DateCalc.init = function(dateToParse) {
var date = DateCalc.parseDate(dateToParse);
var today = Date.now();
var oneDay = 24 * 60 * 60 * 1000;
DateCalc.totalDaysLeft = Math.floor((date - today) / oneDay);
};
DateCalc.parseDate = function(dateToParse) {
var dateVars = dateToParse.split(',').map(Number);
return new Date(dateVars[0], dateVars[1] - 1, dateVars[2]);
};
DateCalc.years = function() {
var years = Math.floor(DateCalc.totalDaysLeft / 365);
DateCalc.totalDaysLeft -= Math.floor(years * 365);
return years;
};
DateCalc.months = function() {
var months = Math.floor(DateCalc.totalDaysLeft / 30);
DateCalc.totalDaysLeft -= Math.floor(months * 30);
return months;
};
DateCalc.days = function() {
return Math.floor(DateCalc.totalDaysLeft / 24);
};
DateCalc.stringify = function(years, months, days) {
var dateString = "";
if (years !== 0)
dateString += years + " years, ";
if (months !== 0)
dateString += months + " months, ";
dateString += days + " day(s).";
return dateString;
};
//here is the .abs() code.
function age(year, month, day) {
var yearDifference = Math.abs(new Date().getFullYear() - year);
var monthDifference = Math.abs(new Date().getMonth() - month + 1);
var dayDifference = Math.abs(new Date().getDate() - day);
var differences = {
year: yearDifference,
month: monthDifference,
day: dayDifference
};
var final = [];
for (var time in differences) {
if (differences[time] > 0) {
var addString = differences[time] + " " + time;
if (differences[time] > 1) {
addString += "s"
}
final.push(addString);
}
}
return final.join(" ");
};
console.log(age(2017, 11, 17));
console.log(age(2016, 1, 2));
//if you tried to look up how far away next January is while you're in December, it will tell you it is 1 year 11 months from now instead of 1 month. This is because it adds the 11 months instead of subtracting it.I am trying to find a solution, as this function is more conscise but the other is more versatile. The function above, I replaced .floor() with rounds the value down with .abs() hoping it would just use the absolute value of the given operation, however, this was not the case.
The problem is that you are using the .abs() function in an inappropriate way. Most mathematical functions do no obey the distributive rule, and the .abs() function belongs to these. For an easier understanding, let us forget your current problem for a moment and let us examine a simple, reduced example:
Let's say you want to know the absolute value of -10. Obviously, the correct result is +10.
On the other hand, -10 could be written as (-20 + 10). But nevertheless, you can not compute abs(-10) using that knowledge:
abs(-20 + 10) = 10, but
abs(-20) + abs(+10) = 30
Applying that knowledge to your problem, we see that abs(Y years + M months + D days) is generally NOT equal to (abs(Y years) + abs(M months) + abs(D days)).
Regarding this problem, there is the additional oddity that each of the terms of the result has another unit, and that the terms depend on each other (e.g. there can be no term like "13 months", because that would be "1 year plus 1 month"), but I won't go into further detail here.
There is a simple solution:
1) Determine the desired resolution of your result (i.e. should your result be accurate to seconds, attoseconds, days or something else).
2) Convert the two dates into the unit determined in step 1), using a randomly chosen, yet fixed point in time as the common starting point.
3) Now you can subtract the two (converted) dates and use the .abs() function without problems.
4) Convert the result back into human readable form.
How do you do that in practice? Well, steps 1), 3) and 4) are easy, but what about step 2)?
Nearly every OS I know (and thus, nearly every programming language) does the conversion needed in step 2) for you. More often than not, the fixed point in time is 1970-01-01 00:00:00, and the OS / programming language provides routines to convert any date / time to the number of seconds (or some other unit) which have elapsed since this fixed point.
For example, in JavaScript, the myDate.getTime() function returns the number of milliseconds which have passed since 1970-01-01 up to myDate.
So convert both dates to "milliseconds since 1970-01-01" and subtract them. Use the .abs() function on the result. Then you have the desired time span as a value of positive milliseconds. Convert that back to human readable form, i.e. years, months and days (which is no problem, is it?)
A second simple solution (just for avoiding negative results):
I hope that you agree with me that comparing two dates is much easier than computing the difference between them (first compare the year; if the years differ, you have undoubtedly found the "greater" date; if the years are equal, do the same with the months, and so on). Then exchange the two dates if necessary. That way, you always can make sure that you subtract the "smaller" date from the "greater" date and that the result always will be positive.
But please note that even when doing so there will still be negative results in parts of the calculation when actually subtracting the dates, so you would have exactly the same problems when using the .abs() function.
A more complicated solution:
You could do the subtraction yourself as well, but then the .abs() function won't help you much. One of the algorithms I can think of could work like a subtraction which is done by hand (I mean the subtraction of normal numbers you have learned in school):
Begin with the least significant unit (for example the days). Subtract the days; if the result is negative, then add 28, 29, 30 or 31 (depending on the month) and make a carry to the months, otherwise keep the result; then do the same thing with the months, and so on. But as I already wrote in my comment, there are many pitfalls when doing so (leap years, months have different numbers of days, and so on), and the .abs() function will not help you here.
Conclusion:
Personally, I would prefer the first (simple) solution I have given. It is easy, understandable and future-proof.
//initial variables
var today = new Date();
var day = today.getDate();
var month = today.getMonth() + 1;
var year = today.getFullYear();
var otherDate = new Date();
var day2 = 0;
var month2 = 0;
var year2 = 0;
if (day < 10) {
day = '0' + day;
}
if (month < 10) {
month = '0' + month;
}
function age(day2, month2, year2) {
dayConv = day2;
monthConv = month2;
yearConv = year2;
newDate = day - dayConv;
newMonth = month - monthConv;
newYear = year - yearConv;
}
function mathDate() {
if (newYear >= 1) {
if (newMonth >= 1) {
if (newDate >= 1) {
console.log(newYear + " years and " + newMonth + " months and " + newDate + " days.");
return newYear + " years and " + newMonth + " months and " + newDate + " days.";
} else if (newDate <= 0) {
console.log(newYear + " years and " + newMonth + " months.");
return newYear + " years and " + newMonth + " months.";
}
} else if (newMonth <= 0) {
console.log(newYear + " years and " + newDate + " days.");
return newYear + " years and " + newDate + " days.";
}
} else if (newYear <= 1) {
if (newMonth >= 1) {
console.log(newMonth + " months and " + newDate + " days.");
return newMonth + " months and " + newDate + " days.";
} else if (newDate <= 0) {
console.log(newMonth + " months.");
return newMonth + " months.";
} else if (newMonth <= 0) {
console.log(newDate + " days.");
return newDate + " days.";
}
}
}
age(13, 4, 2016);
mathDate();
Here is the answer I was able to create.

Adding Day to Date to Make Current Date Tomorrow - Javascript [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 8 years ago.
Hi Everyone and thanks in advance for the help. I'm having an issue with getting my Javascript date code to advance from the current date to tomorrow's date. I've searched here in previous posts but the answers don't seem to be working.
<SCRIPT LANGUAGE="JavaScript">
// Get today's current date.
var now = new Date();
// Array list of days.
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
// Array list of months.
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
// Calculate the number of the current day in the week.
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
// Calculate four digit year.
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;
}
// Join it all together
today = days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear())) ;
</script>
Where would the + 1 be inserted in order to make the date tomorrow's date?
now.setDate(now.getDate() + daysToAdd);
should do the trick. It's not even jQuery, just Javascript. If you're getting the 1 added to the end of the string make sure now is still being treated as a Date and hasn't been reassigned somewhere to a different type.
It shouldn't matter whether daysToAdd is a literal or a variable, either. Both should work.
More specifically, this should work:
<SCRIPT LANGUAGE="JavaScript">
// Get today's current date.
var now = new Date();
now.setDate(now.getDate() + 1);
// Array list of days.
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
// Array list of months.
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
// Calculate the number of the current day in the week.
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
// Calculate four digit year.
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;
}
// Join it all together
today = days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear())) ;
</script>

How to properly calculate remaining time in JS using getTime() from the two dates?

I'm trying to calculate remaining time (ex: 10 years, 2 months and 10 days from today(2014/03/02) in JS using this function:
var d2 = new Date(2024, 3, 12);
var d1 = new Date();
var d0 = new Date(1970, 0, 1);
var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) );
var years = diff.getFullYear();
var months = diff.getMonth();
var days = diff.getDay();
alert("remaining time = " + years + " years, " + months + " months, " + days + " days.");
But instead of get the 10 years difference, I got 1980 years difference (though the days difference I understand that are produced buy the variation of days in months and years):
Is it possible to perform this "remaining time" operation using this strategy? If so, how to get the expected result?
Here the function in a JS shell: jsfiddle.net/3ra6c/
I find here the solution I was looking for:
var date1 = new Date();
var date2 = new Date(2015, 2, 2);
var diff = new Date(date2.getTime() - date1.getTime());
var years = diff.getUTCFullYear() - 1970; // Gives difference as year
var months = diff.getUTCMonth(); // Gives month count of difference
var days = diff.getUTCDate()-1; // Gives day count of difference
alert("remaining time = " + years + " years, " + months + " months, " + days + " days.");
And it seems to work very well!
As I explained, Date is not equipped with such functions and you have to do the math yourself. You can get the milliseconds difference between two dates, but then it's down to doing the math to represent that as you wish.
An example of using moment.js, loaded with require.js, with humanized approximations.
Javascript
require.config({
paths: {
moment: 'http://momentjs.com/downloads/moment.min'
}
});
require(['moment'], function (moment) {
var x = moment(new Date(2024, 3, 12)).from(new Date());
console.log(x);
});
Output
in 10 years
On jsFiddle
Look at their docs to see how you can humanize the output, you may want a little more detail.
var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) )
Why do you add d0? Try to remove it.

getting the difference between two dates [duplicate]

This question already has answers here:
How do I get the difference between two Dates in JavaScript?
(18 answers)
Closed 9 years ago.
I'm working on an assignment where I must use Javascript. In my application a user types a date into a form. Then I try to compare that date with the current date. The difference is the number of days they have to complete a task. However, I'm having a bit of trouble when it comes to the calculations. dueDate is a paramater for an object dynamically created.
function getFormData() {
var adate = document.getElementById("dueDate").value;
if (checkInputText(dueDate, "Please enter a date")) return;
...
}
function processDate(adate) {
var splitArray = adate.split("-");
var year = splitArray[0];
var month = splitArray[1] - 1;
var day = splitArray[2];
var date = new Date(year, month, day);
var dateParse = date.getTime();
return dateParse;
}
function compareDates(dueDate) { //dueDate is the value from the form
var cdate = new Date();
console.log("this is todays date" + " " + cdate); //shows correct date
var cdateparse = Date.parse(cdate);
var dueDateparse = Date.parse(dueDate);
var diff = dueDateparse - cdateparse;
var daysCal = diff / 1000 / 60 / 60 / 24;
var days = parseInt(daysCal); //where I'm having trouble
console.log(days);
if (days == 0) {
mymessage = " you have to do this task today";
}
try {
if (days < 0) {
mymessage = "this task is overdue by" + " " + -days + " " + "days";
throw new Error("you are overdue");
}
} catch (ex) {
alert(ex.message);
return;
}
if (days > 0) {
console.log("the difference is greater than 0");
mymessage = "you have" + " " + days + " " + "more days";
}
}
The issue happens when I put in the current date into the form. I've tried math.floor and math.round but the number is always rounded up and throws my message, saying that the task is overdue. Using parseInt has brought me closest to my desired outcome, but when I put in tomorrow's date it says that I am overdue. Any suggestions?
http://jsfiddle.net/sujesharukil/QspFj/
use
var days = Math.ceil(daysCal);
instead of parseInt.
You should beware of that new Date(year, month, day); creates a timestamp for 0:00 AM.
So everything that happens on that day, will already have a negative diff (> -1, though). So you should use Math.ceil instead of rounding. Or you set the deadline to 23:59:59 (i.e. just increase the day by 1).

javascript add month to date [duplicate]

This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 9 years ago.
I want to add 1 Month or 6 Month to a given Date.
But if i add one Month, the year isnt incremented. And if i add 6 Month to June, i got the Month 00 returned BUT the year is incremented.
Could you please help me out?
function addToBis(monthToAdd){
var tmp = $("#terminbis").val().split('.');
var day = tmp[0];
var month = tmp[1];
var year = tmp[2];
var terminDate = new Date(parseInt(year),parseInt(month), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);
day = "";
month = "";
year = "";
if(terminDate.getDate() < 10){
day = "0"+terminDate.getDate();
} else{
day = terminDate.getDate();
}
if(terminDate.getMonth() < 10){
month = "0"+terminDate.getMonth();
} else{
month = terminDate.getMonth();
}
year = terminDate.getFullYear();
$("#terminbis").val(day+"."+month+"."+year);
}
getMonth returns a number from 0 to 11 which means 0 for January , 1 for february ...etc
so modify like this
var terminDate = new Date(parseInt(year),parseInt(month - 1), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);
and
month = terminDate.getMonth() + 1;
You should use the javascript Date object's native methods to update it. Check out this question's accepted answer for example, it is the correct approach to your problem.
Javascript function to add X months to a date
The function can be written much more concisely as:
function addToBis(monthToAdd){
function z(n) {return (n<10? '0':'') + n}
var tmp = $("#terminbis").val().split('.');
var d = new Date(tmp[2], --tmp[1], tmp[0]);
d.setMonth(d.getMonth() + monthToAdd);
$("#terminbis").val(z(d.getDate()) + '.' + z(d.getMonth() + 1)
+ '.' + d.getFullYear();
}
The value of terminbis and monthToAdd should be validated before use, as should the date generated from the value.

Categories

Resources