Created
April 13, 2015 20:56
-
-
Save denniszhao/8972cd4ae637cf10fe01 to your computer and use it in GitHub Desktop.
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
/** | |
* Converts YouTube formatted duration to seconds. | |
* | |
* Expects input String of the form "PT#M#S", in which the letters PT indicate | |
* that the value specifies a period of time, and the letters M and S refer to | |
* length in minutes and seconds, respectively. The # characters preceding the | |
* M and S letters are both integers that specify the number of minutes (or | |
* seconds) of the video. For example, a value of PT15M51S indicates that the | |
* video is 15 minutes and 51 seconds long. | |
*/ | |
var convertTimeToSeconds = function(time) { | |
var a = time.match(/\d+H|\d+M|\d+S/g), | |
result = 0; | |
var d = { 'H': 3600, 'M': 60, 'S': 1 }, | |
num, | |
type; | |
for (var i = 0; i < a.length; i++) { | |
num = a[i].slice(0, a[i].length - 1); | |
type = a[i].slice(a[i].length - 1, a[i].length); | |
result += parseInt(num) * d[type]; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment