2013-09-27 14 views
10

YouTube API süresi (biçim PT # M # S ISO 8601 süresi) dönüştürmek için? örneğinnasıl JavaScript ile biçim PT # M # S bir tarih saat manipüle nasıl saniye

: PT5M33S

Ben hh:mm:ss olarak çıktı istiyorum.

+1

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

+1

@Daugilas Kakaras Çok güzel! Ama işlevi güncellemeniz gerekiyor. P1D - (bir gün) işlevi gibi format çalışmazsa. Serhiog.Lazin @ –

+1

, gerçek - güncelleme: https://jsfiddle.net/kbeb0p99/4/ * oldukça uzun bir süre sonra :) –

cevap

19

İşte saniye ve diğer parçaların toplam sayısını almak için temel kod. Kural her zaman tarih mantığı istediğiniz yapmamalısın :) Ama neyse, işte gider söylediği gibi Ben, bunu yaparken huzursuz - Google getDuration oyuncu API toplam saniye sağlayan ve tamamen sağlayan, kolay değil yapılan gdata API'daki farklı format. İşte

  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

sayesinde tweaks biraz ile bunu da ihtiyacım olarak çalışmak yaptı. – Nicholas

+0

Süreyle ilgili: "PT26M" ' –

4

basit bir şekilde Youtube API (v3) aracılığıyla youtube video verilerini almak ve saniyeye video süresini (ISO 8601) dönüştürebilirsiniz nasıl. senin Video id ve kamu google anahtar için URL'ye {SİZİN VİDEO ID} ve {SİZİN KEY} niteliğini değiştirmek unutmayın. Google geliştirici konsoluna erişen bir genel anahtar oluşturabilirsiniz.

$.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

Süre hakkında:" PT26M "' –

0

Bu cevaplar teknik olarak doğru olsa da; zaman ve sürelerle çok şey yapmayı planlıyorsanız momentjs'u kontrol etmelisiniz. Ayrıca düzenli momentjs zaman

bu 2 modül çıktısı 05:33 bu

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

yapmak ne kadar kolay bir örneği kadar basit biçimlendirme süreler yapar moment-duration-formats göz atın. Ve başka birçok kullanım var. Bir neden var olsa bu bir standarttır, böylece akılda tutmak beri

youtube ISO8601 biçimi kullanır.

İlgili konular