Hi,
I've enhanced a little script my team use at work for submitting overtime (you didn't need to know that really).
Basically I want o make a drop down box which will display the dates of the past 6 fridays.
So what I need to know is how to obtain the date for the next coming friday, then get the date for 7 days prior to it, then then the date 7 days prior to that... and track back til I have 6 weeks in total.
How do I get the date? So if it was yesterday (thursday 30th Dec 2004) I'd want it to get these dates into an array. 31/12/04, 24/12/04, 17/12/04, 10/04/04, 03/12/04 and 26/11/04.
I don't need someone to write my code is just the obtaining of the date on a particular day I'm unsure of?
Javascript get day of week (bit more complex)
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Got this working myself. My team leader asked for 4 weeks not six so here it is (I've used PHP tags purely for highlighting):
The reason I added a hidden element was because of the DOM techniques my validator uses. The vaildation would have failed with a select tag, I needed the date in an input tag.
Code: Select all
//Gets the date on Friday
function getFridaysDate() {
todaysDate = new Date() //Date today
var todaysDay = todaysDate.getDay() //The day today (0-6, sun-sat)
daysUntilFri = 5-todaysDay //Num of days until friday
var dateOnFri = new Date(todaysDate.getTime()+(daysUntilFri*86400000)) //Move forward correct num of days (86400000 is millisecs per day)
return dateOnFri
}
//Gets the dates of all fridays over the last 6 weeks
function fourWks(theDate) {
dateArray = new Array() //Start an array
startDate = theDate
dateArray[0] = startDate //First element is next fri
for (var i=1; i<4; i++) {
prevFri = new Date(startDate.getTime()-(7*86400000))
dateArray[i] = prevFri
startDate = prevFri
}
return dateArray
}
function makeBox() {
dateOnFri = getFridaysDate()
fourWksBack = fourWks(dateOnFri) //Is an array of dates
var dropBox = '<input type="hidden" name="Date" id="dateField">'
dropBox += '<select name="SelectedDate" class="formel" onChange="updateDate(this)">'
dropBox += '<option value=""> -- Date -- </option>'
for (var i=0; i<4; i++) {
var d = fourWksBack[i].getDate()
if (!/^\d\d$/.test(d)) { //Twin digits only
d = '0'+d
}
var m = fourWksBack[i].getMonth()+1
if (!/^\d\d$/.test(m)) { //Twin digits only
m = '0'+m
}
var y = fourWksBack[i].getYear()
dropBox += '<option value="'+d+'/'+m+'/'+y+'">'+d+'/'+m+'/'+y+'</option>'
}
dropBox += '<select>'
return dropBox
//return fourWksBack
}
var theDrop = makeBox() //Will be used to put into document
//When the drop box is used this adds the date to a hidden element
function updateDate(el) {
document.getElementById('dateField').value = el.options[el.selectedIndex].value //Add date
}- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact: