4 Replies Latest reply on Mar 10, 2010 10:16 PM by Harry Kunz

# Date and Time

hi all

In a Text field I want to display the Milliseconds in 2 digits only

example      05    :   30  :   23   :   00

Hour     Mins   Sec     Milliseconds

for this i just convert the milliseconds in toStrings

and using substr(0,2)

if we do like this it shows the milliseconds in 2 digits

example  05 : 30 : 23 : 01

but it shows in 1 digits if milliseconds was in 2 digits

example  05 : 30 : 23 : 1

how to clear this

am i doing right

• ###### 1. Re: Date and Time

Show all of the code you are using,  Also, what happens for 3 digit milliseconds since you only want to display 2 digits (milliseconds < 1 second will range from 0 thru 999)?

• ###### 2. Re: Date and Time

You can use a prepend-"0" routine before after it is converted to string assuming you already have the time in milliseconds stored in the variable nTime like this:

var sTime:String = nTime.toString();

while(sTime.length < 4) sTime = "0" + sTime;

sTime = sTime.substr(0,2);

This will return values 00 through 09 for millisecond values of 0000 to 0999. Notice that the fourth digit will always be zero because when it reaches 1000 it should be zero while the seconds variable is incremented. Since the first digit is always zero, i would suggest you better display values 0 through 9 instead of 00 through 09 by using:

var sTime:String = nTime.toString();

while(sTime.length < 3) sTime = "0" + sTime;

sTime = sTime.substr(0,1);

• ###### 3. Re: Date and Time

Hi Hurry

can we use any code directly without converting it into string

one of my friend told me it is unnecessary

is there any script for problem

• ###### 4. Re: Date and Time

The end result should always be a string since you are going to set a text field with a number. So you may drop the loop and the string method substr. Take note though that your values will only be 00 to 09 as explained above, so you could just do something as simple as this one liner:

txt.text = "0" + int(nTime * 0.01);

Or if you would like the values to be displayed from 000 to 999 you can use this

var sTime:String = nTime.toString();

if(nTime < 10) sTime = "00" + sTime;

else if(nTime < 100) sTime = "0" + sTime;

yourtextfield.text = sTime;

Or if you would like the values to be displayed with the extra zero from 0000 to 0999 (1000 becomes 0000 and seconds is incremented), you can prepened an extra zero to the last line:

yourtextfield.text = "0" + sTime;

Where nTime = millisecond value from 0 to 999. This is the the most efficient you can do.