2013-09-27 14 views
10

Come posso modificare una data nel formato PT # M # S con JavaScript?Come convertire la durata dell'API di YouTube (durata ISO 8601 nel formato PT # M # S) in secondi

ad esempio: PT5M33S

mi piacerebbe uscita come hh:mm:ss.

+1

Fiddle: https://jsfiddle.net/Daugilas/kbeb0p99/1/ –

+1

@Daugilas Kakaras Molto bello! Ma è necessario aggiornare la funzione. Quando il formato come P1D - (un giorno) la funzione non funziona. –

+1

@ Serhiog.Lazin, true - aggiornato: https://jsfiddle.net/kbeb0p99/4/ * dopo molto tempo :) –

risposta

19

Ecco il codice di base per ottenere il numero totale di secondi e altre parti. Mi sento a disagio nel farlo, poiché la regola dice che ogni volta che vuoi uscire con la logica non dovresti :) Ma comunque, eccola: Google non è stato facile, fornendo secondi totali nell'API del player getduration e fornendo totalmente formato diverso nella gdata api.

  var reptms = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/; 
      var hours = 0, minutes = 0, seconds = 0, totalseconds; 

      if (reptms.test(input)) { 
      var matches = reptms.exec(input); 
      if (matches[1]) hours = Number(matches[1]); 
      if (matches[2]) minutes = Number(matches[2]); 
      if (matches[3]) seconds = Number(matches[3]); 
      totalseconds = hours * 3600 + minutes * 60 + seconds; 
      } 
+0

grazie, con un po 'di modifiche ho fatto funzionare proprio come avevo bisogno. – Nicholas

+0

Cosa dire di 'durata:" PT26M "' –

4

Ecco come è possibile ottenere un insieme di dati video di YouTube tramite Youtube API (v3) in modo semplice e convertire la durata del video (ISO 8601) per secondi. Non dimenticare di cambiare il {TUO VIDEO ID} e {LA CHIAVE} attributo URL del video di id e la vostra chiavegoogle pubblico. Puoi creare una chiave pubblica per accedere alla console per gli sviluppatori di Google.

$.ajax({ 
     url: "https://www.googleapis.com/youtube/v3/videos?id={ YOUR VIDEO ID }&part=contentDetails&key={ YOUR KEY }", 
     dataType: "jsonp", 
     success: function (data) { youtubeCallback (data); } 
    }); 

    function youtubeCallback(data) { 

     var duration = data.items[0].contentDetails.duration; 
     alert (convertISO8601ToSeconds (duration)); 
    }   

    function convertISO8601ToSeconds(input) { 

     var reptms = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/; 
     var hours = 0, minutes = 0, seconds = 0, totalseconds; 

     if (reptms.test(input)) { 
      var matches = reptms.exec(input); 
      if (matches[1]) hours = Number(matches[1]); 
      if (matches[2]) minutes = Number(matches[2]); 
      if (matches[3]) seconds = Number(matches[3]); 
      totalseconds = hours * 3600 + minutes * 60 + seconds; 
     } 

     return (totalseconds); 
    } 
+0

Cosa dire di 'durata:" PT26M "' –

0

Mentre queste risposte sono tecnicamente corrette; dovresti controllare momentjs se pensi di fare molto con tempo e durata. Anche verificare moment-duration-formats che rende durate formattazione altrettanto semplici come momentjs regolare tempo

Un esempio di quanto facile questi 2 moduli rendono

moment.duration('PT5M33S').format('hh:mm:ss') 

che emetterà 05:33. E ci sono molti altri usi.

Sebbene ci sia un motivo per cui YouTube usa il formato ISO8601 poiché è uno standard, tieni questo in mente.

Problemi correlati