Module talk:Date
Template-protected edit request on 15 May 2022
[edit]This edit request has been answered. Set the |answered= parameter to no to reactivate your request. |
Tostkeoydouxyod (talk) 17:21, 15 May 2022 (UTC)
Date active: 2011-always
Not done not a scrubinto change. — xaosflux Talk 17:25, 15 May 2022 (UTC)
Date math
[edit]@Johnuniq: (or anyone else) is there an easy way to basic date math? I.e.
local today = Date('currentdate')
local last_year = today - 1.year
I know I can grab the year, subtract one and form a new date by mashing strings together but before I go that route I want to make sure I’m not missing an easier solution… Thanks! Zackmann (Talk to me/What I been doing) 20:32, 30 September 2025 (UTC)
- @Zackmann08: It looks like you want to do this in a module. Before looking at that, you might like to know about {{extract}}. Examples:
{{extract|currentdate|add=-1y}}→ 12 December 2024{{extract|currentdate|add=-1y|show=year}}→ 2024
- Of course #time can do easily do the above. For a module, if all that is wanted is last year, try
local last_year = require('Module:Date')._current.year - 1
- Johnuniq (talk) 23:46, 30 September 2025 (UTC)
- Actually, Module:Date is not needed.
local last_year = os.date('!*t').year - 1
- Johnuniq (talk) 23:51, 30 September 2025 (UTC)
- Stellar! Thanks so much for the info! - Zackmann (Talk to me/What I been doing) 00:46, 1 October 2025 (UTC)
- Actually, Module:Date is not needed.
Month abbreviations
[edit]In my work on Module:Person date I ran into an issue where abbreviated months (i.e. Aug. Jan. etc.) were not working. I traced through my code and turns out it comes down to a call where I basically do Date('1 Apr. 2016'). By my read of lines 571-582, those abbreviations are not supported by Module:Date. I'm wondering if they can be? I've found a number of cases in articles of people posting dates with this abbreviated form. I think this is a simple change which I have mocked up in the sandbox (see the comparison). That being said, given how widely used this module is I am VERY wary of implementing any change without a thorough code review. @Johnuniq: would love to hear your thoughts. --Zackmann (Talk to me/What I been doing) 08:12, 3 October 2025 (UTC)
- The ustring family is very clever but it is not needed in this case. I put a more efficient version in the sandbox. It's probably ok (I ran some tests). The issue is what abbreviations should be supported as opposed to fixing the wikitext. What about '1 Ap. 2016' and similar? Johnuniq (talk) 08:50, 3 October 2025 (UTC)
- That looks perfect to me. I would think that the non-period abbreviations you already have spelled out in lines 571-582 cover it. A google search turned up this from the Yale Library for what that's worth. I don't think I've ever seen anyone intentionally abbreviate a month with only 2 letters (Oc or De for example) and I'm not in favor of trying to cover EVERY scenario, but covering the basic case of a period after a 3 (or in the case of Sept, a 4) letter month seems like it would be super helpful. --Zackmann (Talk to me/What I been doing) 09:08, 3 October 2025 (UTC)
- @Johnuniq: I tried to create Module:Date/testcases but seem to be doing something wrong because it isn't actually calling the code. If you can point me in the right direction, I'd be happy to imlement more tests... Zackmann (Talk to me/What I been doing) 21:22, 4 October 2025 (UTC)
- @Zackmann08: I fixed testcases. The module uses the convention that entry points callable from another module start with an underscore. That is, _Date not Date. FYI I run tests on 1270 cases on a local computer. It's too hard to do that here so that's why I never wrote a testcases page. However, testcases should exist, so thanks. You might like to have a look at Module:Date/example and its talk. Johnuniq (talk) 01:01, 5 October 2025 (UTC)
- This is going to require some more investigating... See Module talk:Date/testcases. Nothing is being output by the current testcases. I think I need to call the
:text()method and I'm not entirely sure how to do that... - Is reassuring to know that you have a nice test suite that you run locally though! Zackmann (Talk to me/What I been doing) 01:13, 5 October 2025 (UTC)
- Sorry, I wasn't paying attention. You can see that I don't know anything about Module:UnitTests. Its documentation points to Module:ScribuntoUnit which a very quick glance suggests might work. Johnuniq (talk) 02:11, 5 October 2025 (UTC)
- I shall investigate further. Thanks! --Zackmann (Talk to me/What I been doing) 02:14, 5 October 2025 (UTC)
- @Johnuniq: in the meantime, what are your thoughts on bringing the sandbox version live? Were you able to run your test suite against the change? - Zackmann (Talk to me/What I been doing) 09:42, 6 October 2025 (UTC)
- I updated the main module. All my tests pass. Johnuniq (talk) 10:05, 6 October 2025 (UTC)
- @Johnuniq: in the meantime, what are your thoughts on bringing the sandbox version live? Were you able to run your test suite against the change? - Zackmann (Talk to me/What I been doing) 09:42, 6 October 2025 (UTC)
- I shall investigate further. Thanks! --Zackmann (Talk to me/What I been doing) 02:14, 5 October 2025 (UTC)
- Sorry, I wasn't paying attention. You can see that I don't know anything about Module:UnitTests. Its documentation points to Module:ScribuntoUnit which a very quick glance suggests might work. Johnuniq (talk) 02:11, 5 October 2025 (UTC)
- This is going to require some more investigating... See Module talk:Date/testcases. Nothing is being output by the current testcases. I think I need to call the
- @Zackmann08: I fixed testcases. The module uses the convention that entry points callable from another module start with an underscore. That is, _Date not Date. FYI I run tests on 1270 cases on a local computer. It's too hard to do that here so that's why I never wrote a testcases page. However, testcases should exist, so thanks. You might like to have a look at Module:Date/example and its talk. Johnuniq (talk) 01:01, 5 October 2025 (UTC)
- @Johnuniq: I tried to create Module:Date/testcases but seem to be doing something wrong because it isn't actually calling the code. If you can point me in the right direction, I'd be happy to imlement more tests... Zackmann (Talk to me/What I been doing) 21:22, 4 October 2025 (UTC)
- That looks perfect to me. I would think that the non-period abbreviations you already have spelled out in lines 571-582 cover it. A google search turned up this from the Yale Library for what that's worth. I don't think I've ever seen anyone intentionally abbreviate a month with only 2 letters (Oc or De for example) and I'm not in favor of trying to cover EVERY scenario, but covering the basic case of a period after a 3 (or in the case of Sept, a 4) letter month seems like it would be super helpful. --Zackmann (Talk to me/What I been doing) 09:08, 3 October 2025 (UTC)
Detecting format of date
[edit]@Johnuniq: is there a way to detect the input format used to build a date? For example:
local Date = require('Module:Date')._Date
mw.log(Date('1 July 2016')) -- Outputs 1 July 2016
mw.log(Date('July 1, 2016')) -- Outputs 1 July 2016
Now I know that I can override the output with :text('mdy') but say I do not know the format that is being supplied... I want to accept both mdy format as well as dmy format. I want to do some calculation, then return the result in the same format it was supplied. So if I started with mdy, I want to end with mdy and ditto for dmy.
Is there someway to detect which format was sent in?? If not, is this something that could be added? FWIW this is blocking a merge that I can explain further if you are interested, but suffice it to say this capability would be VERY helpful. Zackmann (Talk to me/What I been doing) 03:38, 4 December 2025 (UTC)
- If
dis a valid date (that is, not nil),d.formatis a string: dmy, mdy, ymd, error. See {{extract}} which documentsshow=format. I had forgotten how a module does that so I looked in Module:Date/example (linked in the doc) and it has an example. Johnuniq (talk) 04:45, 4 December 2025 (UTC)- You have NO IDEA what a headache you just saved me... I thought I was going to have to write that functionality myself and I was preparing to put my head through a wall... Thank you!!!!!!! Zackmann (Talk to me/What I been doing) 04:50, 4 December 2025 (UTC)