| Bytes | Lang | Time | Link |
|---|---|---|---|
| 017 | Pip | 210630T001132Z | DLosc |
| 012 | TIBasic | 221118T024347Z | Youserna |
| 030 | Mathematica | 210702T183758Z | Lance |
| 020 | K ngn/k | 210702T103509Z | mkst |
| 035 | Perl 5 | 210701T152715Z | Kjetil S |
| 024 | Excel | 210630T124348Z | Crissov |
| 032 | JavaScript Node.js | 210630T000149Z | mrmonkin |
| 051 | Python 3 | 210630T110121Z | SevC_10 |
| 041 | Python 3 | 210630T132453Z | Hunaphu |
| 018 | Excel formula | 210630T143714Z | Spencer |
| 042 | PHP | 210630T074349Z | Kaddath |
| 057 | R | 210630T121240Z | Dominic |
| nan | 210629T181528Z | Jeff Zei | |
| 027 | C gcc | 210629T215018Z | Noodle9 |
| 040 | R | 210630T073808Z | pajonk |
| 059 | Desmos | 210629T212816Z | Aiden Ch |
| 017 | Vyxal | 210629T204700Z | emanresu |
| 033 | JavaScript | 210630T023531Z | tsh |
| 027 | Haskell | 210629T192459Z | lynn |
| 029 | Factor | 210629T215854Z | chunes |
| 006 | Gaia | 210629T215019Z | ovs |
| 016 | Japt | 210629T213359Z | Shaggy |
| 015 | Jelly | 210629T175158Z | caird co |
| 014 | Vyxal s | 210629T204606Z | a stone |
| 009 | MATL | 210629T180531Z | Luis Men |
| 016 | Bash | 210629T180017Z | Digital |
| 036 | Perl 5 p | 210629T193538Z | Nahuel F |
| 036 | JavaScript ES6 | 210629T182740Z | Arnauld |
| 020 | Charcoal | 210629T185038Z | Neil |
| 036 | Red | 210629T183558Z | Galen Iv |
| 011 | APL Dyalog Unicode | 210629T175326Z | Adá |
| 141 | Java | 210629T174259Z | kaiffeet |
Pip, 24 23 17 bytes
275*a//9-30+b-2%a
Uses the formula from lynn's Haskell solution. Attempt This Online!
Original approach, 23 bytes:
b+$+A*""@<Da
The code contains unprintable characters. Here's a hexdump:
00000000: 622b 242b 412a 221f 1c1f 1e1f 1e1f 1f1e b+$+A*".........
00000010: 1f1e 2240 3c44 61 .."@<Da
Explanation
a (month) and b (day) are command-line arguments
"..........." String containing characters with codes 31, 28, 31, etc.
for the months January through November
A* Get the ASCII code of each character
Da Decrement a
@< The first (^ that many) items of the list of charcodes
$+ Sum
b+ Add b
TI-Basic, 12 bytes
dbd(101.1,Ans+.1
Takes input in Ans as an integer in the format DDMM (for example, November 23 is 2311).
Mathematica, 30 bytes
{1}~DateDifference~{1,#,#2+1}&
Using the DateDifference builtin and the fact that {y} is treated as January 1st on year y. I thought of {1}~DateDifference~{##}& at first, but sadly DateDifference starts from 0 days instead of 1.
K (ngn/k), 25 20 bytes
Solution:
{+/y,x#28+4\3390446}
Explanation:
Assumes months are 0-indexed!
Naive approach, there are likely better ones out there.
{+/y,x#28+4\3390446} / the solution
{ } / lambda taking implicit x, y args
4\3390446 / creates 3 0 3 2 3 2 3 3 2 3 2 from base-4
28+ / add 28 to each item (vectorised)
x# / take 'month' items from this list (January = 0)
y, / prepend 'days'
+/ / sum up
Edits:
- -5 bytes; using deltas against 28 instead of days-per-month
Extra:
- +2 bytes for 1-indexed month
{+/y,x#0,28+4\3390446}
Perl 5, 35 bytes
Just a translation of the Haskell answer from @Lynn
$_=int 275*$F[0]/9-30+$F[1]-2%$F[0]
Excel, 30 24 bytes
=DATE(1,m,d)-DATE(1,1,0)[24]=DAYS(DATE(1,m,d),DATE(1,1,0))[30]
Edit: I used the year 2001 before, because I do not trust Excel with dates before 1905.
JavaScript (Node.js), 35 32 bytes
m=>d=>--m*31+d-'003344555667'[m]
Thanks to A username for pro golf tips!
Python 3, 60 55 51 bytes, 1-based month
lambda function that accepts the month (1-based) and the day.
-5 bytes: used a "31-days" default month, and the list s accounts for the cumulate (absolute) difference between the actual days of months and 31
-4 bytes: the list is removed from the function parameters, thanks to @Hunaphu and @mic_e
lambda m,d,a=[3,0,3,2,3,2,3,3,2,3,2]:28*(m-1)+sum(a[:m-1])+d # original version
lambda m,d,s=[0,0,3,3,4,4,5,5,5,6,6,7]:31*m-31-s[m-1]+d
lambda m,d:31*m-31-[0,0,3,3,4,4,5,5,5,6,6,7][m-1]+d
Explanation (original version):
m,d: input month and daya[...]: list of days to add to the "base" 28-day month, from january to novemeber, passed as optional argument28*(m-1): the total number of days in the previous "28-days" months+sum(a[:m-1]): add the remaining days of the previous months (as difference actual days - 28)+d: add the days of the selected month
Python 3, 56 50 46 bytes, 0-based month
-4 bytes (compared to 1-based month number): if 0-based month number is allowed as input
-6 bytes: used the "31-days" approach
-4 bytes: the list is removed from the function parameters, thanks to @Hunaphu and @mic_e
lambda m,d,a=[3,0,3,2,3,2,3,3,2,3,2]:28*(m)+sum(a[:m])+d # original version
lambda m,d,s=[0,0,3,3,4,4,5,5,5,6,6,7]:31*m-s[m]+d
lambda m,d:31*m-[0,0,3,3,4,4,5,5,5,6,6,7][m]+d
Python 3, 41 bytes
lambda m,d:31*~-m-(539785049600>>3*m&7)+d
Python 3, 41 chars, 50 bytes
lambda m,d:d+ord('0\x00>v´ðĮŪƨǦȢɠʜ'[m])/2
Excel formula, 18 Bytes
=DATE(1,A1,B1)-366
- Cell
A1contains the input month. - Cell
B1contains the input day.
Saved 8 bytes from answer by Crissov by evaluating the offset as 366, then added 2 bytes by converting named ranges m and d into cell references.
If leap years were allowed, then it could be shortened to a single function =DATE(0,A1,B1)
PHP, 42 bytes
fn($m,$d)=>date(z,mktime(0,0,0,$m,$d,1))+1
Pretty much builtin. The year parameter could be omitted, but the current year would then be used. To avoid a leap year 1 was used (it actually corresponds to 2001 according to the doc). 1 is added because the result is zero indexed.
This is satisfying, the byte count is The Answer, which is also the answer to the test case!
1 byte shorter but less satisfying:
PHP, 41 bytes
fn($m,$d)=>date(z,strtotime("1-$m-$d"))+1
R, 57 bytes
(my own attempt)
function(m,d){F[c(8:14,2:8)]=30:31;F[3]=28;sum(F[1:m],d)}
An alternative R solution to pajonk's answer, without using any date built-ins.
R, 34 bytes
(port of Lynn's answer)
function(m,d)(275*m)%/%9-30+d-2%%m
This works on my PowerShell 5 on my computer; I can't get it working on TIO:
PowerShell 5, 97 bytes
function s($m,$d){1+(New-Timespan -st (Get-Date -day 1 -mo 1) -e (Get-Date -day $d -mo $m)).Days}
Call as s 2 11 for the example date (month before day-of-month).
Golfed by @mazzy, 82 bytes
PowerShell, 82 bytes
param($m,$d)1+(New-Timespan -st (Date -day 1 -mo 1) -e (Date -day $d -mo $m)).Days
The golfing relies on an alias or implementation of command (Date) that does not exist in a default Windows 10 installation of PowerShell 5. The TIO PowerShell is PowerShell 6 on Linux.
R, 40 bytes
function(m,d)format(ISOdate(1,m,d),"%j")
Using buildins, returning as string.
Returning as a number (also using buildins):
R, 44 bytes
function(m,d)as.POSIXlt(ISOdate(1,m,d))$yd+1
Desmos, 90 82 59 bytes
f(m,d)=total([0,31,28,31,30,31,30,31,31,30,31,30][1...m])+d
Saved 23 bytes 'cause I was so dumb... I've been trying to do some clever list manipulation to save bytes, but it never came to my mind to just put the list itself.
Very Brief Explanation:
[0,31,28,31,30,31,30,31,31,30,31,30]: The number of days in each month(excluding December), with an extra 0 element at the beginning.
total( ... [1...m])+d: Sum of the first m elements of the list explained above, then add d.
Try It On Desmos! - Prettified
Solution using Lynn's "closed form" formula, 34 bytes
f(m,d)=floor(275m/9)-30+d-mod(2,m)
Haskell, 27 bytes
m%d=div(275*m)9-30+d-mod 2m
This is a “closed-form” answer (in a C-like language it would be 275*m/9-30+d-2%m).
Factor, 29 bytes
[ 1 -rot <date> day-of-year ]
1 -rot <date>Create a timestamp from the year 1 and the two inputs. (1 is not a leap year according to Factor's calendar vocabulary.) This is the shortest way I know of to create a timestamp object from a month and day.day-of-yearFactor has a builtin for this once you have a timestamp.
Gaia, 6 bytes
A dyadic function taking the month above the day. Gaia has a nice collection of date/time builtins.
(∂k<Σ+
( # decrement the month
∂k # push list [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] (builtin)
< # take the first month-1 elements from this list
Σ # sum them
+ # add the day
Japt, 16 bytes
Well, this ain't right :\ Normally Japt would be ruling the roost in a date based challenge but not this time, despite all the tricks I can muster. Which makes me seriously worried that, after 16 long months, I've lost my edge when it comes to golfing on me phone over a few pints down the boozer!
ÒÐBì)nÐUi¹z864e5
Jelly, 16 15 bytes
“LɓịNH’D+28⁸;ḣS
Takes \$d\$ then \$m\$ on the command line
“LɓịNH’D+28⁸;ḣS - Main link. Takes d on the left and m on the right
“LɓịNH’ - Compressed integer; 303232332323
D - Convert to digits; [3, 0, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3]
+28 - Plus 28 to each; [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
⁸; - Prepend d; [d, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
ḣ - Take the first m
S - Sum
Vyxal s, 14 bytes
‹»HȦð9E»fẎ28+p
Try it Online! Port of @caird's Jelly solution
‹»HȦð9E»fẎ28+p
»HȦð9E» Push compressed integer 303232332323
f Convert to digits
‹ Ẏ Slice [0:m-1]
28+ Add 28 to each list element
p Prepend to the day of the month
s flag: Sums the top of the stack and outputs the sum.
MATL, 9 bytes
14Liq:)hs
Explanation
14L % Push [31 28 31 30 31 30 31 31 30 31 30 31]: month lengths (predefined literal)
i % Input: month, m
q % Subtract 1
: % Inclusive range from 1 to that
) % Index into the array of month lengths: gives its first m-1 terms
h % Implicit input: day, d. Concatenate with previous array
s % Sum of array. Implicit display
Bash, 16
date -d$1/1 +%-j
Input is given on the command-line as slash-separated integers, with month first.
JavaScript (ES6), 40 37 36 bytes
Expects (day)(month).
d=>g=m=>--m?31-(4/m&2)+~m%9%2+g(m):d
How?
This is a recursive function that computes the number of days in each full month (i.e. lower than \$m\$), sums them all together and finally adds \$d\$ on the last iteration.
We use 4 / m & 2 to distinguish between February and all other months:
- For January,
4 / 1 & 2is4 & 2, which is \$0\$ - For February,
4 / 2 & 2is2 & 2, which is \$2\$ - For \$m>2\$,
4 / m & 2is \$0\$ because \$0<4/m<2\$
We use ~m % 9 % 2 to subtract \$1\$ for months that do not have \$31\$ days:
m | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
~m | -2 | -3 | -4 | -5 | -6 | -7 | -8 | -9 | -10 | -11 | -12
mod 9 | -2 | -3 | -4 | -5 | -6 | -7 | -8 | 0 | -1 | -2 | -3
mod 2 | 0 | -1 | 0 | -1 | 0 | -1 | 0 | 0 | -1 | 0 | -1
(This also works for December, but we never have to compute the total number of days in this month.)
Charcoal, 20 bytes
I⁺↨E…”)“M⟧₂”⊖N⁻³¹ι¹N
Try it online! Link is to verbose version of code. Explanation:
”)“M⟧₂” Compressed string `03010100101`
… Truncated to length
N Month as an integer
⊖ Decremented
E Map over characters
³¹ Literal integer `31`
⁻ Subtract
ι Current value
↨ ¹ Take the sum
⁺ Plus
N Day as an integer
I Cast to string
Implicitly print
Base 1 conversion is used in case the list is empty (i.e. January).
APL (Dyalog Unicode), 11 bytes
Full program. Prompts for [month,day]
1⎕DT⊂1900,⎕
Try it on TryAPL! (⎕ is stdin, but is emulated with the variable ∆ since TryAPL doesn't allow stdin)
⎕ prompt for [month,day]; [2,11]
1900, prepend 1900; [1900,2,11]
⊂ enclose to represent as scalar time stamp; [[1900,2,11]]
1⎕DT convert DateTime to days since 1899-12-31; 42
Java, 141 bytes
int d(int d,int m){for(int i=1;i<m;i++){switch(i){case(2):d+=28;break;case(4):case(6):case(9):case(11):d+=30;break;default:d+=31;}}return d;}
