2016-11-19 69 views
0

我已經編寫了一個使用YouTube API v3來提取信息的js文件。我嘗試了很多方法來改變時間,但沒有任何工作。有人可以向我解釋如何讓它改變爲月/日/年?無法獲取YouTube視頻時間格式更改

謝謝。

var channelName = '2kolf'; 
 
var vidWidth = 166; 
 
var vidHeight = 86; 
 
var vidResults = 4; 
 

 
$ (document).ready(function() { 
 
\t $.get(
 
\t \t "https://www.googleapis.com/youtube/v3/channels",{ 
 
\t \t \t part: 'contentDetails', 
 
\t \t \t forUsername: channelName, 
 
\t \t \t key: ''}, 
 
\t \t  function(data) { 
 
\t \t \t \t $.each(data.items, function(i, item) { 
 
\t \t \t \t \t console.log(item); 
 
\t \t \t \t \t pid = item.contentDetails.relatedPlaylists.uploads; 
 
\t \t \t \t \t getVids(pid); 
 
\t \t \t \t }) 
 
\t \t \t } 
 
\t); 
 

 
\t function getVids(pid){ 
 
\t \t $.get(
 
\t \t "https://www.googleapis.com/youtube/v3/playlistItems",{ 
 
\t \t \t part: 'snippet', 
 
\t \t \t maxResults: vidResults, 
 
\t \t \t playlistId: pid, 
 
\t \t \t key: ''}, 
 
\t \t \t function(data) { 
 
\t \t \t \t var output; 
 
\t \t \t \t $.each(data.items, function(i, item) { 
 
\t \t \t \t \t console.log(item); 
 
        videoTitle = item.snippet.title.replace("Season 8 - ", ""); 
 
\t \t \t \t \t videoId = item.snippet.resourceId.videoId; 
 
\t \t \t \t \t videoThumbnails = item.snippet.thumbnails.medium.url; 
 
\t \t \t \t \t videoPublished = item.snippet.publishedAt;//This is to show the date published// 
 
\t \t \t \t \t videoAuthor = item.snippet.channelTitle.replace("2kOLF", "2K Online Franchise"); 
 
\t \t \t \t \t videoStats = item.snippet.statistics; 
 
\t \t \t \t \t \t \t \t \t \t 
 
output = '<li class="YouTubeVideos"><a class="various fancybox.iframe" title="'+videoTitle+'" href=\"//www.youtube.com/embed/'+videoId+'\?fs=1&autoplay=1"><img height="'+vidHeight+'" width="'+vidWidth+'" src='+videoThumbnails+' ></a><br><p>'+videoTitle+'</p><br><span style="font-weight: normal;">by '+videoAuthor+'<br>'+videoPublished+'</span></li>'; 
 
\t \t \t \t \t 
 
\t \t \t \t \t //Append to results listStyleType 
 
\t \t \t \t \t $('#results').append(output); 
 
\t \t \t \t }) 
 
\t \t \t } 
 
\t); 
 
\t } 
 
});

回答

0

你不能改變的的Youtube API的結果日期格式。你必須改變使用JS的格式。您可以使用外部插件如Moment.js來格式化日期。以下是純JS方式。

videoPublished = new Date(item.snippet.publishedAt); 
formattedDate = (videoPublished .getMonth()+1) + '/' + videoPublished .getDate() + '/' + videoPublished .getFullYear(); 

使用formattedDate顯示

+0

謝謝您的答覆。我改變了代碼並添加了你提到的內容。但是,我收到一個錯誤。 'ReferenceError:mydate未定義 – ssx95351