<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.cultopedia.org/index.php?action=history&amp;feed=atom&amp;title=Module%3AAuto_date_formatter</id>
	<title>Module:Auto date formatter - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.cultopedia.org/index.php?action=history&amp;feed=atom&amp;title=Module%3AAuto_date_formatter"/>
	<link rel="alternate" type="text/html" href="https://www.cultopedia.org/index.php?title=Module:Auto_date_formatter&amp;action=history"/>
	<updated>2026-04-06T04:11:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.5</generator>
	<entry>
		<id>https://www.cultopedia.org/index.php?title=Module:Auto_date_formatter&amp;diff=217&amp;oldid=prev</id>
		<title>imported&gt;Rjjiii: There is probably a more robust way to fix this. I&#039;ll post on the talk page in moment.</title>
		<link rel="alternate" type="text/html" href="https://www.cultopedia.org/index.php?title=Module:Auto_date_formatter&amp;diff=217&amp;oldid=prev"/>
		<updated>2025-10-24T02:39:45Z</updated>

		<summary type="html">&lt;p&gt;There is probably a more robust way to fix this. I&amp;#039;ll post on the talk page in moment.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;require (&amp;#039;strict&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
local lang_obj = mw.language.getContentLanguage();								-- needed for the function to reformat dates&lt;br /&gt;
&lt;br /&gt;
local global_df = mw.loadData (&amp;#039;Module:Citation/CS1/Configuration&amp;#039;).global_df;	-- fetch global date format specified by {{use xxx dates}} template&lt;br /&gt;
local base_format = global_df and global_df:match (&amp;#039;^(%a%a%a)&amp;#039;);				-- get {{use xxx dates}} base date format: &amp;#039;dmy&amp;#039; or &amp;#039;mdy&amp;#039;&lt;br /&gt;
local date_style = global_df and global_df:match (&amp;#039;^%a%a%a%-(.+)&amp;#039;);				-- get |cs1-dates= style modifiers&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[[--------------------------&amp;lt; F O R M A T T E R &amp;gt;------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
local function to format &amp;lt;date&amp;gt; according to &amp;lt;format&amp;gt; (dmy or mdy) and &amp;lt;style&amp;gt; (long, short, year initial) as&lt;br /&gt;
specified by the |cs1-dates= parameter of the {{use xxx dates}} template.&lt;br /&gt;
&lt;br /&gt;
valid |cs1-dates= format character strings are:&lt;br /&gt;
	l, ll, all	– default; long-form publication and access- / archive-dates; &amp;#039;all&amp;#039; when |cs1-dates= omitted or empty&lt;br /&gt;
	ls			– long-form publication dates; abbreviated access- / archive-dates&lt;br /&gt;
	ly			– long-form publication dates; year-initial numeric access- / archive-dates (ymd)&lt;br /&gt;
	s, ss		– abbreviated publication and access- / archive-dates&lt;br /&gt;
	sy			– abbreviated publication dates; year-initial numeric access- / archive-dates (ymd)&lt;br /&gt;
	y, yy		– year-initial numeric publication, access- and archive-dates (ymd); overrides base format&lt;br /&gt;
&lt;br /&gt;
note: &amp;#039;all&amp;#039; is added by get_date_format() in Module:Citation/CS1/Configuration when |cs1-dates= is omitted or empty;&lt;br /&gt;
cs1|2 and this module then assume long-form date style for all dates&lt;br /&gt;
&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
local function formatter (date, format, style)&lt;br /&gt;
--[[&lt;br /&gt;
First check for year-only in the date field.&lt;br /&gt;
Next check for a year and month only.&lt;br /&gt;
In either case, just return the date as provided.&lt;br /&gt;
]]&lt;br /&gt;
	if date:match(&amp;#039;^%d%d%d%d$&amp;#039;) then                                            -- YYYY&lt;br /&gt;
		return date  &lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if date:match(&amp;#039;^%a+%s+%d%d%d%d$&amp;#039;) or                                        -- &amp;quot;Month YYYY&amp;quot;&lt;br /&gt;
	   date:match(&amp;#039;^%d%d%d%d%-%d%d$&amp;#039;) then                                      -- &amp;quot;YYYY-MM&amp;quot;&lt;br /&gt;
		return date  &lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local format_strings_t = {													-- map &amp;lt;style&amp;gt; to formatDate() format strings (same as #time parser function)&lt;br /&gt;
		[&amp;#039;dmy&amp;#039;] = {&lt;br /&gt;
			[&amp;#039;l&amp;#039;] = &amp;#039;j F Y&amp;#039;,													-- long month name&lt;br /&gt;
			[&amp;#039;s&amp;#039;] = &amp;#039;j M Y&amp;#039;,													-- abbreviated month name&lt;br /&gt;
			[&amp;#039;y&amp;#039;] = &amp;#039;Y-m-d&amp;#039;														-- year initial numeric; overrides &amp;lt;format&amp;gt; (dmy/mdy)&lt;br /&gt;
			},&lt;br /&gt;
		[&amp;#039;mdy&amp;#039;] = {&lt;br /&gt;
			[&amp;#039;l&amp;#039;] = &amp;#039;F j, Y&amp;#039;,													-- long month name&lt;br /&gt;
			[&amp;#039;s&amp;#039;] = &amp;#039;M j, Y&amp;#039;,													-- abbreviated month name&lt;br /&gt;
			[&amp;#039;y&amp;#039;] = &amp;#039;Y-m-d&amp;#039;														-- year initial numeric; overrides &amp;lt;format&amp;gt; (dmy/mdy)&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
	local good, new_date = pcall (lang_obj.formatDate, lang_obj, format_strings_t[format][style], date);	-- attempt to reformat; on success, &amp;lt;good&amp;gt; is boolean true&lt;br /&gt;
	return (good and new_date) or date;											-- returns &amp;lt;new_date&amp;gt; when &amp;lt;good&amp;gt; is boolean true; &amp;lt;date&amp;gt; else&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[[--------------------------&amp;lt; _ P U B _ D A T E _ F O R M A T &amp;gt;----------------------------------------------&lt;br /&gt;
&lt;br /&gt;
Module entry point&lt;br /&gt;
&lt;br /&gt;
For publication dates |date=, |publication-date=, etc&lt;br /&gt;
&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
local function _pub_date_format (date)&lt;br /&gt;
	if not global_df then&lt;br /&gt;
		return date;															-- when article does not have {{use xxx dates}}, abandon&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local styles_t = {all = &amp;#039;l&amp;#039;, ll = &amp;#039;l&amp;#039;, l = &amp;#039;l&amp;#039;, ls = &amp;#039;l&amp;#039;, ly = &amp;#039;l&amp;#039;, s = &amp;#039;s&amp;#039;, ss = &amp;#039;s&amp;#039;, sy = &amp;#039;s&amp;#039;, yy = &amp;#039;y&amp;#039;, y = &amp;#039;y&amp;#039;};	-- map known styles for publication dates&lt;br /&gt;
	return formatter (date, base_format, styles_t[date_style] or &amp;#039;l&amp;#039;);			-- not a known style default to &amp;#039;l&amp;#039; (long form)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[[--------------------------&amp;lt; P U B _ D A T E _ F O R M A T &amp;gt;------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
#invoke entry point&lt;br /&gt;
&lt;br /&gt;
For publication dates |date=, |publication-date=, etc&lt;br /&gt;
&lt;br /&gt;
	{{#invoke:auto date formatter|pub_date_format|16 March 2025}}&lt;br /&gt;
&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
local function pub_date_format (frame)&lt;br /&gt;
	return _pub_date_format (frame.args[1]);									-- &amp;lt;args[1]&amp;gt; is date to be formatted&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[[--------------------------&amp;lt; _ A C C E S S _ A R C H I V E _ F O R M A T &amp;gt;----------------------------------&lt;br /&gt;
&lt;br /&gt;
module entry point&lt;br /&gt;
&lt;br /&gt;
For access and archive dates |access-date=, |accessdate=, archive-date=, archivedate=&lt;br /&gt;
&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
local function _access_archive_format (date)&lt;br /&gt;
	if not global_df then&lt;br /&gt;
		return date;															-- when article does not have {{use xxx dates}}, abandon&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	local styles_t = {all = &amp;#039;l&amp;#039;, ll = &amp;#039;l&amp;#039;, l = &amp;#039;l&amp;#039;, ls = &amp;#039;s&amp;#039;, ly = &amp;#039;y&amp;#039;, s = &amp;#039;s&amp;#039;, ss = &amp;#039;s&amp;#039;, sy = &amp;#039;y&amp;#039;, yy = &amp;#039;y&amp;#039;, y = &amp;#039;y&amp;#039;};	-- map known styles for access/archive dates&lt;br /&gt;
	return formatter (date, base_format, styles_t[date_style] or &amp;#039;l&amp;#039;);			-- not a known style default to &amp;#039;l&amp;#039; (long form)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[[--------------------------&amp;lt; A C C E S S _ A R C H I V E _ F O R M A T &amp;gt;------------------------------------&lt;br /&gt;
&lt;br /&gt;
#invoke entry point&lt;br /&gt;
&lt;br /&gt;
For access and archive dates |access-date=, |accessdate=, archive-date=, archivedate=&lt;br /&gt;
&lt;br /&gt;
	{{#invoke:auto date formatter|access_archive_format|16 March 2025}}&lt;br /&gt;
&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
local function access_archive_format (frame)&lt;br /&gt;
	return _access_archive_format (frame.args[1]);								-- &amp;lt;args[1]&amp;gt; is date to be formatted&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--[[--------------------------&amp;lt; E X P O R T S &amp;gt;----------------------------------------------------------------&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
return {&lt;br /&gt;
	pub_date_format = pub_date_format,											-- #invoke entry points&lt;br /&gt;
	access_archive_format = access_archive_format,&lt;br /&gt;
&lt;br /&gt;
	_pub_date_format = _pub_date_format,										-- module entry points&lt;br /&gt;
	_access_archive_format = _access_archive_format,&lt;br /&gt;
	}&lt;/div&gt;</summary>
		<author><name>imported&gt;Rjjiii</name></author>
	</entry>
</feed>