Created
May 17, 2023 16:27
-
-
Save rsadwick/99507fc3bd8b9ec34683aa0dc473575c to your computer and use it in GitHub Desktop.
javascript: get remaining time by seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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