From d991eb52ef88f9562048e7c407d76302a1c3c8ce Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 2 Dec 2024 12:41:45 +0530 Subject: [PATCH] fix: added utils to get format & formatted date --- frontend/src/utils/index.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/frontend/src/utils/index.js b/frontend/src/utils/index.js index 4bb9b7f6..b0e44f5e 100644 --- a/frontend/src/utils/index.js +++ b/frontend/src/utils/index.js @@ -3,7 +3,7 @@ import TaskPriorityIcon from '@/components/Icons/TaskPriorityIcon.vue' import { usersStore } from '@/stores/users' import { gemoji } from 'gemoji' import { useTimeAgo } from '@vueuse/core' -import { toast, convertToUserTimezone } from 'frappe-ui' +import { toast, convertToUserTimezone, luxonDate } from 'frappe-ui' import { h } from 'vue' export function createToast(options) { @@ -38,10 +38,34 @@ export function formatTime(seconds) { return formattedTime.trim() } -export function formatDate(date, format = 'EEE, MMM d, yyyy h:mm a') { +export function formatDate(date, format, onlyDate = false, onlyTime = false) { + format = getFormat(date, format, onlyDate, onlyTime, false) return convertToUserTimezone(date, format) } +export function getFormat( + date, + format, + onlyDate = false, + onlyTime = false, + withDate = true, +) { + if (!date) return '' + let dateFormat = + window.sysdefaults.date_format.replace('mm', 'MM') || 'yyyy-MM-dd' + let timeFormat = window.sysdefaults.time_format || 'HH:mm:ss' + format = format || 'EEE, MMM d, yyyy h:mm a' + + if (onlyDate) format = dateFormat + if (onlyTime) format = timeFormat + if (onlyTime && onlyDate) format = `${dateFormat} ${timeFormat}` + + if (withDate) { + return luxonDate(date).toFormat(format) + } + return format +} + export function timeAgo(date) { return useTimeAgo(date).value }