useTime.ts (570B)
1 import { useState, useEffect } from "react"; 2 import moment from "moment-timezone"; 3 4 /** 5 * @description A custom hook which returns the current time as a moment object. 6 * @param none 7 * @returns {moment.Moment} moment object containing current time. 8 */ 9 function useTime<never>(): moment.Moment { 10 const [time, setTime] = useState(moment()); 11 12 function updateTime() { 13 setTime(moment()); 14 } 15 16 useEffect(() => { 17 const interval = setInterval(() => updateTime(), 1000); 18 return () => clearInterval(interval); 19 }); 20 21 return time; 22 } 23 24 export default useTime;