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

Zero Padding

Status
Not open for further replies.

thankgodfortektips

Programmer
Dec 20, 2005
95
KY
Hi All,

I now visual basic but not sure how to do this in javascript. I am trying to get a button that will take the current time and fill in a hour and minute field. I have figured most of it out, but when the hour or minutes is under 9 it returns 9 instead of 09.

So, what I need to do is if x is <9 then turn x into 09... sounds easy but cant figure it out. Below is what I have so far.
************************
var mydate= new Date()
var theyear=mydate.getFullYear()
var themonth=mydate.getMonth()+1
var thetoday=mydate.getDate()
var theHour=mydate.getHours()
var theMinute=mydate.getMinutes()

if (theHour<9){
theHour = "0" + theHour
}
************************
This above is giving errors...

Thanks in advance
 
What line does the error occur on? I use something like that regularly to format dates and it works fine.

I'd guess that later on you're trying to deal with 09 as a number, and that won't work. Numbers beginning with a zero are considered octal (base 8) in Javascript and most other languages with roots in C. Since there is no 8 or 9 in octal, 08 and 09 will create an error. If this is your real problem, you'll have to use parseInt(value, 10) to evaluate the variable value in base 10.

If you didn't copy and paste your code in here, please do that instead so we can see EXACTLY what the browser is working with.

Lee
 
Surely you want to prepend zero if it's less than or equal to nine... not just less than nine!

Post the code you have, inside
Code:
tags. Take the time to run it in Firefox (and check the console output) - install Firebug debugger (and learn how useful it is) if you have the time too.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top