Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, {useState} from 'react';
- import './Clock_style.css'
- function Clock(props) {
- const [time, set_time] = useState('');
- const showTime = () =>{
- let date = new Date();
- let h = date.getHours(); // 0 - 23
- let m = date.getMinutes(); // 0 - 59
- let s = date.getSeconds(); // 0 - 59
- let session = "AM";
- if(h === 0){
- h = 12;
- }
- if(h > 12){
- h = h - 12;
- session = "PM";
- }
- h = (h < 10) ? "0" + h : h;
- m = (m < 10) ? "0" + m : m;
- s = (s < 10) ? "0" + s : s;
- let time_string = h + ":" + m + ":" + s + " " + session;
- set_time(time_string)
- };
- setInterval(() => showTime(), 1000);
- return (
- <div className="clock_box">
- {time}
- </div>
- );
- }
- export default Clock;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement