import React, { Component } from 'react'; import moment from 'moment'; interface Props { initialTime: number; } interface State { timeDisplay: string; } class CountDown extends Component { public state = { timeDisplay: '' }; public componentDidMount() { this.startCountDown(); } public render() { return

{this.state.timeDisplay}

; } private startCountDown = () => { const time = moment(this.props.initialTime); let intervalId: number; const setTimeDisplay = () => { const diff = moment.duration(time.diff(moment())); let timeDisplay; if (diff) { const pieces = [ diff.days() > 0 && `${diff.days()} days`, diff.hours() > 0 && `${diff.hours()} hours`, diff.minutes() > 0 && `${diff.minutes()} minutes`, diff.seconds() > 0 && `${diff.seconds()} seconds` ].filter(piece => !!piece); timeDisplay = `in ${pieces.join(', ')}`; } else { clearInterval(intervalId); timeDisplay = 'Auction is over!'; } this.setState({ timeDisplay }); }; intervalId = window.setInterval(setTimeDisplay, 1000); setTimeDisplay(); }; } interface ITime { text: string; time: number; } const ENSTime: React.SFC = ({ text, time }) => (

{text}

{moment(time).format('LLLL')}

); export default ENSTime;