Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Create A Countdown Time That Resets At A Specific Time

Let me first say I do not have a deep understanding of javascript but I know how to work my way around enough to write small scripts for pages. A client of mine needs me to do the following for a website:

  1. Find the user's local time on their computer.
  2. Take that local time and subtract it from 6pm.
  3. Display that time in a countdown or just a statement letting the user know how much time is left for same day shipping.
  4. After 6pm the time resets or disappears until the next business day.

So far I've been able to create the logic for getting the time from the local computer. I thought I'd be able to use datejs but it does not calculate hours in a day.

Here is the current code I have:

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

var suffix = "AM";
if (hours >= 12) 
{
  suffix = "PM";
  hours = hours - 12;
 }

var suffix = "AM";

if (hours >= 12) 
{
  suffix = "PM"; 
  hours = hours - 12;
}

if (hours == 0) 
{
  hours = 12;
}

if (minutes < 10)
  minutes = "0" + minutes;

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");
like image 331
Nina Morena Avatar asked Oct 18 '25 10:10

Nina Morena


2 Answers

How about this:

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
  }


if (minutes < 10)
minutes = "0" + minutes

if (suffix == "PM" && hours >= 6)
    {
 document.write("You're too late for next day shipping!");
}
    else
    {
    var hoursLeft = 5 - hours;
var minsLeft = 60 - minutes;
    document.write("<b> You've got " + hoursLeft  + " hours and " + minsLeft + " minutes left to qualify for next day shipping! </b>")
    }
like image 80
Giovanni B Avatar answered Oct 20 '25 00:10

Giovanni B


if this site would let me comment on other people's answers I'd give the credit for this to Giovanni, but since I can't yet comment on other people's work, here's what needs to change.

var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

var suffix = "AM";

if (minutes < 10)
minutes = "0" + minutes

if (hours >= 18)
{
 document.write("You're too late for next day shipping!");
}
else
{
var hoursLeft = 17 - hours;
var minsLeft = 60 - minutes;
if(minsLeft==60){
    minsLeft=0;
    hoursLeft++;
}

document.write("<b> You've got " + hoursLeft  + " and " + minsLeft + " minutes left to qualify for next day shipping! </b>")
}

The reason for this is that people who are ordering at 5AM might see think that they have to submit within the next hour for their shipping to be next day when in fact they have the next 13 hours.

EDIT: saw your timezone concern and here is a post that might interest you.

EDIT 2: posted the wrong link. The correct one should be up now, though it might be a bit of a dated answer.

like image 26
Scott M Avatar answered Oct 19 '25 23:10

Scott M