@fanam-pkg/core-utils
Version:
Core Functions are managed here for quick web development
36 lines (30 loc) • 1.09 kB
text/typescript
import customParseFormat from "dayjs/plugin/customParseFormat"
import utc from "dayjs/plugin/utc"
import dayjs from "dayjs"
import {DF_FULL_DATE} from "../utils"
dayjs.extend(utc)
export const useDateTime = () => {
const formatDate = (date: string, format = DF_FULL_DATE) => dayjs(date).format(format)
const formatDateUTC = (inputDate: string) => dayjs(inputDate).utc(true).format()
const getJSDate = (date: string | number | Date, dateFormat: string = DF_FULL_DATE): Date => {
try {
dayjs.extend(customParseFormat)
if (typeof date === "string") {
const value = dayjs(date, dateFormat)
if (value.isValid()) {
date = value.toDate()
} else throw new Error("Error in converting date format")
} else if (typeof date === "number") date = new Date(date)
} catch (error) {
console.log(">>>>>>>>>>>>>>>>> useDateTime - getJSDate: ", error, date, dateFormat)
date = new Date(date)
}
return date
}
return {
dayjs,
formatDate,
getJSDate,
formatDateUTC,
}
}