Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Humanipedia
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Module:Diff days
Module
Discussion
English
Read
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
require('strict'); local get_args = require ('Module:Arguments').getArgs; local lang_object = mw.getContentLanguage(); --[[--------------------------< D I F F _ D A Y S >------------------------------------------------------------ return difference between two date/time strings <t1>, and <t2>, in days to <precision> decimal places. Converts each date/time string to a unix timestamp (seconds), subtracts <t1> from <t2>, divides the difference by the number of seconds in a day (86400) and returns a formatted string to <precision> decimal places. The default precision is 3 decimal places. A positive result is returned when <t2> is the same as, or is 'later' (more recent) than, <t1>; negative results else. to call this from a template: {{#invoke:diff days|diff_days|<t1>|<t2>|precision=}} where <t1> and <t2> are date/time strings acceptable to the MediaWiki #time parser ]] local function diff_days (frame) local args_t = get_args (frame); local t1 = args_t[1]; -- t1 is 'start' time local t2 = args_t[2]; -- t2 is 'end' time local precision = tonumber (args_t.precision) or 3; -- number of decimal places to show; default is 3 if 0 > precision then return '<span style="color:#d33">invalid precision: ' .. precision .. '</span>'; end if not t1 or not t2 then return '<span style="color:#d33">both of t1 and t2 required</span>'; end local good; local time1, time2; -- to hold Unix time stamps representing the dates good, time1 = pcall (lang_object.formatDate, lang_object, 'U', t1 ); -- convert t1 value to unix timestamp if not good then return '<span style="color:#d33">invalid time: ' .. t1 .. '</span>'; end good, time2 = pcall (lang_object.formatDate, lang_object, 'U', t2 ); -- convert t2 value to unix timestamp if not good then return '<span style="color:#d33">invalid time: ' .. t2 .. '</span>'; end return string.format ('%.'.. precision .. 'f', (time2 - time1)/86400); end --[[--------------------------< E X P O R T E D F U N C T I O N >-------------------------------------------- ]] return { diff_days = diff_days }
Summary:
Please note that all contributions to Humanipedia may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Humanipedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Diff days/doc
(
edit
)