Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date formatting javascript 1

Status
Not open for further replies.

emms

Programmer
Jan 11, 2001
55
GB
i'm building a pop-up calendar to allow users to select a date, this will automatically fill a form field with the date selected,

I'm new to javascript and have been given the function

function setFormDate(ddmmyy){
opener.document.Search.date.value = ddmmyy;
opener.focus();
close();
}

I seem to be getting problems with the date formatting from this - is there anyway i can change the code to return the date in a dd mmm yyyy format, i've tried changing the values but it always returns with a javascript error.

thanks for any help you can give
 
I just looked in my Javascript reference and I don't see any functions which will quickly format a date the way you want so I'll show you a way that requires a little extra coding. Try something like this:

var m1 = "";
var y1 = "";

if ( mmddyy.substr(4,2) > 25 ) y1 = "19" + mmddyy.substr(4,2);
else y1 = "20" + mmddyy.substr(4,2);

if ( mmddyy.substr(2,2) == "01" )
m1 = "Jan";
else if ( mmddyy.substr(2,2) == "02" )
m1 = "Feb";
.....
.....
else if ( mmddyy.substr(2,2) == "12" )
m1 = "Dec";

opener.document.Search.date.value = mmddyy.substr(0,2) + " " + m1 + " " + y1;

This is assuming you are passing a string variable exactly 6 characters long which contains the date in the "ddmmyy" format.

Hope this helps.
GJ
 
thankyou, tried that and it worked great

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top