Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4114722
lastModified.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
lastModified.js
View Options
/**
* Updates the text content of a specific HTML element to display a human-readable,
* relative time format based on a timestamp attribute.
*
* @return {void}
*/
function
init
()
{
const
lastmodEl
=
document
.
getElementById
(
'citizen-lastmod-relative'
);
if
(
!
lastmodEl
||
typeof
Intl
.
RelativeTimeFormat
!==
'function'
)
{
return
;
}
const
lang
=
document
.
documentElement
.
getAttribute
(
'lang'
);
const
rtf
=
new
Intl
.
RelativeTimeFormat
(
lang
);
const
DIVISIONS
=
[
{
amount
:
60
,
name
:
'seconds'
},
{
amount
:
60
,
name
:
'minutes'
},
{
amount
:
24
,
name
:
'hours'
},
{
amount
:
7
,
name
:
'days'
},
{
amount
:
4.34524
,
name
:
'weeks'
},
{
amount
:
12
,
name
:
'months'
},
{
amount
:
Number
.
POSITIVE_INFINITY
,
name
:
'years'
}
];
const
SECONDS_IN_MILLISECOND
=
1000
;
// eslint-disable-next-line jsdoc/require-returns-check
/**
* Formats the time elapsed since a given date in a human-readable relative time format.
*
* @param {string} date - The timestamp to calculate relative time from.
* @return {string} The formatted relative time string.
*/
const
formatTimeAgo
=
(
date
)
=>
{
const
timestamp
=
parseFloat
(
date
);
if
(
isNaN
(
timestamp
)
)
{
mw
.
log
.
error
(
'[Citizen] Invalid timestamp value'
);
return
;
}
let
duration
=
timestamp
-
Date
.
now
()
/
SECONDS_IN_MILLISECOND
;
for
(
let
i
=
0
;
i
<
DIVISIONS
.
length
;
i
++
)
{
const
division
=
DIVISIONS
[
i
];
if
(
Math
.
abs
(
duration
)
<
division
.
amount
)
{
return
rtf
.
format
(
Math
.
round
(
duration
),
division
.
name
);
}
duration
/=
division
.
amount
;
}
};
lastmodEl
.
lastChild
.
textContent
=
formatTimeAgo
(
lastmodEl
.
getAttribute
(
'data-timestamp'
)
);
}
module
.
exports
=
{
init
:
init
};
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Aug 19, 00:22 (4 d, 23 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
5b/77/30090efb3fd50f2b60c25b482590
Default Alt Text
lastModified.js (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment