Skip to content

Commit 74f3054

Browse files
authored
Merge pull request #915 from frappe/mergify/bp/main/pr-913
fix: future date is not captured in pretty date (backport #913)
2 parents 90e80da + ac08ca8 commit 74f3054

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

frontend/src/utils/index.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,32 @@ export function prettyDate(date, mini = false) {
8686

8787
let dayDiff = Math.floor(diff / 86400)
8888

89-
if (isNaN(dayDiff) || dayDiff < 0) return ''
89+
if (isNaN(dayDiff)) return ''
9090

9191
if (mini) {
9292
// Return short format of time difference
93-
if (dayDiff == 0) {
93+
if (dayDiff < 0) {
94+
if (Math.abs(dayDiff) < 1) {
95+
if (diff < 60) {
96+
return __('now')
97+
} else if (diff < 3600) {
98+
return __('in {0} m', [Math.floor(diff / 60)])
99+
} else if (diff < 86400) {
100+
return __('in {0} h', [Math.floor(diff / 3600)])
101+
}
102+
}
103+
if (Math.abs(dayDiff) == 1) {
104+
return __('tomorrow')
105+
} else if (Math.abs(dayDiff) < 7) {
106+
return __('in {0} d', [Math.abs(dayDiff)])
107+
} else if (Math.abs(dayDiff) < 31) {
108+
return __('in {0} w', [Math.floor(Math.abs(dayDiff) / 7)])
109+
} else if (Math.abs(dayDiff) < 365) {
110+
return __('in {0} M', [Math.floor(Math.abs(dayDiff) / 30)])
111+
} else {
112+
return __('in {0} y', [Math.floor(Math.abs(dayDiff) / 365)])
113+
}
114+
} else if (dayDiff == 0) {
94115
if (diff < 60) {
95116
return __('now')
96117
} else if (diff < 3600) {
@@ -111,7 +132,34 @@ export function prettyDate(date, mini = false) {
111132
}
112133
} else {
113134
// Return long format of time difference
114-
if (dayDiff == 0) {
135+
if (dayDiff < 0) {
136+
if (Math.abs(dayDiff) < 1) {
137+
if (diff < 60) {
138+
return __('just now')
139+
} else if (diff < 120) {
140+
return __('in 1 minute')
141+
} else if (diff < 3600) {
142+
return __('in {0} minutes', [Math.floor(diff / 60)])
143+
} else if (diff < 7200) {
144+
return __('in 1 hour')
145+
} else if (diff < 86400) {
146+
return __('in {0} hours', [Math.floor(diff / 3600)])
147+
}
148+
}
149+
if (Math.abs(dayDiff) == 1) {
150+
return __('tomorrow')
151+
} else if (Math.abs(dayDiff) < 7) {
152+
return __('in {0} days', [Math.abs(dayDiff)])
153+
} else if (Math.abs(dayDiff) < 31) {
154+
return __('in {0} weeks', [Math.floor(Math.abs(dayDiff) / 7)])
155+
} else if (Math.abs(dayDiff) < 365) {
156+
return __('in {0} months', [Math.floor(Math.abs(dayDiff) / 30)])
157+
} else if (Math.abs(dayDiff) < 730) {
158+
return __('in 1 year')
159+
} else {
160+
return __('in {0} years', [Math.floor(Math.abs(dayDiff) / 365)])
161+
}
162+
} else if (dayDiff == 0) {
115163
if (diff < 60) {
116164
return __('just now')
117165
} else if (diff < 120) {

0 commit comments

Comments
 (0)