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())
}
%>
AleksAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ahoffmannConnect With a Mentor Commented:
how about testing what was suggested? http:#20795121
0
 
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Shanmuga SundaramDirector of Software EngineeringCommented:
0
 
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
 
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
 
RobSampsonConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.