This is a discussion on How can I compare a date in a string with todays date? within the HTML, CSS and Javascript Coding Techniques forums, part of the Web Development category; Hi all... How can I compare a date in a string with todays date? Thanks......
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi all... <script language="JavaScript"> function isitToday(dateString,dateType) { var now = new Date(); var today = new Date(now.getYear(),now.getMonth(),now.getDate()); if (dateType == 1) var date = new Date(dateString.substring(0,4), dateString.substring(4,6)-1, dateString.substring(6,8)); else if (dateType == 2) var date = new Date(dateString.substring(0,2), dateString.substring(2,4)-1, dateString.substring(4,6)); else if (dateType == 3) var date = new Date(dateString.substring(6,10), dateString.substring(3,5)-1, dateString.substring(0,2)); else if (dateType == 4) var date = new Date(dateString.substring(6,8), dateString.substring(3,5)-1, dateString.substring(0,2)); else return false; if (date.toString() == today.toString()) return true; else return false; } if (isitToday("19970529",1)) alert('true'); else alert('false'); if (isitToday("970529",2)) alert('true'); else alert('false'); if (isitToday("29/05/1997",3)) alert('true'); else alert('false'); if (isitToday("02/06/97",4)) alert('true'); else alert('false'); </script> Thanks... |
| |||
| <script language="text/javascript"> function lastMod() { var x = new Date (document.lastModified); Modif = new Date(x.toGMTString()); Year = takeYear(Modif); Month = Modif.getMonth(); Day = Modif.getDate(); Mod = (Date.UTC(Year,Month,Day,0,0,0))/86400000; x = new Date(); today = new Date(x.toGMTString()); Year2 = takeYear(today); Month2 = today.getMonth(); Day2 = today.getDate(); now = (Date.UTC(Year2,Month2,Day2,0,0,0))/86400000; daysago = now - Mod; if (daysago < 0) return ''; unit = 'days'; if (daysago > 730) { daysago = Math.floor(daysago/365); unit = 'years'; } else if (daysago > 60) { daysago = Math.floor(daysago/30); unit = 'months'; } else if (daysago > 14) { daysago = Math.floor(daysago/7); unit = 'weeks' } var towrite = 'Page last changed '; if (daysago == 0) towrite += 'today'; else if (daysago == 1) towrite += 'yesterday'; else towrite += daysago + ' ' + unit + ' ago'; return towrite; } function takeYear(theDate) { x = theDate.getYear(); var y = x % 100; y += (y < 38) ? 2000 : 1900; return y; } document.write(lastMod()) </script> |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Check that a string contains a date. | itbarota | HTML, CSS and Javascript Coding Techniques | 2 | 03-11-2008 09:01 PM |
| Convert String to Date: new Date(string) | oxygen | Flash Actionscript Programming | 3 | 03-07-2008 09:24 PM |
| Convert a date in milliseconds back to a date string | itbarota | HTML, CSS and Javascript Coding Techniques | 2 | 03-03-2008 09:03 PM |
| How can I show the documents last modified date in the format: month, date, year, tim | Pvinothkumar | HTML, CSS and Javascript Coding Techniques | 3 | 12-28-2007 05:58 AM |
| How to convert a date to a globally accepted date format, in DOT NET 2005? | Archer | C# Programming | 1 | 07-25-2007 03:21 AM |