Skip to content

Instantly share code, notes, and snippets.

@ethansocal
Created October 31, 2021 03:41
Show Gist options
  • Save ethansocal/ce6e4918ee8a6c01ace05ecc28e78f69 to your computer and use it in GitHub Desktop.
Save ethansocal/ce6e4918ee8a6c01ace05ecc28e78f69 to your computer and use it in GitHub Desktop.
function time_parser(input: string): number {
/**
* @name time_parser
* @description Parses a string of time into a number of seconds
* @param {string} input - String of time to parse
*
* @return {number} - Number of seconds
*/
let total_seconds = 0;
const times: { [index: string]: number } = {
"y": 365 * 24 * 60 * 60,
"w": 7 * 24 * 60 * 60,
"d": 24 * 60 * 60,
"h": 60 * 60,
"m": 60,
"s": 1
}
let current_time = "";
for (var i = 0; i < input.length; i++) {
if (times[input[i]] !== undefined) {
total_seconds += times[input[i]] * parseInt(current_time);
current_time = "";
} else {
current_time += input[i];
}
}
return total_seconds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment