Skip to content

Instantly share code, notes, and snippets.

@FerJSsilva
Created July 17, 2018 17:07
Show Gist options
  • Save FerJSsilva/7752487f6939e22d81601bc5307297de to your computer and use it in GitHub Desktop.
Save FerJSsilva/7752487f6939e22d81601bc5307297de to your computer and use it in GitHub Desktop.
A simple React Clock using moment.js
import React, { Component } from 'react';
import moment from 'moment';
class Clock extends Component {
state = {
dateTime: moment.utc().format('ddd DD MMM HH:mm:ss'),
}
componentDidMount() {
this.timerId = setInterval(this.tick, 1000);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick = () => {
this.setState({
dateTime: moment.utc().format('ddd DD MMM HH:mm:ss'),
});
}
render() {
const { dateTime } = this.state;
return (
<span>{dateTime}</span>
);
}
}
export default Clock;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment