Forum Moderators: phranque
In the second part of this statement, document.form1.select.value, this text box will contain the text form of a date, such as "August 4, 2001". Is there a function that will convert this text field to a date field in the form mm/dd/yyyy?
Thank you,
Bob
There is no built in function, what you would have to do is something like this:
var months = new Array(12);
months[0] = "january";
months[1] = "febuary";
months[2] = "march";
months[3] = "april";
months[4] = "may";
months[5] = "june";
months[6] = "july";
months[7] = "august";
months[8] = "septemeber";
months[9] = "october";
months[10] = "november";
months[11] = "december";
function convertDate(date) {
date = date.replace(/\,/g, "");
var bits = date.split(" ");
bits[0] = bits[0].toLowerCase();
var month = 1, day = bits[1], year = bits[2];
for (var i = 0; i < 12; i++) {
if (bits[0] == months[i]) {
month = i + 1;
}
}
var newDate = month + "/" + day + "/" + year;
return newDate;
}
Then just incorporate the new function into your present on, like so:
function passvalue(){
document.form1.select2.value = convertDate(document.form1.select.value);
}
Hope that helps
Jordan
The replace() method uses a Regular Expressions (regexp) to replace a string, or parts of a string, with another one. It can be very powerful, but it's also good for quick little stuff like this.
The syntax of the replace method is:
variable.replace(pattern, "replacement string");
pattern - The regexp pattern.
This can de done two ways in JavaScript, well three actually, but two are the same thing and just involve and extra variable:
1. /regexp match pattern/gi - this is put directly in the replace() method as we did
2. var variablename = /regexp match pattern/gi; - this alloows us to use the variablename in the replace() method
3. var variablename = new RegExp("regexp match pattern", "gi"); - this does the same as 2 but is only initialized when the replace() method is actually called (this will save a bit of memory with alot of replaces and such but isn't usually any more beneficial).
You'll notice the "gi" in the examples above. These are modifiers that tell the replace() method how to behave. "g" means global and acts on every instance of the match, and "i" means case-insensitive.
The only other thing to note is the "\" on the front of "\,", this is to indicate an escape sequence in JavaScript. It means, essentially, not to treat the character that follows it as anything meaningful to JS (where otherwise it might be), but to take it as the literal character. I wasn't sure if "," has any special meaning in a regexp pattern, so I escaped it just to be on the safe side.
Prolly more info than you wanted to know, lol, but knowing is half the battle (G.I Joe!) :)
Jordan