Reputation: 37
I have written a js file that pulls info using YouTube API v3. I've tried many methods to get the time to change but nothing works. Can someone explain to me how to get it to change to- month/date/year?
Thank you.
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);
})
}
);
}
});
Upvotes: 0
Views: 869
Reputation: 8850
You can't change the dateformat of Youtube API results. You have to change the format using JS. You can use external plugins like Moment.js to format the date. Following is the pure JS way.
videoPublished = new Date(item.snippet.publishedAt);
formattedDate = (videoPublished .getMonth()+1) + '/' + videoPublished .getDate() + '/' + videoPublished .getFullYear();
Use formattedDate
to display
Upvotes: 1