You are here: irt.org | FAQ | JavaScript | date | Q1430 [ previous next ]
The following was submitted by Richard Norman
I needed a script which compared today's date and returned a value I could use in a conditional statement. I found the script in Q16 - How can I compare a date in a string with todays date?, but there was a problem, I needed to know wether it was ahead or behind as well as just today. Also, I discovered that when I ran the script listed there, it was not Year 2000 Compliant.
What I ended up doing was modifying the script so that not only could you have a more varied type of input to the function (I added 3 new types), but also I allowed for the Y2K, Y3K, Y4K, etc. problem.
This function is a simple adaptation of the previous function in that it compares the date variables directly and returns a -1 if the date in the string is behind today's date, 0 if it is the same as today's date or the datetype is out of range, and a 1 if it is beyond today's date.
<script language="JavaScript"><!-- function DatePosition(dateString,dateType) { /* function DatePosition parameters: dateString dateType returns: integer (-1, 0, 1) dateString is a date passed as a string in the following formats: type 1 : 19970529 type 2 : 970529 type 3 : 29/05/1997 type 4 : 29/05/97 type 5 : 05/29/1997 type 6 : 05291997 type 7 : 052997 dateType is a numeric integer from 1 to 7, representing the type of dateString passed, as defined above. Returns -1 if the date passed is behind todays date Returns 0 if the date passed is equal to todays date or if dateType is not 1 to 7 Returns 1 if the date passed is ahead of todays date Added Y2K checking. (Works for any century cross over) */ var now = new Date(); var today = new Date(now.getYear(),now.getMonth(),now.getDate()); var century = parseInt(now.getYear()/100)*100; if (dateType == 1) var date = new Date(dateString.substring(0,4), dateString.substring(4,6)-1, dateString.substring(6,8)); else if (dateType == 2) { if ((now.getYear()%100)>=parseInt(dateString.substring(0,2))) { var date = new Date(century+parseInt(dateString.substring(4,6)), parseInt(dateString.substring(2,4)-1), dateString.substring(4,6)); } else { var date = new Date(century-100+parseInt(dateString.substring(0,2)), parseInt(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) { if ((now.getYear()%100)>=parseInt(dateString.substring(6,8))) { document.write(century+parseInt(dateString.substring(6,8)),'<P>'); var date = new Date(century+parseInt(dateString.substring(4,6)), parseInt(dateString.substring(3,5)-1), dateString.substring(0,2)); } else { document.write(century-100+parseInt(dateString.substring(6,8)),'<P>'); var date = new Date(century-100+parseInt(dateString.substring(4,6)), parseInt(dateString.substring(3,5)-1), dateString.substring(0,2)); } } else if (dateType == 5) var date = new Date(dateString.substring(6,10), dateString.substring(0,2)-1, dateString.substring(3,5)); else if (dateType == 6) var date = new Date(dateString.substring(4,8), dateString.substring(0,2)-1, dateString.substring(2,4)); else if (dateType == 7) { if ((now.getYear()%100)>=parseInt(dateString.substring(4,6))) { document.write('datestring Century:',century+parseInt(dateString.substring(4,6)),'<P>'); var date = new Date(century+parseInt(dateString.substring(4,6)), parseInt(dateString.substring(0,2)-1), dateString.substring(2,4)); } else { document.write('datestring Century:',century-100+parseInt(dateString.substring(4,6)),'<P>'); var date = new Date(century-100+parseInt(dateString.substring(4,6)), parseInt(dateString.substring(0,2)-1), dateString.substring(2,4)); } } else return false; if (date < today) { document.write(date.toString(),' is behind ',today.toString(),'<P>'); return -1; } else if (date > today) { document.write(date.toString(),' is ahead of ',today.toString(),'<P>'); return 1; } else { document.write(date.toString(),' is the same as ',today.toString(),'<P>'); return 0; } } //--> </script> <script language="JavaScript"><!-- document.writeln(DatePosition('041500',7)); //--></script>