Skip to content

Instantly share code, notes, and snippets.

@ohaal
Last active June 16, 2016 09:06
Show Gist options
  • Save ohaal/435bf677230d609afa66f24706c6af91 to your computer and use it in GitHub Desktop.
Save ohaal/435bf677230d609afa66f24706c6af91 to your computer and use it in GitHub Desktop.
generate countdown string to a timestamp
var cdStamp = 1472301000000;
var cdDuration = cdStamp - (new Date().getTime());
cdDuration = cdDuration > 0 ? cdDuration : 0;
var cdTimes = [ 7, 24, 60, 60, 1000];
var cdUnit = ['uke', 'dag', 'time', 'minutt', 'sekund'];
var cdUnitEnd = [ 'r', 'er', 'r', 'er', 'er'];
var cdResult = cdTimes
.map((t, i, timeArr) => {
var unitMulti = timeArr.slice(i, timeArr.length).reduce((a,b) => a*b);
var timeSubtract = 0;
if (i > 0) {
var unitMultiPrev = timeArr.slice(i-1, timeArr.length).reduce((a,b) => a*b);
timeSubtract = timeArr[i-1] * Math.floor(cdDuration / unitMultiPrev);
}
return Math.floor(cdDuration / unitMulti) - timeSubtract;
})
.map((t, i) => {
return t + ' ' + cdUnit[i] + (t > 1 ? cdUnitEnd[i] : '');
})
.filter(t => /^[^0]/.test(t))
.join(' ');
console.log(cdResult);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment