Forum Moderators: phranque

Message Too Old, No Replies

javascript: convert August 4, 2003 to date mm/dd/yyyy

Javascript: convert text date to mm/dd/yyyy

         

bmatth1

11:21 pm on Sep 2, 2003 (gmt 0)

10+ Year Member



function passvalue(){
document.form1.select2.value=document.form1.select.value;
}

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

MonkeeSage

12:52 am on Sep 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi 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

bmatth1

1:35 am on Sep 3, 2003 (gmt 0)

10+ Year Member



Can you explain the date.replace(/\,\g.\,"") function?

I can't seem to find this one. I think it takes /\, and comma, and replaces it with "" (null) - am I close?

MonkeeSage

2:04 am on Sep 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yup, very close. Replaces a "," with "" (null) :)

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

macrost

6:46 am on Sep 3, 2003 (gmt 0)

10+ Year Member



Jordan,
I just wanted to say, very beautiful function!

Mac

P.S. I have a good regex for email and phone number validation if any would like it.

bmatth1

10:07 am on Sep 3, 2003 (gmt 0)

10+ Year Member



Mac,

Yes, by all means please share this.

Jordan,
Thank you. This is very useful and interesting.

Thanks guys for the help.
Bob