Skip to content

Commit 8f1249a

Browse files
authored
Merge pull request #870 from frappe/main-hotfix
2 parents aa93191 + d02ccc0 commit 8f1249a

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

frontend/src/utils/index.js

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import TaskStatusIcon from '@/components/Icons/TaskStatusIcon.vue'
22
import TaskPriorityIcon from '@/components/Icons/TaskPriorityIcon.vue'
33
import { usersStore } from '@/stores/users'
44
import { gemoji } from 'gemoji'
5-
import { useTimeAgo } from '@vueuse/core'
65
import { getMeta } from '@/stores/meta'
7-
import { toast, dayjsLocal, dayjs } from 'frappe-ui'
6+
import { toast, dayjsLocal, dayjs, getConfig } from 'frappe-ui'
87
import { h } from 'vue'
98

109
export function formatTime(seconds) {
@@ -65,7 +64,85 @@ export function getFormat(
6564
}
6665

6766
export function timeAgo(date) {
68-
return useTimeAgo(date).value
67+
return prettyDate(date)
68+
}
69+
70+
function getBrowserTimezone() {
71+
return Intl.DateTimeFormat().resolvedOptions().timeZone
72+
}
73+
74+
export function prettyDate(date, mini = false) {
75+
if (!date) return ''
76+
77+
let systemTimezone = getConfig('systemTimezone')
78+
let localTimezone = getConfig('localTimezone') || getBrowserTimezone()
79+
80+
if (typeof date == 'string') {
81+
date = dayjsLocal(date)
82+
}
83+
84+
let nowDatetime = dayjs().tz(localTimezone || systemTimezone)
85+
let diff = nowDatetime.diff(date, 'seconds')
86+
87+
let dayDiff = Math.floor(diff / 86400)
88+
89+
if (isNaN(dayDiff) || dayDiff < 0) return ''
90+
91+
if (mini) {
92+
// Return short format of time difference
93+
if (dayDiff == 0) {
94+
if (diff < 60) {
95+
return __('now')
96+
} else if (diff < 3600) {
97+
return __('{0} m', [Math.floor(diff / 60)])
98+
} else if (diff < 86400) {
99+
return __('{0} h', [Math.floor(diff / 3600)])
100+
}
101+
} else {
102+
if (dayDiff < 7) {
103+
return __('{0} d', [dayDiff])
104+
} else if (dayDiff < 31) {
105+
return __('{0} w', [Math.floor(dayDiff / 7)])
106+
} else if (dayDiff < 365) {
107+
return __('{0} M', [Math.floor(dayDiff / 30)])
108+
} else {
109+
return __('{0} y', [Math.floor(dayDiff / 365)])
110+
}
111+
}
112+
} else {
113+
// Return long format of time difference
114+
if (dayDiff == 0) {
115+
if (diff < 60) {
116+
return __('just now')
117+
} else if (diff < 120) {
118+
return __('1 minute ago')
119+
} else if (diff < 3600) {
120+
return __('{0} minutes ago', [Math.floor(diff / 60)])
121+
} else if (diff < 7200) {
122+
return __('1 hour ago')
123+
} else if (diff < 86400) {
124+
return __('{0} hours ago', [Math.floor(diff / 3600)])
125+
}
126+
} else {
127+
if (dayDiff == 1) {
128+
return __('yesterday')
129+
} else if (dayDiff < 7) {
130+
return __('{0} days ago', [dayDiff])
131+
} else if (dayDiff < 14) {
132+
return __('1 week ago')
133+
} else if (dayDiff < 31) {
134+
return __('{0} weeks ago', [Math.floor(dayDiff / 7)])
135+
} else if (dayDiff < 62) {
136+
return __('1 month ago')
137+
} else if (dayDiff < 365) {
138+
return __('{0} months ago', [Math.floor(dayDiff / 30)])
139+
} else if (dayDiff < 730) {
140+
return __('1 year ago')
141+
} else {
142+
return __('{0} years ago', [Math.floor(dayDiff / 365)])
143+
}
144+
}
145+
}
69146
}
70147

71148
export function taskStatusOptions(action, data) {

0 commit comments

Comments
 (0)