User:Mxn/CommentsInLocalTime

Comments in local time is a user script that reformats talk page timestamps – the ones inserted by  and   – to use a combination of relative date and time formats where appropriate. It is a rewrite of Comments in Local Time with a focus on greater customizability and easier localization.

Features
Recent timestamps are replaced by a relative time such as "3 hours ago" or "Last Tuesday at 5:51 PM". Older timestamps are displayed in full. Both relative and absolute date formats are included in the default tooltip, as is an ISO 8601 timestamp. Relative timestamps refresh automatically.

In all cases, the time is expressed in your computer's local time rather than the wiki's default time zone. This may differ from the time zone used in the MediaWiki interface if your Time offset preference is set to something other than your computer's local time. If your timezone observes daylight saving time during the summer, summertime timestamps are expressed in daylight saving time even if you are currently in the middle of winter:


 * 00:00, 1 July 2015 (UTC)
 * 00:00, 1 December 2015 (UTC)

This user script recognizes both the timestamp format currently used by the English Wikipedia and the abbreviated one used here until 2005:


 * 18:16, 23 Dec 2004 (UTC)
 * 08:51, 23 November 2015 (UTC)

By default, the display format is determined by two user preferences that you can set:


 * Interface language
 * Date format

Installation
On this wiki, append the following code to your user script:

User:Mxn/CommentsInLocalTime.js

On another Wikimedia wiki, use the following code instead:

However, note that the version of the user script on this wiki is only capable of parsing date formats produced by MediaWiki's English localization. If your wiki uses a different language or timezone by default, you may need to take some additional steps.

Compatibility
This user script should in principle work in the following browsers:


 * Firefox 3.5 or above
 * Google Chrome
 * Internet Explorer 9 or above
 * Opera 9 or above
 * Safari 3 or above

However, it has only been tested in a subset of these browsers. Your help is appreciated in verifying the accuracy of the list above.

Customization
You can customize various aspects of this user script by defining a global  object in your user script. A completely customized  looks like this, but don't be intimidated: all of the top-level options are optional. See the section of User:Mxn/CommentsInLocalTime.js labeled for the default values:

None of the original Comments in Local Time gadget is applicable to this user script, so none of them is supported.

User options
This user script relies on Moment.js for nearly all date and calendar logic. Most of the options simply expose options available through that library.

Formats
A format can take either of two forms:


 * A string causes the user script to format the timestamp according to the Moment.js format syntax. In particular, you can place arbitrary text in the format by surrounding it in.
 * A function is called once per timestamp to retrieve the formatted string. A moment object is passed in, and the return value should be a string. See the Moment.js documentation for the myriad things you can do with a moment object. There is no requirement that the return value have anything to do with the moment object.

Some common formats:

Style
This user script wraps each timestamp in a  tag and applies the   class. MediaWiki styles this class with a dotted underline to indicate the existence of a tooltip. You can easily customize the timestamp style from the comfort of your user stylesheet. For instance, this style rule italicizes each timestamp and reduces its font size to 90% of the surrounding text:

How it works
This user script finds all promising DOM text nodes in the document using. Then it uses the Moment.js library to parse and reformat each timestamp. The  is potentially the most expensive part of the script; a fix for  would make it unnecessary.

Porting to other wikis
On a wiki that uses MediaWiki's English localization and UTC by default, you can simply install the existing user script without any additional steps. If you're an administrator, you can install it as a gadget for all the wiki's users:


 * 1) Add the following line to MediaWiki:Gadgets-definition:  * CommentsInLocalTime[ResourceLoader|dependencies=mediawiki.util,moment]|CommentsInLocalTime.js
 * 2) Create MediaWiki:Gadget-CommentsInLocalTime.js with the following code:

Site options
If your wiki uses another localization or timezone by default, you may also need to customize some options that help the user script parse timestamps out of wikitext. See the section of User:Mxn/CommentsInLocalTime.js labeled for the default values. Again, extend the  object:

Each of the following options is optional:

The per-wiki configuration affects parsing only. The actual display is determined by the user's current interface language, which can be configured using Special:Preferences or the  and   URL parameters. The default date and time formats are determined by the Moment.js library. In the somewhat unlikely event that your language is not yet supported by Moment.js, consider contributing a pull request to that project on GitHub. Contributions to Moment.js improve time display across the Web, not just at Wikipedia.