Created
April 22, 2019 09:11
-
-
Save oropesa/8a26a30b5d312da0f937cb28c6ad739c to your computer and use it in GitHub Desktop.
Functions to caste the youtube duration & youtube date to a common time & date string
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
/** | |
* Examples: | |
* - PT1H20M5S > 01:20:05 | |
* - PT1H5S > 01:00:05 | |
* - PT2H10M > 02:10:00 | |
* - PT2M10S > 00:02:10 | |
* - PT2H > 02:00:00 | |
* - PT2M > 00:02:00 | |
* - PT2S > 00:00:02 | |
* | |
* @param {string} duration | |
* @return {string} | |
*/ | |
function YTDuration2TimeString( duration ) { | |
let match = duration.match( /PT(\d+H)?(\d+M)?(\d+S)?/ ); | |
match = match.slice( 1 ).map( x => { if( x != null ) { return x.replace( /\D/, '' ); } } ); | |
const hours = ( parseInt( match[ 0 ] ) || 0 ); | |
const minutes = ( parseInt( match[ 1 ] ) || 0 ); | |
const seconds = ( parseInt( match[ 2 ] ) || 0 ); | |
return new Date( ( hours * 3600 + minutes * 60 + seconds ) * 1000 ).toISOString().substr( 11, 8 ); | |
} | |
/** | |
* Example: | |
* - Mon Apr 22 2019 10:07:50 GMT+0100 > 22/04/2019 10:07:50 | |
* | |
* @param {number} ytdate | |
* @return {string} | |
*/ | |
function YTDate2DateString( ytdate ) { | |
const f2digit = function( number ) { return isNaN( number *1 ) ? '??' : number *1 < 10 ? '0' + number : number +''; }; | |
const theDate = new Date( ytdate ); | |
let date = [ f2digit( theDate.getDate() ), f2digit( theDate.getMonth() + 1 ), f2digit( theDate.getFullYear() ) ].join( '/' ); | |
let time = [ f2digit( theDate.getHours() ), f2digit( theDate.getMinutes() ), f2digit( theDate.getSeconds() ) ].join( ':' ); | |
return `${date} ${time}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment