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

Hello, I want to format date to

Status
Not open for further replies.

112055

Programmer
May 13, 2002
61
US
Hello,
I want to format date to 'MM/DD/YYYY', is there a way to automatically fill in leading zero for single digit date if user enter 1/2/03 to 01/02/2003.. ?

Thanks for your help.

vi
 
yy,mm,dd is the variable for year,month,year...

dt=new Date(yy,mm-1,dd)
alert(dt)

Known is handfull, Unknown is worldfull
 
i am trying to format a date as well but in the following format... yyyy-mm-dd. I would like the month and day to have a leading zero if less than 10. I tried the given solution but it doesn't work for me. Can anyone help out with a different solution? Thanks.
 
can i have ur code?

Known is handfull, Unknown is worldfull
 
<SCRIPT LANGUAGE = &quot;JavaScript&quot;>
function ValidDate(sDate)
{
var expression = /[0123]\d\/[01]\d\/\d{4}/;
return expression.test(sDate);
}
</script>
pass in sDate in string format

on error goto hell
 
var datetest = Date.parse(datetext);
nDate = new Date(datetest);
DateYear = nDate.getFullYear();
DateMonth = nDate.getMonth();
DateMonth = DateMonth + 1;
DateDay = nDate.getDate();

DateCompare = DateYear + &quot;-&quot; + DateMonth + &quot;-&quot; + DateDay

i would like datemonth and dateday to have a leading zero before i put it in datecompare.
 
cylly,

First cast both of your integer variables to strings :

strDateMonth = &quot;&quot; + DateMonth;

(I dunno if strDateMonth = (String) DateMonth would work, it works in java, dunno about javascript, just do it the above way to be safe)

Then check for the length of the strings, don't want to put a leading zero if it's 10, 11, or 12:

if (strDateMonth.length == 1) {

}

And inside the if block just use string concatenation to stick the zero on the front:

strDateMonth = &quot;0&quot; + strDateMonth;


that should do it

-kaht
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top