Skip to content

Instantly share code, notes, and snippets.

@rsadwick
Created May 17, 2023 16:27
Show Gist options
  • Save rsadwick/99507fc3bd8b9ec34683aa0dc473575c to your computer and use it in GitHub Desktop.
Save rsadwick/99507fc3bd8b9ec34683aa0dc473575c to your computer and use it in GitHub Desktop.
javascript: get remaining time by seconds
function getTimeRemaining(seconds) {
const now = new Date();
// Add the specified number of seconds to the current time
now.setSeconds(now.getSeconds() + seconds);
const current = new Date();
let diffInMinutes = Math.floor((now - current) / 60000);
let hours = Math.floor(diffInMinutes / 60);
let minutes = diffInMinutes % 60;
// Construct the return string
let timeString = '';
if (hours > 0) {
timeString += hours + ' hours ';
}
if (minutes > 0) {
timeString += minutes + (minutes === 1 ? ' minute' : ' minutes');
}
return timeString.trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment