【问题标题】:Can't get YouTube video time format to change无法更改 YouTube 视频时间格式
【发布时间】:2016-11-19 07:02:36
【问题描述】:

我编写了一个使用 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);
				})
			}
	);
	}
});

【问题讨论】:

    标签: javascript youtube youtube-data-api


    【解决方案1】:

    您无法更改 Youtube API 结果的日期格式。您必须使用 JS 更改格式。您可以使用Moment.js 等外部插件来格式化日期。以下是纯JS方式。

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

    使用formattedDate显示

    【讨论】:

    • 感谢您的回复。我更改了代码并添加了您提到的内容。但是,我得到一个错误。 ReferenceError: mydate is not defined
    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 2014-12-03
    • 1970-01-01
    • 2018-03-28
    • 2013-06-27
    • 2017-06-18
    • 2011-04-01
    • 1970-01-01
    相关资源
    最近更新 更多