Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Countdown script displayed in 48 hour format?

Featured Replies

Hi all,

 

I have a countdown script. It works great, except I would like it to display in a 48 hour format instead of a 24 hour format; It will only ever be a 48 hour countdown.

 

Eg '47 hours 8 mins 2 secs remaining', rather than '1 day 24 hours 8 mins 2 secs remaining'.

 

Any help would be very appreciated. :)

 

 

/*usage*/
<script type="text/javascript">
 TargetDate = "03/24/2011 11:59 PM";
 CountActive = true;
 CountStepper = -1;
 LeadingZero = true;
 DisplayFormat = "%%H%% %%M%% %%S%%";
 FinishMessage = "Who Pays Full Price Is Ready For Launch!";
</script>




//countdown functionality

function calcage(secs, num1, num2) {
 s = ((Math.floor(secs/num1))%num2).toString();
 if (LeadingZero && s.length < 2)
   s = "0" + s;
 return "<span>" + s + "</span>";
}

function CountBack(secs) {
 if (secs < 0) {
   document.getElementById("countdown").innerHTML = FinishMessage;
   return;
 }
 DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
 DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
 DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
 DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

 document.getElementById("countdown").innerHTML = DisplayStr;
 if (CountActive)
   setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}

function putspan(backcolor, forecolor) {
document.write("<span id='countdown'></span>");
}

if (typeof(BackColor)=="undefined")
 BackColor = "white";
if (typeof(ForeColor)=="undefined")
 ForeColor= "black";
if (typeof(TargetDate)=="undefined")
 TargetDate = "12/31/2020 5:00 AM";
if (typeof(DisplayFormat)=="undefined")
 DisplayFormat = "%%D%% %%H%% %%M%% %%S%%";
if (typeof(CountActive)=="undefined")
 CountActive = true;
if (typeof(FinishMessage)=="undefined")
 FinishMessage = "";
if (typeof(CountStepper)!="number")
 CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
 LeadingZero = true;


CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
 CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
putspan(BackColor, ForeColor);
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
 ddiff = new Date(dnow-dthen);
else
 ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);

**EDIT**

 

Oops - should of read thread properly!!

Edited by Bomb

Try changing this line:

DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));

 

To:

DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,7200,48));

  • Author

Thanks guys,

 

Spitfire - I done this, but i'm still not getting 48 hours?

 

It looks like the script isn't 'allowing' the hour to be counted over 24 hours?

 

Tony

Thanks guys,

 

Spitfire - I done this, but i'm still not getting 48 hours?

 

It looks like the script isn't 'allowing' the hour to be counted over 24 hours?

 

Tony

 

Sorry, I think I got that wrong. It should be:

DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,48));

 

The way I told you to do it before meant that hours were 120 mins long! :huh:

  • Author

Sorry, I think I got that wrong. It should be:

DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,48));

 

The way I told you to do it before meant that hours were 120 mins long! :huh:

 

Haha, thank you ever so much - all working now.

 

I have also learnt something new! :D

 

Tony

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.