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!

Hi all, I have some problem with

Status
Not open for further replies.

garfield11

IS-IT--Management
Jul 4, 2002
44
SG
Hi all,

I have some problem with dates so can anyone please help.

Below r codes to convert eg. 15/2/02 --> 15th Feb 2002:

function convDate(QDate)
{
var dayNum=new Array("1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th","10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th","31st")
var monName=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

var strMainString= QDate.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=day+" " +monName[test.getMonth()-1]+ " " +year;
QDate.value=output;
}

But now e problem is:
when I entered a date - day beyond 10 (eg. 10th --> 31th) n month beyond 10 (eg. 10 --> 12 that's Oct --> Dec) can convert

whereas

when i entered a date - day before 10 (eg. 1st --> 9th) n month before 10 (eg. 1 --> 9 that's Jan --> Sep) got error.. undefined undefined

BUT

if i type 01/01/02 (put a "0" in front)- can convert

so can anybody tell me what's wrong?

Thanks!

Love_Garfield
 
it's due to the following code :
Code:
   var year= strMainString.substring(6,10); //last four
   var month= strMainString.substring(3,5); //middle two
   var day= strMainString.substring(0,2); //first two
If you enter for exemple "1/2/2002",
day will contain "1/",
month will contain "/2",
year will contain ... heuu something strange...

may be you can use the "split" function. Water is not bad as soon as it stays out human body ;-)
 
What Targol means is this:

<script>
function convDate(QDate)
{
var 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;)
var monName=new Array(&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,&quot;Aug&quot;,&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot;)

var arrDate = new Array();
arrDate = QDate.split(&quot;/&quot;);

if(arrDate.length==3){
var test = new Date(parseInt(arrDate[2],10), parseInt(arrDate[1],10)-1, parseInt(arrDate[0],10));
}else{ // the datestring was not correct
return false;
}
if(test==&quot;NaN&quot;){ //string could not be converted to a date
return false;
}
return dayNum[test.getDate()-1] + &quot; &quot; + monName[test.getMonth()] + &quot; &quot; + parseInt(arrDate[2],10)
}
alert(convDate(&quot;27/02/2002&quot;))
alert(convDate(&quot;33/02/2002&quot;))
alert(convDate(&quot;q3/02/2002&quot;))
</script>

Or instead of an input box you use a popup calendar like thisone:


the calendar works only in IE but I have made a javascript version that works in mozilla and IE.
 
It's exaclty that, harmmeijer. Are you a medium ??? ;-) Water is not bad as soon as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top