Advertisement
pestiand82

Óra

Apr 18th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useState} from 'react';
  2. import './Clock_style.css'
  3.  
  4. function Clock(props) {
  5.     const [time, set_time] = useState('');
  6.  
  7.     const showTime = () =>{
  8.         let date = new Date();
  9.         let h = date.getHours(); // 0 - 23
  10.         let m = date.getMinutes(); // 0 - 59
  11.         let s = date.getSeconds(); // 0 - 59
  12.         let session = "AM";
  13.  
  14.         if(h === 0){
  15.             h = 12;
  16.         }
  17.  
  18.         if(h > 12){
  19.             h = h - 12;
  20.             session = "PM";
  21.         }
  22.  
  23.         h = (h < 10) ? "0" + h : h;
  24.         m = (m < 10) ? "0" + m : m;
  25.         s = (s < 10) ? "0" + s : s;
  26.         let time_string = h + ":" + m + ":" + s + " " + session;
  27.         set_time(time_string)
  28.     };
  29.  
  30.     setInterval(() => showTime(), 1000);
  31.  
  32.     return (
  33.         <div className="clock_box">
  34.             {time}
  35.         </div>
  36.     );
  37. }
  38.  
  39. export default Clock;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement