# Adding milisseconds to a countdown

**URL:** <https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705>\
**Category:** flash\
**Created:** [May 30, 2008, 1:45am UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705 "2008-05-30T01:45:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuugo](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Shuugo](https://forum.kirupa.com/u/Shuugo)\
**Post date:** [May 30, 2008, 1:45am UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/1 "2008-05-30T01:45:10Z")

</div>

[SIZE=2]Hello. I used to come to Kirupa before O.o but never realized it had a forum. The thing is: I need help adding “milisseconds” to a countdown where I currently have “days”, “hours”, “minutes” and “seconds”. The intention here is to remove the “days” and add “milisseconds”. I got the (flawed) code from a tutorial, fixed it here and there, and somehow it works now, but I am a newcomer to Flash and I understand absolutely nothing of ActionScript, so adding the milisseconds is way beyond what I can do now \>\_\<.

Here’s the code:  
[/SIZE]

```auto
[SIZE=2]this.onEnterFrame = function() {
var today:Date = new Date();
var currentYear = today.getFullYear();
var currentTime = today.getTime();

var targetDate:Date = new Date(currentYear,5,1);
var targetTime = targetDate.getTime();

var timeLeft = targetTime - currentTime;

var sec = Math.floor (timeLeft/1000);
var min = Math.floor (sec/60);
var hrs = Math.floor (min/60);
var days = Math.floor (hrs/24);
sec = string(sec % 60);
if (sec.length < 2) {
sec = "0" + sec;
}
min = string(min % 60)
if (min.length < 2) {
min = "0" + min;
}
hrs = string(hrs % 24)
if (hrs.length < 2) {
hrs = "0" + hrs;
}
days = string(days);

var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
countdown_txt.text = counter;
}
[/SIZE]

```

[SIZE=2]  
[/SIZE] [SIZE=2]  
[/SIZE]  
[SIZE=2]

I really need help with this \>\_\< and it is kind of urgent.

Thanks a LOT in advance to anyone who replies here, any help is appreciated.  
[/SIZE]

---

<div class="post-metadata">

**Author:** ![rbnzdave](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@rbnzdave](https://forum.kirupa.com/u/rbnzdave)\
**Post date:** [May 30, 2008, 2:08am UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/2 "2008-05-30T02:08:19Z")

</div>

here. ( well its modified to correctly get the milliseconds, however the function name will be invalid in as3.

```auto

this.onEnterFrame = function() {
var today:Date = new Date();
var currentYear = today.getFullYear();
var currentTime = today.getTime();

var targetDate:Date = new Date(currentYear,5,1);
var targetTime = targetDate.getTime();

var timeLeft = targetTime - currentTime;

var mSec = Math.floor (timeLeft);
var sec = Math.floor (timeLeft/1000);
var min = Math.floor (sec/60);
var hrs = Math.floor (min/60);
//var days = Math.floor (hrs/24);
mSec = String(mSec %1000);
if (mSec.length < 2) {
    mSec = "00" + sec;
    
} else if (mSec.length < 3) {
    mSec = "0" + sec;
    
}
sec = String(sec % 60);
if (sec.length < 2) {
sec = "0" + sec;
}
min = String(min % 60)
if (min.length < 2) {
min = "0" + min;
}
hrs = String(hrs % 24)
if (hrs.length < 2) {
hrs = "0" + hrs;
}

var counter:String = hrs + ":" + min + ":" + sec + ":" + mSec;
countdown_txt.text = counter;
}

```

---

<div class="post-metadata">

**Author:** ![Shuugo](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Shuugo](https://forum.kirupa.com/u/Shuugo)\
**Post date:** [May 30, 2008, 3:06am UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/3 "2008-05-30T03:06:09Z")

</div>

Thanks a lot. It’s getting the milisseconds now, however, some problems arised:

1. The counter quickly corrects the value to a base 60 number, but you can see it making the correction (it fastly displays the numbers 9,8,7 and then slows down and keeps counting)

2. The milisseconds counter has 3 numbers, I need it to have 2 (forgot to mention it, my apologies \>\_\<)

3. It’s not getting the time correctly @\_@

Can anyone please provide me help with those new issues? Thanks a lot for replying and helping me, rbnzdave ^^

---

<div class="post-metadata">

**Author:** ![rbnzdave](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@rbnzdave](https://forum.kirupa.com/u/rbnzdave)\
**Post date:** [May 30, 2008, 3:28am UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/4 "2008-05-30T03:28:24Z")

</div>

time is correct for me. 31 days this month. days:1 hrs:8 mins:33 sec:12 msec:14

have changed the resolution on the mSec ( added days to check the time reading )

```auto

var today:Date = new Date();
var currentYear = today.getFullYear();
var currentTime = today.getTime();

var targetDate:Date = new Date(currentYear,5,1);
var targetTime = targetDate.getTime();

var timeLeft = targetTime - currentTime;

var mSec = Math.floor (timeLeft);
var sec = Math.floor (timeLeft/1000);
var min = Math.floor (sec/60);
var hrs = Math.floor (min/60);
var days = Math.floor (hrs/24);
mSec = String(mSec %100);
if (mSec.length < 2) {
    mSec = "0" + sec;
    
} 
sec = String(sec % 60);
if (sec.length < 2) {
sec = "0" + sec;
}
min = String(min % 60)
if (min.length < 2) {
min = "0" + min;
}
hrs = String(hrs % 24)
if (hrs.length < 2) {
hrs = "0" + hrs;
}
days = String(days);

var counter:String = days + ":" + hrs + ":" + min + ":" + sec + ":" + mSec;
trace(counter);

```

---

<div class="post-metadata">

**Author:** ![rbnzdave](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@rbnzdave](https://forum.kirupa.com/u/rbnzdave)\
**Post date:** [May 30, 2008, 3:28am UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/5 "2008-05-30T03:28:57Z")

</div>

oh by the way, you do know your script counts down to the 1st June…

---

<div class="post-metadata">

**Author:** ![Shuugo](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Shuugo](https://forum.kirupa.com/u/Shuugo)\
**Post date:** [May 31, 2008, 8:57pm UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/6 "2008-05-31T20:57:22Z")

</div>

With those new changes, now the script does not count \>\_\<. In my output window, I get a value which I don’t really understand (might be the time left for the day to come), but the counting doesn’t start, I just get an empty box.

I’ll post a screenshot: [http://img135.imageshack.us/img135/1749/countgx0.jpg](http://img135.imageshack.us/img135/1749/countgx0.jpg)

Sorry for not replying earlier \>_\< I had a busy day yesterday, didn’t even manage to get on the PC \>_\<

If possible, keep helping me \>\_\< I have to get this done for tomorrow night at most T\_\_\_T and I’d like it to be perfect, which is near to happen thanks to the help I’ve been getting. Thanks a lot and keep it up, please ^^!

---

<div class="post-metadata">

**Author:** ![Shuugo](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Shuugo](https://forum.kirupa.com/u/Shuugo)\
**Post date:** [May 31, 2008, 9:10pm UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/7 "2008-05-31T21:10:11Z")

</div>

Oh, on some other forum, people gave me this code, which looks a lot like the one rbnzdave gave me before, but it might be useful, I don’t know \>\< (I’ve made little adjustments since it was crashing for some reason @\_@):

```auto

this.onEnterFrame = function() {
    var today:Date = new Date();
    var currentYear = today.getFullYear();
    var currentTime = today.getTime();

    var targetDate:Date = new Date(currentYear, 5, 1);
    var targetTime = targetDate.getTime();

    var timeLeft = targetTime-currentTime;
    var mili = (timeLeft);
    var sec = Math.floor(timeLeft/1000);
    var min = Math.floor(sec/60);
    var hrs = Math.floor(min/60);
    
    sec = (sec % 60);
    mili = (mili % 600);

    if (sec.length < 2) {
        sec = "0"+sec;
    }
    min = (min%60);
    if (min.length < 2) {
        min = "0"+min;
    }
    hrs = (hrs%24);
    if (hrs.length < 2) {
        hrs = "0"+hrs;
    }

    var counter:String = hrs+":"+min+":"+sec+":"+mili;
    countdown_txt.text = counter;
};

```

It works, it counts everything, BUT, if the hours value is not 10,11 or 12, it displays as a single number, like “X”, instead of “0X”. I wanted the decimal for the hours value to always be displayed \>\_\<. Also, the milisseconds has 3 numbers, instead of 2, which is the goal here.

Just decided to leave this code here for anyone to see because it might help solving the problems somehow @\_@. Also, this one does not have days, so I assume the time checking is wrong (tested it and it seemed wrong indeed)

And answering what rbnzdave asked before, yes, I know about the 0-11 things on the month, so 5 = June indeed ^^ but thanks for warning me.

Well \>_\< I’m really depending on you guys help ^^ so please, lend me a hand on this one if it’s in your power \>_\< there’s no way I can accomplish this by myself.

Thanks a lot in advance ^^.

---

<div class="post-metadata">

**Author:** ![Shuugo](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Shuugo](https://forum.kirupa.com/u/Shuugo)\
**Post date:** [June 1, 2008, 8:06am UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/8 "2008-06-01T08:06:42Z")

</div>

I’m running out of time T\_T someone please, save my day~

---

<div class="post-metadata">

**Author:** ![Shuugo](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@Shuugo](https://forum.kirupa.com/u/Shuugo)\
**Post date:** [June 2, 2008, 3:45am UTC](https://forum.kirupa.com/t/adding-milisseconds-to-a-countdown/261705/9 "2008-06-02T03:45:35Z")

</div>

Solved it with a different way 8D thanks for the help ^^!
