2
\$\begingroup\$

I'm creating an update function to show my users when the next update will be available. I'm using the jQuery countdown plugin to show the time. What is a more efficient way to write this?

today = new Date();
todayDay = today.getDate();
todayYear = today.getFullYear();
todayMonth = today.getMonth();
todayMonth ++;
var countTo = "";
// $('#countdown').countdown('12:00:00');
var morningUpdate = new Date();
morningUpdate.setHours(10,0,0,0);

var middayUpdate = new Date();
middayUpdate.setHours(14,0,0,0);

var afternoonUpdate = new Date();
afternoonUpdate.setHours(17,0,0,0);

if(today < afternoonUpdate) {
 countTo = "17:00:00";
}

if(today < middayUpdate) {
    countTo = "14:00:00";
}

if(today < morningUpdate) {
    countTo = "10:00:00";
}

todayString = todayYear + "/" + todayMonth + "/" + todayDay;
console.log(todayString);
var countDownTo = todayString + " " + countTo;
console.log(countDownTo);
console.log("2015/5/13 12:00:00");

$('#clock').countdown(countDownTo, function(event) {
    var totalHours = event.offset.totalDays * 24 + event.offset.hours;
    $(this).html(event.strftime(totalHours + ' hr %M min %S sec'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/hilios/jQuery.countdown/v2.0.4/src/countdown.js"></script>
<div id="clock"></div>

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You don't need to create Date object for each day time case (morning/midday/afternoon) since you don't actually use any date component except hour. So you only need to extract current hour from today and define range it belong to.

var today = new Date();
var hour = today.getHours();
hour = hour < 10? 10 : hour < 14? 14 : hour < 17? 17 : 0;
var countDownTo = [today.getFullYear(), today.getMonth()+1, today.getDate()].join('/')+ (hour? hour+':00:00' : '');
...
\$\endgroup\$
0
1
\$\begingroup\$

This doesn't really address your efficiency concern, but I want to bring it up as a potential behavioral problem with your code.

Unless the "update" is going to be performed independently in each timezone at 10:00am, 12:00pm, and 5:00pm, you may want the countdown to be based on UTC time, rather than based on the local time.

For example, if the update is going to occur for everyone at 10:00am United States Eastern time (UTC -5), you'll want the counter to measure the time until 5:00am UTC time (or 4:00am UTC time if the US east coast is observing daylight savings time).

All of the Date functions that get or set time values have an equivalent UTC function (e.g. .getUTCHours(), .setUTCMonth()).

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for your clear answer Thriggle. For now my dashboard will only be available in The Netherlands so maybe I will implement this is in a later stage. \$\endgroup\$
    – Rover
    Commented May 15, 2015 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.