Advertisement
Guest User

Untitled

a guest
May 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import React from 'react';
  2. import moment from 'moment';
  3.  
  4. class Countdown extends React.Component {
  5. state = {
  6. days: undefined,
  7. hours: undefined,
  8. minutes: undefined,
  9. seconds: undefined
  10. };
  11.  
  12. componentDidMount() {
  13. this.interval = setInterval(() => {
  14. const { timeTillDate, timeFormat } = this.props;
  15. const then = moment(timeTillDate, timeFormat);
  16. const now = moment();
  17. const countdown = moment(then - now);
  18. const days = countdown.format('D');
  19. const hours = countdown.format('HH');
  20. const minutes = countdown.format('mm');
  21. const seconds = countdown.format('ss');
  22. this.setState({ days, hours, minutes, seconds });
  23. }, 1000);
  24. }
  25.  
  26. componentWillUnmount() {
  27. if (this.interval) {
  28. clearInterval(this.interval);
  29. }
  30. }
  31.  
  32. render() {
  33. const { days, hours, minutes, seconds } = this.state;
  34.  
  35. return (
  36. <div>
  37. <h1>Countdown</h1>
  38. <div className="countdown-wrapper">
  39. <div className="countdown-item">
  40. {days}
  41. <span>days</span>
  42. </div>
  43. <div className="countdown-item">
  44. {hours}
  45. <span>hours</span>
  46. </div>
  47. <div className="countdown-item">
  48. {minutes}
  49. <span>minutes</span>
  50. </div>
  51. <div className="countdown-item">
  52. {seconds}
  53. <span>seconds</span>
  54. </div>
  55. </div>
  56. </div>
  57. );
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement