我编写了一个js文件,它使用YouTube API v3获取信息。我尝试过很多方法来争取时间去改变,但是没有什么效果。有人能向我解释一下如何将它更改为-月/日/年吗?
谢谢。
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);
})
}
);
}
});
发布于 2016-11-19 08:20:18
不能更改Youtube API结果的日期格式。您必须使用JS更改格式。您可以使用像Moment.js这样的外部插件来格式化日期。以下是纯粹的JS方式。
videoPublished = new Date(item.snippet.publishedAt);
formattedDate = (videoPublished .getMonth()+1) + '/' + videoPublished .getDate() + '/' + videoPublished .getFullYear();
使用formattedDate
显示
https://stackoverflow.com/questions/40690143
复制