-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-script.js
More file actions
38 lines (30 loc) · 1.45 KB
/
test-script.js
File metadata and controls
38 lines (30 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const posted = { type: 'hours', value: 1 };
const platform = 'whatsapp';
function getRelativeTime(posted, platform = 'instagram') {
if (!posted || typeof posted !== 'object') {
posted = { type: 'hours', value: 1, customText: '' }
}
if (posted.type === 'custom') {
return posted.customText || ''
}
if (platform === 'whatsapp') {
let offsetMs = 0
if (posted.type === 'minutes') offsetMs = (posted.value || 1) * 60000
else if (posted.type === 'hours') offsetMs = (posted.value || 1) * 3600000
const date = new Date(Date.now() - offsetMs)
const timeStr = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false })
// WhatsApp Format: "Today, 14:30" or "Yesterday, 09:15"
const nowDay = new Date().setHours(0, 0, 0, 0)
const postedDay = new Date(date).setHours(0, 0, 0, 0)
const diffDay = Math.round((nowDay - postedDay) / (1000 * 60 * 60 * 24))
if (diffDay === 0) return `Today, ${timeStr}`
if (diffDay === 1) return `Yesterday, ${timeStr}`
return `${date.getDate()} ${date.toLocaleString('default', { month: 'short' })}, ${timeStr}`
}
// Instagram Format
if (posted.type === 'now') return 'just now'
if (posted.type === 'minutes') return `${posted.value || 1}m`
if (posted.type === 'hours') return `${posted.value || 1}h`
return 'just now'
}
console.log("Output is:", getRelativeTime(posted, platform));