I have this function called week(), which gives me the current the first (startDate) and last (endDate) day of the week and the week number. I also have these two other functions called weekPlus() and weekMinus(), which contains variables that add or substract from the variables startDate / endDate by 7 and from the week number by 1.
<script>
// First and last day of the current week
var curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
function week() {
var curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
// First day of the week
var first = curr.getDate() - curr.getDay();
// Last day of the week
var last = first + 6;
var startDate = new Date(curr.setDate(first));
var endDate = new Date(curr.setDate(last));
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
function weekPlus() {
// First day of the week
var first = curr.getDate() - curr.getDay();
// Last day of the week
var last = first + 6;
var startDate = new Date(curr.setDate(first + 7));
var endDate = new Date(curr.setDate(last + 7));
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7 + 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
function weekMinus() {
// First day of the week
var first = curr.getDate() - curr.getDay();
// Last day of the week
var last = first + 6;
var startDate = new Date(curr.setDate(first - 7));
var endDate = new Date(curr.setDate(last - 7));
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7 - 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
</script>
<div id="start">start</div>
<div id="end">end</div>
<div id="week">week</div>
<button onclick="week()">current</button>
<button onclick="weekPlus()">add</button>
<button onclick="weekMinus()">substract</button>
So the idea is that when you first click the current button you get the current date of the weeks first and last day and after that you can either add or substract to the dates.
So is there any quick and effective way to increase or decrease the days by 7 and the week number by 1 every time you click the buttons and not just once?
Problems with the code:
Adding or substracting when the month changes, month name doesn't change but stays the same, for example: Sun Oct 29 2017 19:20:14 - Wed Oct 04 2017 19:20:14
When adding or substacting, the week number sometimes jumps by 2, 5 or more.
The problem is that you're defining cur every time you call the function. If you define it outside, you can keep on adding days to the same starting date:
// First and last day of the current week
var curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
function weekPlus()
{
// First day of the week
var first = curr.getDate() - curr.getDay();
// Last day of the week
var last = first + 6;
var startDate = new Date(curr.setDate(first + 7));
var endDate = new Date(curr.setDate(last + 7));
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7 + 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
<div id="start">start</div>
<div id="end">end</div>
<div id="week">week</div>
<button onclick="weekPlus()">add</button>
You had some problems with the curr variable, since you were modifying it as you used it. Keep it as a reference and then you'll have no problems:
// First and last day of the current week
var curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
curr.setHours(0,0,0,0);
function week() {
// First day of the week
var first = curr.getDate() - curr.getDay();
// Last day of the week
var last = first + 6;
var startDate = new Date(curr.setDate(first));
var endDate = new Date(curr.setDate(last));
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
function startOfWeek(date) {
date.setDate(date.getDate() - date.getDay());
return date;
}
function endOfWeek(date) {
date = startOfWeek(date);
date.setDate(date.getDate() + 6);
return date;
}
function weekPlus() {
var startDate = new Date(startOfWeek(curr));
startDate.setDate(startDate.getDate() + 7);
var endDate = endOfWeek(curr);
endDate.setDate(endDate.getDate() + 7);
curr = startDate;
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7 + 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
function weekMinus() {
var startDate = new Date(startOfWeek(curr));
startDate.setDate(startDate.getDate() - 7);
var endDate = endOfWeek(curr);
endDate.setDate(endDate.getDate() - 7);
curr = startDate;
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7 - 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
<div id="start">start</div>
<div id="end">end</div>
<div id="week">week</div>
<button onclick="week()">current</button>
<button onclick="weekPlus()">add</button>
<button onclick="weekMinus()">substract</button>
You can move the curr variable outside of the function and every time you run the function - update the value of that variable:
// First and last day of the current week
var curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
function weekPlus() {
// First day of the week
var first = curr.getDate() - curr.getDay();
// Last day of the week
var last = first + 6;
var startDate = new Date(curr.setDate(first + 7));
var endDate = new Date(curr.setDate(last + 7));
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7 + 1);
};
document.getElementById("week").innerHTML = ("Week " + new Date().getWeekNumber());
curr = startDate;
}
<div id="start"></div>
<div id="end"></div>
<div id="week"></div>
<input type="button" value="Plus" id="plus" onclick="weekPlus()"/>
Related
I'm currently trying to make a date / week counter, where you can increase or decrease the week number alongside the date of the week's first and last day. This is what I've got so far thanks to #ishegg
<script>
var curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
curr.setHours(0,0,0,0);
function week() {
curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
// First day of the week
var first = curr.getDate() - curr.getDay();
// Last day of the week
var last = first + 6;
var startDate = new Date(curr.setDate(first));
var endDate = new Date(curr.setDate(last));
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
function startOfWeek(date) {
date.setDate(date.getDate() - date.getDay());
return date;
}
function endOfWeek(date) {
date = startOfWeek(date);
date.setDate(date.getDate() + 6);
return date;
}
function weekPlus() {
var startDate = new Date(startOfWeek(curr));
startDate.setDate(startDate.getDate() + 7);
var endDate = endOfWeek(curr);
endDate.setDate(endDate.getDate() + 7);
curr = startDate;
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7 + 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
function weekMinus() {
var startDate = new Date(startOfWeek(curr));
startDate.setDate(startDate.getDate() - 7);
var endDate = endOfWeek(curr);
endDate.setDate(endDate.getDate() - 7);
curr = startDate;
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7 - 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
</script>
<div id="start">start</div>
<div id="end">end</div>
<div id="week">week</div>
<button onclick="week()">current</button>
<button onclick="weekPlus()">add</button>
<button onclick="weekMinus()">substract</button>
So after you press the current -button, you'll get the last and first day of the week including the week number.
You can add or substract by 1 week with each button press.
Problems with the code:
After adding or substracting the weeks -> pressing the current -button to reset the weeks to the current date, adding or substracting doesn't start from the current week, but from before you press the current button.
After pressing the add -button couple of times and then pressing substract, the week number instantly goes down by 3, instead of 1. (Same thing reversed, going up by 3, instead of 1)
Whenever the year changes, sometimes the week jumps straight to week 2.
Any idea's how to solve these problems with the code?
Any help is appreciated.
The problem is that the Date#set**** functions mutate the argument. This affects also curr at times you don't really want that to happen.
Solve this by making sure that the functions startOfWeek and endOfWeek do not modify their arguments, but return new Date instances. And then stick to calling those functions where possible, avoiding code duplication. There are only a few places where curr should be modified, and that is in the week (I renamed it to weekReset) and the weekPlus function (and weekMin, but see next point).
I would remove weekMin, as it has the same logic as weekPlus. You can settle the difference by providing weekPlus a parameter.
Don't redefine Date.prototype.getWeekNumber over and over again: just do this once.
Finally, split the rendering logic from the date calculations, again to avoid code duplication. I would create a separate display function for generating the output. Note that you don't need document.getElementById since current browsers create global variables for elements that have an id. This was also the reason I renamed the week function so it would not coincide with the variable for the element with that id.
Here is the revised code:
Date.prototype.getWeekNumber = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
};
var curr;
weekReset();
function display() {
start.textContent = curr;
end.textContent = endOfWeek(curr);
week.textContent = ("Week " + curr.getWeekNumber());
}
function weekReset() {
curr = startOfWeek(new Date());
display();
}
function startOfWeek(date) {
var start = new Date(date);
start.setHours(0,0,0,0);
start.setDate(start.getDate() - start.getDay());
return start;
}
function endOfWeek(date) {
date = startOfWeek(date);
date.setDate(date.getDate() + 6);
return date;
}
function weekPlus(weeks) {
curr.setDate(curr.getDate() + 7 * weeks);
display();
}
<div id="start">start</div>
<div id="end">end</div>
<div id="week">week</div>
<button onclick="weekReset()">current</button>
<button onclick="weekPlus(1)">add</button>
<button onclick="weekPlus(-1)">substract</button>
I want to calculate exact month and days between two dates.
If my start-date is "Jan 12, 2014" and my end-date is "Mar 27, 2017".
I should get as "38 months and 15 days".
But I am able to find only no. of days between start-date and end-date. I need some help to find months and days between start-date and end-date.
Then I need to divide the 15 days by no. of days of the end-date month.
Can anyone help me? I am new to date function.
var date = new Date();
console.log("date: "+date);
var currentDate = $filter('date')(date, "yyyy-MM-dd HH:mm:ss");
$scope.userdob = "2017-01-29";
var dobdate = $filter('date')($scope.userdob, "yyyy-MM-dd HH:mm:ss");
console.log("dob: "+dobdate);
/* differentiate Date */
var date1 = $filter('date')($scope.userdob, "yyyy-MM-dd");
var date2 = $filter('date')(date, "yyyy-MM-dd");
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[0], date1[1], date1[2]);
date2 = new Date(date2[0], date2[1], date2[2]);
// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000)
var date1_unixtime = parseInt(date1.getTime() / 1000);
var date2_unixtime = parseInt(date2.getTime() / 1000);
// This is the calculated difference in seconds
var timeDifference = date2_unixtime - date1_unixtime;
// in Hours
var timeDifferenceInHours = timeDifference / 60 / 60;
// and finaly, in days :)
$scope.timeDifferenceInDays = timeDifferenceInHours / 24;
console.log("timeDifferenceInDays: "+$scope.timeDifferenceInDays);
Try this:
function monthCalculator() {
var d1 = new Date();
var d2 = new Date('2013', '02', 12);
var years = d1.getFullYear() - d2.getFullYear();
var months = d1.getMonth() - d2.getMonth();
var totalMonths = (years * 12) + months;
var d1Date = d1.getDate();
var d2Date = d2.getDate();
var days = d1Date - d2Date;
var d1LastDate = null;
var d2LastDate = null;
if(days < 0) {
var d1LastDate = new Date(d1.getFullYear(), d1.getMonth() + 1, 0).getDate();
var d2LastDate = new Date(d2.getFullYear(), d2.getMonth() + 1, 0).getDate();
if(d1Date != d1LastDate || d2Date != d2LastDate) {
totalMonths -= 1;
days = (new Date(d2.getFullYear(), d2.getMonth() + 1, 0).getDate()) + days;
} else {
days = 0;
}
}
console.log(totalMonths);
return totalMonths;
}
Maybe I'm interpreting it wrong but shouldn't it just be
var diffMonths = date2.getMonth() - date1.getMonth();
var diffDays = date2.getDate() - date1.getDate();
var diffYears = date2.getYear() - date1.getYear();
diffMonths += 12* diffYears
if(diffDays<0){
diffMonths -= 1;
var daysInMonth = new Date(date2.getYear(), date2.getMonth()-1, 0).getDate();
diffDays = daysInMonth + diffDays;
}
console.log('The difference between the two dates is ' + diffMonths + ' months and ' + diffDays + ' days');
Greetings Chris
I create function that loop on dates and enter then to array according to the week and the day in the week.
This is the code of the loop and the functions.
var weeks = [];
//get the week number of the date;
var getWeek = function(date) {
var onejan = new Date(date.getFullYear(), 0, 1);
return Math.ceil((((date - onejan) / 86400000) + onejan.getDay() + 1) / 7);
};
var curr = new Date();
//get the first day of the week
var startDay = new Date(curr.setDate(curr.getDate() - curr.getDay()));
//endDay="2015/9/30"
var endDay = new Date(2015, 8, 30);
while (startDay < endDay) {
if (weeks[getWeek(startDay)] == undefined) {
weeks[getWeek(startDay)] = [];
}
weeks[getWeek(startDay)][startDay.getDay()] = startDay.toString();
var newDate = startDay.setDate(startDay.getDate() + 1);
startDay = new Date(newDate);
}
console.log(weeks);
The code runs and I dont know why it "confuse" with some dates but like the today 2015.9.19 it put it on month 39 but when I run the getWeek on this date it says 38.
var getWeek = function(date) {
var onejan = new Date(date.getFullYear(), 0, 1);
return Math.ceil((((date - onejan) / 86400000) + onejan.getDay() + 1) / 7);
};
alert(getWeek(new Date("2015/9/19")));
so I dont understand what is wrong in the loop.
There is nothing wrong with the loop - the problem is with how you initialize you start date. Hours, minutes and seconds in particular.
Compare these:
getWeek(new Date()); // 39
getWeek(new Date(2015, 8, 19)); // 38
getWeek(new Date(2015, 8, 19, 1)); // 39
I guess that error accumulates, when you do Math.ceil() - you might want a deeper debugging for that.
Anyway, here's how your code may look like:
var weeks = [];
//get the week number of the date;
var getWeek = function(date) {
var onejan = new Date(date.getFullYear(), 0, 1);
return Math.ceil((((date - onejan) / 86400000) + onejan.getDay() + 1) / 7);
};
var curr = new Date();
// -----------------
// Subtract the date, because there is a chance we will get a prev month.
curr.setDate(curr.getDate() - curr.getDay());
// Initialize with diff constructor, so m:h:s will be 0.
var startDay = new Date(curr.getFullYear(), curr.getMonth(), curr.getDate());
// -----------------
//endDay="2015/9/30"
var endDay = new Date(2015, 8, 30);
while (startDay < endDay) {
if (weeks[getWeek(startDay)] == undefined) {
weeks[getWeek(startDay)] = [];
}
weeks[getWeek(startDay)][startDay.getDay()] = startDay.toString();
var newDate = startDay.setDate(startDay.getDate() + 1);
startDay = new Date(newDate);
}
console.log(weeks);
I have to calculate variables in javascript after x days from current day.I have to add some number of days based on some input parameter.
var currentDate = new Date();
var dd = currentDate.getDate();
var mm = currentDate.getMonth()+1;
var yyyy = currentDate.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
currentDate= mm+'/'+dd+'/'+yyyy;
Now I want to get some date after 28 days from currentDate variable but it should not include Saturday and Sunday.
So my question is how to exclude the weekends (2 days) from the 28 (for example).
Any help will be appreciated.
Here's a generic function to add n business days to a date
function addDays(dt, n) {
var rem = n % 5;
var add = 7 * (n - rem) / 5 + rem;
var ret = new Date(dt);
ret.setDate(ret.getDate() + add);
if (ret.getDay() == 6) ret.setDate(ret.getDate() + 2);
else if (ret.getDay() == 0) ret.setDate(ret.getDate() + 1);
return ret;
}
This is really simple. Script below goes through all days between start and end date and checks if it isn't Saturday (tmpDate.getDay() != 6) or Sunday (tmpDate.getDay() != 0)
var start = new Date();
var end = new Date(2016, 6, 1);
var allDays = Math.floor((end.getTime() - start.getTime())/ 86400000);
var workingDays = 0;
for(var i = 0; i < allDays; i++){
var tmpDate = new Date();
tmpDate.setTime(today.getTime() + i * 24*60*60*1000);
if(tmpDate.getDay() != 0 && tmpDate.getDay() != 6)
wokringDays++;
}
alert(workingDays);
This will give you the number of working days:
function getWorkingDays(currentDateObj, numberOfDays)
{
if(numberOfDays < 0) return false;
var futureDateObj = new Date();
futureDateObj.setDate(currentDateObj.getDate() + numberOfDays);
var daysCnt = 1 + Math.round((futureDateObj.getTime()-currentDateObj.getTime())/(24*3600*1000));
var weekCnt = Math.floor( (currentDateObj.getDay() + daysCnt) / 7 );
var weekends = 2 * weekCnt + (currentDateObj.getDay()==0) - (futureDateObj.getDay()==6);
return numberOfDays - weekends;
}
console.log(getWorkingDays(new Date(), 28));
I need to calculate if a date in the table is within 5 days and update the class of the parent row. I was able to do this (in FF and Chrome) but for some reason, it isn't working in IE8.
An item in the table would have a date like "08/28/13" and the TD would have the class "time".
$('td:contains("Scheduled")').parents('tr').addClass('scheduled');
$("td.time").each(function (index) {
var date = $(this).text();
//sep date
var year = date.substring(6, 10);
var month = date.substring(0, 2);
var day = date.substring(3, 5);
alert(month + ' ' + day + ', ' + year);
//today
var d = new Date();
var today = d.getDate();
var m = new Array();
m[0] = "01";
m[1] = "02";
m[2] = "03";
m[3] = "04";
m[4] = "05";
m[5] = "06";
m[6] = "07";
m[7] = "08";
m[8] = "09";
m[9] = "10";
m[10] = "11";
m[11] = "12";
var b = d.getMonth();
var n = parseInt(m[d.getMonth()]);
var monthStart = new Date(year, b, 1);
var monthEnd = new Date(year, b + 1, 1);
var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24);
var nextMonth = n + 1;
var dayDiff = 5 - (monthLength - today);
if ((monthLength - 5) < today && nextMonth == month && day < dayDiff) {
if (!$(this).parents('tr').hasClass('scheduled')) {
$(this).parents('tr').addClass('urgent');
};
};
if (n == month && today < day && day <= (today + 5)) {
if (!$(this).parents('tr').hasClass('scheduled')) {
$(this).parents('tr').addClass('urgent');
};
};
});
I tried the solution in the question that #abc123 commented and got the numerical timestamp- based difference between 5 days. I also updated the way I got today's date. Less code.
$('td:contains("Scheduled")').parents('tr').addClass('scheduled');
$("td.time").each(function (index) {
var date = $(this).text();
var oppTime = date.substring(0, 10);
var d = new Date();
var today = d.toJSON();
var t = today.substring(0, 4) + "/" + today.substring(5, 7) + "/" + today.substring(8, 10);
var timeStamp_oppDate = new Date(oppTime).getTime();
var timeStamp_thisDay = new Date(t).getTime();
var timeDiff = timeStamp_oppDate - timeStamp_thisDay;
if (432000000 >= timeDiff) {
if (!$(this).parents('tr').hasClass('scheduled')) {
$(this).parents('tr').addClass('urgent');
};
};
});