commit 1d61ea8663658c5bbbea211aac922a2cbbda234f
parent 9f2026dbecb6e173c182bc887db97761ec48a701
Author: Agastya Chandrakant <me@hanabi.in>
Date: Mon, 18 May 2020 12:00:00 +0530
create helper functions
Diffstat:
2 files changed, 36 insertions(+), 0 deletions(-)
diff --git a/src/comp/helpers/prepData.js b/src/comp/helpers/prepData.js
@@ -0,0 +1,21 @@
+import moment from 'moment-timezone';
+
+function prepData(time, TZ2, TZ1 = '') {
+ const ISOtime = time.toISOString();
+ const tz = TZ1 || moment.tz.guess();
+ const firstTZ = tz.replace(/_/g, ' ');
+ const now = moment(ISOtime);
+ const firstAbbr = moment.tz.zone(tz).abbr(now);
+ const secondAbbr = moment.tz.zone(TZ2).abbr(now);
+ let fmtTime1 = now.tz(tz).format('HH:mm:ss MMM DD, YYYY');
+ let fmtTime2 = now.tz(TZ2).format('HH:mm:ss MMM DD, YYYY');
+ return {
+ firstAbbr,
+ fmtTime1,
+ fmtTime2,
+ firstTZ,
+ secondAbbr,
+ };
+}
+
+export default prepData;
diff --git a/src/comp/helpers/useTime.js b/src/comp/helpers/useTime.js
@@ -0,0 +1,15 @@
+import React from 'react';
+
+function useTime() {
+ const [time, setTime] = React.useState(new Date());
+ function updateTime() {
+ setTime(new Date());
+ }
+ React.useEffect(() => {
+ let interval = setInterval(() => updateTime(), 1000);
+ return () => clearInterval(interval);
+ });
+ return time;
+}
+
+export default useTime;