Team:KULeuven/Tools/New Day/Date Retriever
From 2008.igem.org
(Difference between revisions)
m |
m |
||
(99 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
<html> | <html> | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
- | var | + | /* |
+ | * @author KULeuven | ||
+ | */ | ||
+ | var monthNames = new Array('January', 'February', 'March', 'April', 'May', | ||
'June', 'July', 'August', 'September', 'October', 'November', 'December'); | 'June', 'July', 'August', 'September', 'October', 'November', 'December'); | ||
- | + | document.write(pageDate().toString().substring(0,11)); | |
- | function | + | /* Extracts the corresponding calendar date out of the page url */ |
- | // | + | function pageDate() { |
- | var | + | // Retrieve page url |
- | + | var urlTokens = location.href.split("/"); | |
- | var | + | var urlLast = urlTokens[urlTokens.length - 1]; |
- | var | + | // Retrieve page date |
- | var | + | var wikiTokens = urlLast.split("#"); |
- | + | var wikiFirst = wikiTokens[0]; | |
- | + | ||
- | // | + | // Retrieve seperate date components |
- | + | var dateTokens = wikiFirst.split("_"); | |
- | + | ||
- | + | var day = parseInt(dateTokens[0]); | |
- | + | var month; | |
- | var | + | for (x in monthNames) { |
- | + | if(dateTokens[1] == monthNames[x]) {month = x; break;} | |
- | + | ||
- | + | ||
- | + | ||
} | } | ||
+ | var year = parseInt(dateTokens[2]); | ||
+ | // Construct a Date object and return the result | ||
var date = new Date(); | var date = new Date(); | ||
- | date. | + | date.setFullYear(year, month, day); |
return date; | return date; | ||
} | } | ||
- | function | + | /* Retrieves date ndays before this page date */ |
- | var | + | function datePast(ndays) { |
- | + | var pastDate = new Date(); | |
- | + | // getDate, setDate methods are rubbish, stick to numeric substraction! | |
- | + | var ntime = pageDate().getTime() - ndays*86400*1000; | |
- | + | pastDate.setTime(ntime); | |
- | + | ||
- | + | return pastDate; | |
} | } | ||
- | function | + | /* Searches date ndays after this page date */ |
- | var | + | function dateFuture(ndays) { |
+ | var futureDate = new Date(); | ||
+ | // getDate, setDate methods are rubbish, stick to numeric adding! | ||
+ | var ntime = pageDate().getTime() + ndays*86400*1000; | ||
+ | futureDate.setTime(ntime); | ||
- | var | + | return futureDate; |
- | + | } | |
+ | |||
+ | /* Returns the date of Friday previous week */ | ||
+ | function previousWeekFriday() { | ||
+ | var dayOfWeek = pageDate().getDay(); | ||
+ | var previousWeekFriday = new Date(); | ||
- | + | switch(dayOfWeek) { | |
- | switch( | + | // case 0 is Sunday, substract 9 days |
- | // Sunday | + | case 0: previousWeekFriday = datePast(9); break; |
- | case 0: | + | default: previousWeekFriday = datePast(dayOfWeek + 2); break; |
- | default: | + | |
} | } | ||
- | + | return previousWeekFriday; | |
- | + | ||
- | return | + | |
} | } | ||
- | function | + | /* Returns the date of Monday next week */ |
- | var | + | function nextWeekMonday() { |
+ | var dayOfWeek = pageDate().getDay(); | ||
+ | var nextWeekMonday = new Date(); | ||
- | + | switch(dayOfWeek) { | |
- | + | // case 0 is Sunday, add 1 day | |
+ | case 0: nextWeekMonday = dateFuture(1); break; | ||
+ | default: nextWeekMonday = dateFuture(8 - dayOfWeek); break; | ||
+ | } | ||
- | var | + | return nextWeekMonday; |
- | switch( | + | } |
- | // | + | |
- | case 6 | + | /* Returns the date of Friday this week */ |
- | default: | + | function thisFriday() { |
+ | var dayOfWeek = pageDate().getDay(); | ||
+ | var thisFriday; | ||
+ | |||
+ | switch(dayOfWeek) { | ||
+ | // case 0 is Sunday, substract 2 days | ||
+ | case 0: thisFriday = datePast(2); break; | ||
+ | // case 6 is Saturday, substract 1 day, can also be merged into default, in which case we look -1 day // ahead, corresponding to going 1 day back in the past | ||
+ | case 6: thisFriday = datePast(1); break; | ||
+ | default: thisFriday = dateFuture(5 - dayOfWeek); break; | ||
} | } | ||
- | + | return thisFriday; | |
+ | } | ||
- | + | /* Converts a date into a valid url for the KULeuven wiki and loads the page */ | |
+ | function goToDate(date) { | ||
+ | // base wiki url of KULeuven | ||
+ | var URL = "https://2008.igem.org/Team:KULeuven/"; | ||
+ | URL += date.getDate(); | ||
+ | URL += '_'; | ||
+ | // month indexing of Date object index matching to array monthNames indexing | ||
+ | URL += monthNames[date.getMonth()]; | ||
+ | URL += '_'; | ||
+ | URL += date.getFullYear(); | ||
+ | // redirect browser to desired day | ||
+ | window.location.href = URL; | ||
} | } | ||
</script> | </script> | ||
Line 92: | Line 124: | ||
</tr> | </tr> | ||
<tr align="center"> | <tr align="center"> | ||
- | <td align="left" width="25%"><a href="javascript:void(0);" onClick=" | + | <td align="left" width="25%"><a href="javascript:void(0);" onClick="goToDate(previousWeekFriday());">< previous friday</a></td> |
- | <td width="25%"><a href="javascript:void(0);" onClick=" | + | <td width="25%"><a href="javascript:void(0);" onClick="goToDate(datePast(1));">← yesterday</a></td> |
- | <td width="25%"><a href="javascript:void(0);" onClick=" | + | <td width="25%"><a href="javascript:void(0);" onClick="goToDate(dateFuture(1));">tomorrow →</a></td> |
- | <td align="right" width="25%"><a href="javascript:void(0);" onClick=" | + | <td align="right" width="25%"><a href="javascript:void(0);" onClick="goToDate(nextWeekMonday());">next monday ></a></td> |
</tr> | </tr> | ||
</table> | </table> |
Latest revision as of 23:45, 29 October 2008
<< return to notebook | return to homepage >> | ||
< previous friday | ← yesterday | tomorrow → | next monday > |