time-convertor-ts

Convert time across timezones (typescript)
git clone http://git.hanabi.in/repos/time-convertor-ts.git
Log | Files | Refs | README | LICENSE

commit ba480443d404b785b154642003473de83abbdf40
parent 71be29692892750c6ecfd18998504dd5327a7f02
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Sun, 24 May 2020 23:12:43 +0530

useTime hook

Diffstat:
Asrc/utils/useTime.ts | 26++++++++++++++++++++++++++
1 file changed, 26 insertions(+), 0 deletions(-)

diff --git a/src/utils/useTime.ts b/src/utils/useTime.ts @@ -0,0 +1,25 @@ +import { useState, useEffect } from 'react'; +import moment from 'moment-timezone'; + + +/** + * @description A custom hook which returns the current time as a moment object. + * @param none + * @returns {moment.Moment} moment object containing current time. + */ +function useTime<never>(): moment.Moment { + const [time, setTime] = useState(moment()); + + function updateTime() { + setTime(moment()); + } + + useEffect(() => { + const interval = setInterval(() => updateTime(), 1000); + return () => clearInterval(interval); + }); + + return time; +} + +export default useTime; +\ No newline at end of file