Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing two dates

Status
Not open for further replies.

d222222

Programmer
Jun 12, 2007
34
US
I have a date that is in a form that is retrieved from the database and I need to check the date and see if it is more than 6 months old and if it is, make the row red. However the only way I can get it to work is if I use "innerHTML" and hardcode the date. I'm trying to find the code that will recognize it as a date value and not a string.

This is what works as a string if it's hardcoded:

Code:
		for (var i=0, row; row = dataRows[i]; i++) {
		
			var children = row.childNodes;

			for (var j = 0; j < grid.columns.length ; j++) {

				if (grid.columns[j].id == 'rm_coords.date_updated' && children[j].innerHTML < '11/18/2008') {

I have also tried the following to get the date to compare and it doesn't work. Here is that code:

Code:
        var myDate = new Date();  
        myDate.setDate(myDate.getDate()-180);
        var myNewDate = myDate.getMonth()+"|"+myDate.getDate()+"|"+myDate.getYear()

		for (var i=0, row; row = dataRows[i]; i++) {
		
			var children = row.childNodes;

			for (var j = 0; j < grid.columns.length ; j++) {

if (grid.columns[j].id == 'rm_coords.date_updated' && children[j].nodeValue < myNewDate) {

Thanks.
 
I've been playing with it and now I get it to read the date but it's not quite right. I'm using an alert popup box so that I can see the date the it is using. Here is the code:

Code:
        var myDate = new Date();  
        myDate.setDate(myDate.getDate()-160);
        var myNewDate = myDate.getMonth()+"|"+myDate.getDate()+"|"+myDate.getYear()
        alert(myNewDate); 

		for (var i=0, row; row = dataRows[i]; i++) {
		
			var children = row.childNodes;

			for (var j = 0; j < grid.columns.length ; j++) {

This is getting all of the records that are between the myNewDate (current date - 180 days) and October. I need them for all of the records prior to the 180 day mark.

if (grid.columns[j].id == 'rm_coords.date_updated' && children[j].innerHTML < myNewDate) {
row.style.background = (row.className == 'dataRow') ? '#FF5555' : '#FF5555';

[/code]

Also, the previous code was for making the rows that apply, alternate colors. We want all of the ones that apply to be the alternate. However I'm not sure about which code to change without messing it all up.
 
- You shouldn't be doing a string compare on dates unless they are in the format YYYYMMDD as your results will not always be accurate. E.g. if you use DD/MM/YYYY:

'15/03/1970' < '25/01/1970' will return 'true' even though it isn't, and if you use MM/DD/YYYY:

'11/25/1970' < '01/25/2008' will return 'false' even though it isn't.

So if the time component isn't important, then always use YYYYMMDD.

You might also find the 'parse' method of the Date object useful (
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top