Youtube VideoPreview format publication date with javascript-time-ago
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import {Image} from 'semantic-ui-react'; | |
import './VideoPreview.scss'; | |
import TimeAgo from 'javascript-time-ago'; | |
import en from 'javascript-time-ago/locale/en'; | |
TimeAgo.locale(en); | |
const timeAgo = new TimeAgo('en-US'); | |
export class VideoPreview extends React.Component { | |
render() { | |
const {video} = this.props; | |
if (!video) { | |
return <div/>; | |
} | |
const viewAndTimeString = VideoPreview.getFormattedViewAndTime(video); | |
const horizontal = this.props.horizontal ? 'horizontal': null; | |
return ( | |
{/* ... rest unchanged*/} | |
<div className='video-preview-metadata-container'> | |
<div className='channel-title'>{video.snippet.channelTitle}</div> | |
<div><span>{viewAndTimeString}</span></div> | |
{/* ... rest unchanged*/} | |
); | |
} | |
static getFormattedViewAndTime(video) { | |
const publicationDate = new Date(video.snippet.publishedAt); | |
return `${video.statistics.viewCount} views • ${timeAgo.format(publicationDate)}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment