Skip to content

Instantly share code, notes, and snippets.

@Fraasi
Created September 29, 2017 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fraasi/d8dfe3eb49b587636329ddb8d55e6738 to your computer and use it in GitHub Desktop.
Save Fraasi/d8dfe3eb49b587636329ddb8d55e6738 to your computer and use it in GitHub Desktop.
Simple timer to run my bot once everyday
function msToTime(duration) {
// from https://stackoverflow.com/questions/19700283/how-to-convert-time-milliseconds-to-hours-min-sec-format-in-javascript
var milliseconds = parseInt((duration%1000)/100)
, seconds = parseInt((duration/1000)%60)
, minutes = parseInt((duration/(1000*60))%60)
, hours = parseInt((duration/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}
function msUntillCodeRuns() {
var now = new Date();
var then = new Date(now);
then.setHours(23, 0, 0, 0); //when to run
if (then - now <= 0) then.setDate(now.getDate() + 1);
var msToRunTime = then - now;
console.log(`${msToTime(msToRunTime)} to runtime.`);
return msToRunTime;
}
function codeToRun() {
console.log("Code runs...")
setTimeout(codeToRun, msUntillCodeRuns());
}
setTimeout(codeToRun, msUntillCodeRuns());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment