我编写了一个使用YouTube API v3提取信息的js文件。我已经尝试了很多方法来获得改变的时间,但没有任何效果。有人可以向我解释如何让它改变到月/日/年?
谢谢。
var channelName = '2kolf';
var vidWidth = 166;
var vidHeight = 86;
var vidResults = 4;
$ (document).ready(function() {
$.get(
"https://www.googleapis.com/youtube/v3/channels",{
part: 'contentDetails',
forUsername: channelName,
key: ''},
function(data) {
$.each(data.items, function(i, item) {
console.log(item);
pid = item.contentDetails.relatedPlaylists.uploads;
getVids(pid);
})
}
);
function getVids(pid){
$.get(
"https://www.googleapis.com/youtube/v3/playlistItems",{
part: 'snippet',
maxResults: vidResults,
playlistId: pid,
key: ''},
function(data) {
var output;
$.each(data.items, function(i, item) {
console.log(item);
videoTitle = item.snippet.title.replace("Season 8 - ", "");
videoId = item.snippet.resourceId.videoId;
videoThumbnails = item.snippet.thumbnails.medium.url;
videoPublished = item.snippet.publishedAt;//This is to show the date published//
videoAuthor = item.snippet.channelTitle.replace("2kOLF", "2K Online Franchise");
videoStats = item.snippet.statistics;
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>';
//Append to results listStyleType
$('#results').append(output);
})
}
);
}
});
答案 0 :(得分:1)
您无法更改Youtube API结果的日期格式。您必须使用JS更改格式。您可以使用Moment.js之类的外部插件来格式化日期。以下是纯粹的JS方式。
videoPublished = new Date(item.snippet.publishedAt);
formattedDate = (videoPublished .getMonth()+1) + '/' + videoPublished .getDate() + '/' + videoPublished .getFullYear();
使用formattedDate
显示