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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting input date (format:ddmmyyyy) to format: Monday 7th May 2001

Status
Not open for further replies.

jbl100

Programmer
May 7, 2001
1
GB
Hello all,

i've got a bit of a problem. I've got a page where i would like users to enter the date in the first textbox (in the format dd/mm/yy) and then using javascript, automatically convert that date to format (Monday 7th May 2001).

I've got it to work to some degree, however, the weekday produced is inaccurate, so is the last day of the month and December.

Hope you can help.

Code is attached below, Jonathan

<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<script language=&quot;javascript&quot;>
<!--display date-->
dayName = new Array (&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;)
dayNum = new Array (&quot;1st&quot;, &quot;2nd&quot;, &quot;3rd&quot;, &quot;4th&quot;, &quot;5th&quot;, &quot;6th&quot;, &quot;7th&quot;, &quot;8th&quot;, &quot;9th&quot;,&quot;10th&quot;, &quot;11th&quot;, &quot;12th&quot;, &quot;13th&quot;, &quot;14th&quot;, &quot;15th&quot;, &quot;16th&quot;, &quot;17th&quot;, &quot;18th&quot;, &quot;19th&quot;, &quot;20th&quot;, &quot;21st&quot;, &quot;22nd&quot;, &quot;23rd&quot;, &quot;24th&quot;, &quot;25th&quot;, &quot;26th&quot;, &quot;27th&quot;, &quot;28th&quot;, &quot;29th&quot;, &quot;30th&quot;,&quot;31st&quot;)
monName = new Array (&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;)

function convDate()
{
var strMainString= document.form1.txtDateNum.value; //date typed in 1st textbox

var year= strMainString.substring(6,10); //last four
var month= strMainString.substring(3,5); //middle two
var day= strMainString.substring(0,2); //first two


var test = new Date(year, month, day);

output=dayName[test.getDay()]+ &quot; &quot; +dayNum[test.getDate()-1]+&quot; &quot; +monName[test.getMonth()-1]+ &quot; &quot; +year;
document.form1.txtDateText.value=output;




}


</script>

</HEAD>
<BODY>


<form method=post action=&quot;update_wkact2.asp&quot; name=form1 onSubmit=&quot;&quot;>
<center>
Date(dd/mm/yyyy):<input type=&quot;text&quot; name=&quot;txtDateNum&quot; size=15 onBlur=convDate()><br>
Date(in words): <input type=&quot;text&quot; name=&quot;txtDateText&quot; size=25 value=&quot;&quot;><br>
Events:<textarea name=&quot;txtDaysEvents&quot; rows=&quot;4&quot; cols=&quot;40&quot; wrap=&quot;no&quot;></textarea><br>
<input type=&quot;submit&quot; value=&quot;Submit&quot;>
</center>
</form>
</BODY>
</HTML>
 
You could bastardise my somewhat large function in the Javascript FAQ section (FAQ number 1b I think). Currently it converts most inputs to DD-MON-YYYY but its easy to change - just alter the newMonth array to JANUARY|FEBRUARY etc, ditto the dates. Then do the same for the conversion bit for numeric to aplha-numeric dates.

Or just add the bottom validation bits from '//check leap year onwards' to your own function.

Hope its of some use,

Cheers

Joe :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top