Created
November 19, 2016 00:02
-
-
Save santisteban/f91a1336edbae72e40aff2d2ba0c537e to your computer and use it in GitHub Desktop.
Seconds-to-Duration Template Tag ported to JavaScript
This file contains 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
/** | |
* Seconds-to-Duration Template Tag ported to JavaScript | |
* @author Dan Ward 2009 (http://d-w.me) | |
* @author Nicolás Santisteban (https://ibk.rocks) | |
**/ | |
function secstodur(value, long=false) { | |
// Place seconds in to integer | |
var secs = parseInt(value); | |
// If seconds are greater than 0 | |
if (secs > 0) { | |
// Place durations of given units in to variables | |
var daySecs = 86400; | |
var hourSecs = 3600; | |
var minSecs = 60; | |
// If short string is enabled | |
if (!long) { | |
// Set short names | |
var dayUnitName = ' day'; | |
var hourUnitName = ' hr'; | |
var minUnitName = ' min'; | |
var secUnitName = ' sec'; | |
// Set short duration unit splitters | |
var lastDurSplitter = ' '; | |
var nextDurSplitter = lastDurSplitter; | |
// If short string is not provided or any other value | |
} else { | |
// Set long names | |
var dayUnitName = ' day'; | |
var hourUnitName = ' hour'; | |
var minUnitName = ' minute'; | |
var secUnitName = ' second'; | |
// Set long duration unit splitters | |
var lastDurSplitter = ' and '; | |
var nextDurSplitter = ', '; | |
} | |
// Create string to hold outout | |
var durationString = ''; | |
// Calculate number of days from seconds | |
var days = parseInt(Math.floor(secs / parseInt(daySecs))); | |
// Subtract days from seconds | |
var secs = secs - (days * parseInt(daySecs)); | |
// Calculate number of hours from seconds (minus number of days) | |
var hours = parseInt(Math.floor(secs / parseInt(hourSecs))); | |
// Subtract hours from seconds | |
var secs = secs - (hours * parseInt(hourSecs)); | |
// Calculate number of minutes from seconds (minus number of days and hours) | |
var minutes = parseInt(Math.floor(secs / parseInt(minSecs))); | |
// Subtract days from seconds | |
var secs = secs - (minutes * parseInt(minSecs)); | |
// Calculate number of seconds (minus days, hours and minutes) | |
var seconds = secs; | |
// If number of days is greater than 0 | |
if (days > 0) { | |
// Add multiple days to duration string | |
var durationString = durationString + ' ' + String(days) + dayUnitName + (days > 1 && 's' || ''); | |
} | |
// Determine if next string is to be shown | |
if (hours > 0) { | |
// If there are no more units after this | |
if (minutes <= 0 && seconds <= 0) { | |
// Set hour splitter to last | |
var hourSplitter = lastDurSplitter; | |
// If there are unit after this | |
} else { | |
// Set hour splitter to next | |
var hourSplitter = (durationString.length > 0 && nextDurSplitter || ''); | |
} | |
} | |
// If number of hours is greater than 0 | |
if (hours > 0) { | |
// Add multiple days to duration string | |
var durationString = durationString + hourSplitter + ' ' + String(hours) + hourUnitName + (hours > 1 && 's' || ''); | |
} | |
// Determine if next string is to be shown | |
if (minutes > 0) { | |
// If there are no more units after this | |
if (seconds <= 0) { | |
/// Set minute splitter to last | |
var minSplitter = lastDurSplitter; | |
// If there are unit after this | |
} else { | |
// Set minute splitter to next | |
var minSplitter = (durationString.length > 0 && nextDurSplitter || ''); | |
} | |
} | |
// If number of minutes is greater than 0 | |
if (minutes > 0) { | |
// Add multiple days to duration string | |
var durationString = durationString + minSplitter + ' ' + String(minutes) + minUnitName + (minutes > 1 && 's' || ''); | |
} | |
// Determine if next string is last | |
if (seconds > 0) { | |
// Set second splitter | |
var secSplitter = (durationString.length > 0 && lastDurSplitter || ''); | |
} | |
// If number of seconds is greater than 0 | |
if (seconds > 0) { | |
// Add multiple days to duration string | |
var durationString = durationString + secSplitter + ' ' + String(seconds) + secUnitName + (seconds > 1 && 's' || ''); | |
} | |
// Return duration string | |
return durationString.trim() | |
// If seconds are not greater than 0 | |
} else { | |
// Provide 'No duration' message | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment