Date function help needed

I have the function below that retrieves today's date, problem is when I display it on a field it is formated as follow:

1/1/2008

I need it to be formatted as: 01/01/2008

Function:

<% function todayStr() {
var today=new Date()
return today.getMonth()+1+"/"+today.getDate()+"/"+(today.getYear())
}
%>
LVL 1
AleksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
I don't know JavaScript, or if it has a Right function, but try changing this:
return today.getMonth()+1+"/"+today.getDate()+"/"+(today.getYear())

to this:
return righ("00" + today.getMonth()+1, 2) +"/"+ Right("00" + today.getDate(), 2) +"/"+ (today.getYear())

Regards,

Rob.
0
ahoffmannCommented:
var month=(new Date().getMonth()+"").replace(/^(\d)$/,"0$1");
var day=(new Date().getDay()+"").replace(/^(\d)$/,"0$1");
0
Shanmuga SundaramDirector of Software EngineeringCommented:
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

AleksAuthor Commented:
I tried this but it gives me an error, says an object is expected:

<% function todayStr() {
var today=new Date()
return righ("00" + today.getMonth()+1, 2) +"/"+ Right("00" + today.getDate(), 2) +"/"+ (today.getYear())
}
%>

0
ahoffmannCommented:
are you talking about PHP or JavaScript code?
0
AleksAuthor Commented:
the javascript, and I use ASP.

I tried this but it gives me an error, says an object is expected:

<% function todayStr() {
var today=new Date()
return righ("00" + today.getMonth()+1, 2) +"/"+ Right("00" + today.getDate(), 2) +"/"+ (today.getYear())
}
%>
0
ahoffmannCommented:
how about testing what was suggested? http:#20795121
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobSampsonCommented:
Sorry, I missed a "t" on the first Right:
This:
return righ("00" + today.getMonth()+1, 2) +"/"+ Right("00" + today.getDate(), 2) +"/"+ (today.getYear())

should be
return Right("00" + today.getMonth()+1, 2) +"/"+ Right("00" + today.getDate(), 2) +"/"+ (today.getYear())

Regards,

Rob.
0
RobSampsonCommented:
Sorry, I missed a "t" on the first Right:
This:
return righ("00" + today.getMonth()+1, 2) +"/"+ Right("00" + today.getDate(), 2) +"/"+ (today.getYear())

should be
return Right("00" + today.getMonth()+1, 2) +"/"+ Right("00" + today.getDate(), 2) +"/"+ (today.getYear())

Regards,

Rob.
0
AleksAuthor Commented:
Technical Information (for support personnel)

Error Type:
Microsoft JScript runtime (0x800A138F)
Object expected
/BlueDot/Intranet/Shipping/index.asp, line 125

same error, line 125 is:

return Right("00" + today.getMonth()+1, 2) +"/"+ Right("00" + today.getDate(), 2) +"/"+ (today.getYear())
0
RobSampsonCommented:
Hmm, as I said, I'm not familiar with JScript, but a look through the JScript Language Reference suggests that this may work....

strZero = "0";
strMonth = strZeros.concat(today.getMonth() + 1);
if (strMonth.length = 3)
      strMonth = strMonth.substr(2, 2);
strDay = strZeros.concat(today.getDate() + 1);
if (strDay.length = 3)
      strDay = strMonth.substr(2, 2);

return strMonth +"/"+ strDay +"/"+ today.getYear();

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.