|
@@ -3,12 +3,72 @@ import { useState } from "react";
|
|
|
/*
|
|
|
Executes before Render
|
|
|
*/
|
|
|
-export const useConstructor = (callBack = () => {}) => {
|
|
|
+export const useConstructor = (callBack = () => { }) => {
|
|
|
const [hasBeenCalled, setHasBeenCalled] = useState(false);
|
|
|
if (hasBeenCalled) return;
|
|
|
callBack();
|
|
|
setHasBeenCalled(true);
|
|
|
}
|
|
|
|
|
|
-export const capitalize = (str) => str.charAt(0).toUpperCase() + str.substring(1,str.length).toLowerCase()
|
|
|
+const _MS_PER_SECOND = 1000;
|
|
|
+const _MS_PER_MINUTE = _MS_PER_SECOND * 60;
|
|
|
+const _MS_PER_HOUR = _MS_PER_MINUTE * 60;
|
|
|
+const _MS_PER_DAY = _MS_PER_HOUR * 24;
|
|
|
+const _MS_PER_MONTH = _MS_PER_DAY * 31;
|
|
|
+const _MS_PER_YEAR = _MS_PER_MONTH * 12;
|
|
|
|
|
|
+export const capitalize = (str) => str.charAt(0).toUpperCase() + str.substring(1, str.length).toLowerCase()
|
|
|
+
|
|
|
+export const getDifferenceInDates = (startDate, endDate) => {
|
|
|
+ var diff = Math.floor(startDate.getTime() - endDate.getTime());
|
|
|
+ return {
|
|
|
+ "year": Math.floor(diff / _MS_PER_YEAR),
|
|
|
+ "month": Math.floor(diff / _MS_PER_MONTH),
|
|
|
+ "day": Math.floor(diff / _MS_PER_DAY),
|
|
|
+ "minute": Math.floor(diff / _MS_PER_MINUTE),
|
|
|
+ "second": Math.floor(diff / _MS_PER_SECOND),
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+export const getTimestamp = (iso_date_str) => {
|
|
|
+ let diffObj = getDifferenceInDates(new Date(), new Date(iso_date_str))
|
|
|
+ var result = "Timestamp Not Found"
|
|
|
+ for (const key of Object.keys(diffObj)) {
|
|
|
+ if (diffObj[key] > 0) {
|
|
|
+ result = `${diffObj[key]} ${capitalize(key)}${ diffObj > 1 ? "s" : ""} ago`
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+const _getMonth = (index) => {
|
|
|
+ const months = {
|
|
|
+ 0: "January",
|
|
|
+ 1: "Febraury",
|
|
|
+ 2: "March",
|
|
|
+ 3: "April",
|
|
|
+ 4: "May",
|
|
|
+ 5: "June",
|
|
|
+ 6: "July",
|
|
|
+ 7: "August",
|
|
|
+ 8: "September",
|
|
|
+ 9: "October",
|
|
|
+ 10: "November",
|
|
|
+ 11: "December"
|
|
|
+ }
|
|
|
+ if (Object.hasOwn(months, index)) {
|
|
|
+ return months[index]
|
|
|
+
|
|
|
+ } else {
|
|
|
+ return "Month Not Found"
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+export const getDate = (iso_date_str) => {
|
|
|
+ let date = new Date(iso_date_str)
|
|
|
+ return `${date.getDate()} ${_getMonth(date.getMonth())} ${date.getFullYear()}`
|
|
|
+}
|