getting the difference between two dates [duplicate] - javascript

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

Related

take a date string, add a class to span if expiration date is less than 2 weeks from now

i've been stuck on this problem for a while now and I am ready to pull my hair out :). I have to add a class to a span if a campaign date is expiring in 2 weeks or less. The date retrieved is a string in the following format
07/26/2017
when I run this function, I am passing the datestring as an argument since the method will be attached to the var which holds the string. But for whatever reason this logic isn't working. Am I totally screwing this up? It's failing silently somewhere. Thank you. I know it should be easy but I am caught in a loop.
campMethods.EndDateAlert = function (dateString) {
var currentDate = new Date ();
var twoWeeks = new Date ();
twoWeeks.setDate(currentDate.getDate() + 14)
var $EndDateSpan = $('.campaign-end-date');
if (dateString <= twoWeeks) {
$EndDateSpan.addClass('red');
}
return dateString;
};
You can do that with some Math. The key is, 2 weeks = 14 days.
Here is Pure Javascript example for you:
var date = "07/26/2017".split("/");
var formatedDate = (date[2] + '' + date[0] + '' + date[1]);
var currentDate = new Date();
var today = currentDate.getFullYear() +''+ ("0" + (currentDate.getMonth() + 1)).slice(-2)+''+("0" + currentDate.getDate()).slice(-2);
var compareDay = formatedDate - today;
if(compareDay < 14){// 14 day = 2 week
// do something for less than 2 weeks
console.log('Less than 2 weeks will be expired');
} else {
// also can do something
console.log('more than 2 weeks will be expired.');
}
Javascript Date Reference
Try comparing milliseconds of the dates.
We know that there are 1000 * 60 * 60 * 24 * 14 = 1209600000 milliseconds in two weeks, knowing this we can add 1209600000ms to the current date and compare this to the milliseconds of the due date.
let dueDate = new Date('07/26/2017');
if(Date.now() + 1209600000 > dueDate.getMilliseconds()){
//do stuff
}

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>

Subtracting days/months/years from a Date object

var inputDate = '20/4/2010'.split('/');
var dateFormatted = new Date(parseInt(inputDate[2]), parseInt(inputDate[1]), parseInt(inputDate[0]));
var expiryDate = (dateFormatted.getDate() - 1) + '/' + dateFormatted.getMonth() + '/' + (dateFormatted.getFullYear() + year);
This is the Javascript code I'm using to work out an expiry date given a user inputted date. Currently, the expiry date is original date minus one day and original year minus X.
The problems with this code, firstly, it doesn't take into account invalid dates. For example, if the user supplied date is '1/10/2010', the expiry date will be '0/10/2013' (assuming the expiry date is +3 years).
I could do something like:
var inputDate = '20/4/2010'.split('/');
var day = parseInt(inputDate[0]);
var month = parseInt(inputDate[1]);
var year = parseInt(inputDate[2]);
if (day < 1)
{
if (month == ...)
{
day = 31
month = month - 1;
}
else
{
day = 30
month = month - 1;
}
}
var dateFormatted = new Date(parseInt(inputDate[2]), parseInt(inputDate[1]), parseInt(inputDate[0]));
var expiryDate = (dateFormatted.getDate() - 1) + '/' + dateFormatted.getMonth() + '/' + (dateFormatted.getFullYear() + year);
But more problems arise... Firstly, the code gets a little convoluted. Secondly, this check would have to be done on the day. and then the month. Is there a cleaner, simpler way?
Also, there's a certain circumstance that would involve me needing to calculate the expiry date to the 'end of the month' for that date. For example:
Expiry date is: +3 years
User date is: '14/10/2010'
Expiry date is: '31/10/2013'
I was hoping the Date object would support these calculations but according to https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date, it seems not...
Easy way to see if a date inputed is a valid date:
var d = Date.parse('4/20/2010');
if (isNaN(d.valueOf())) {
alert ("bad date value");
}
Then, here is a dateAdd function that I use regularly. Extends the Date object, so it's easy to use:
Date.prototype.dateAdd = function(size,value) {
value = parseInt(value);
var incr = 0;
switch (size) {
case 'day':
incr = value * 24;
this.dateAdd('hour',incr);
break;
case 'hour':
incr = value * 60;
this.dateAdd('minute',incr);
break;
case 'week':
incr = value * 7;
this.dateAdd('day',incr);
break;
case 'minute':
incr = value * 60;
this.dateAdd('second',incr);
break;
case 'second':
incr = value * 1000;
this.dateAdd('millisecond',incr);
break;
case 'month':
value = value + this.getUTCMonth();
if (value/12>0) {
this.dateAdd('year',value/12);
value = value % 12;
}
this.setUTCMonth(value);
break;
case 'millisecond':
this.setTime(this.getTime() + value);
break;
case 'year':
this.setFullYear(this.getUTCFullYear()+value);
break;
default:
throw new Error('Invalid date increment passed');
break;
}
}
Then just use:
var d = new Date();
d.dateAdd('day', -1).dateAdd('year', 3);
T'da
A similar question has been answered here:
How to add/subtract dates with javascript?
Similar thing can be done for months and years.
For e.g.
var date = new Date('2011','01','02');
alert('the original date is '+date);
var newdate = new Date(date);
newdate.setMonth(newdate.getMonth() - 7);
var nd = new Date(newdate);
alert('the new date is '+nd);
var currentDate = new Date(year,month,day);
var expiryDate = new Date();
expiryDate.setTime(currentDate.getTime() + (3 * 365 * 24 * 60 * 60 * 1000));
using the number of seconds past 1970 is fine for this :-)
oh, you have more rules. well after that you will still have to check for those cases...
Maybe this will be useful to you: http://code.google.com/p/datejs/wiki/APIDocumentation

How to calculate years and months between two dates in 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.

Categories

Resources