Get the date of the nth day of week in a given year and month
Introduction
Often, people refer to dates as the "second Friday in August, 2018" or the "fourth Sunday in March, 2012". But it's hard to tell what date that is! Your task to is to write a program that receives a year, a month, a day of the week, and an integer, and output that date.
Challenge
For input, you will get a year, a month, a day of week, and a number.
You can take input in any reasonable format, like using a string for the day of week or using a zero indexed weekday, or even take the year and month in a single string. Do explain your input format in your answer, though.
The integer that tells you which day of week in the month to target will be an integer from 1-5. The integer will never refer to a day of week that does not exist(e.g. the fifth Friday of February 2019, which doesn't exist).
Years will always be positive.
Your output can be in any reasonable format, including printing your final date. However, please explain your output format un your answer.
Providing the year and month in the output is optional. Also, you may assume the date is valid.
Example Input and Output
Consider this input, with the format being taking in the year as a 4 digit number, month as an integer, day of week as string, and the ordinal number as an integer:
2019, 3, Saturday, 2
2019, 12, Sunday, 1
2019, 9 Saturday, 1
Output:
March 9
December 1
September 7
This is code-golf, so shortest answer wins.
code-golf date
|
show 1 more comment
Introduction
Often, people refer to dates as the "second Friday in August, 2018" or the "fourth Sunday in March, 2012". But it's hard to tell what date that is! Your task to is to write a program that receives a year, a month, a day of the week, and an integer, and output that date.
Challenge
For input, you will get a year, a month, a day of week, and a number.
You can take input in any reasonable format, like using a string for the day of week or using a zero indexed weekday, or even take the year and month in a single string. Do explain your input format in your answer, though.
The integer that tells you which day of week in the month to target will be an integer from 1-5. The integer will never refer to a day of week that does not exist(e.g. the fifth Friday of February 2019, which doesn't exist).
Years will always be positive.
Your output can be in any reasonable format, including printing your final date. However, please explain your output format un your answer.
Providing the year and month in the output is optional. Also, you may assume the date is valid.
Example Input and Output
Consider this input, with the format being taking in the year as a 4 digit number, month as an integer, day of week as string, and the ordinal number as an integer:
2019, 3, Saturday, 2
2019, 12, Sunday, 1
2019, 9 Saturday, 1
Output:
March 9
December 1
September 7
This is code-golf, so shortest answer wins.
code-golf date
1
I thought I might be able to use GNUdate
, but amusingly, the parser has an... interesting interpretation here.
– Doorknob♦
Jan 1 at 20:44
May week number be 0-indexed?
– Jo King
Jan 2 at 1:52
Sure, if it suits your program
– Embodiment of Ignorance
Jan 2 at 2:12
Suggested test case: 2018, 12, Sunday 1
– Adám
Jan 2 at 10:55
Suggested test case: the first Saturday in September 2019
– Shaggy
Jan 2 at 12:07
|
show 1 more comment
Introduction
Often, people refer to dates as the "second Friday in August, 2018" or the "fourth Sunday in March, 2012". But it's hard to tell what date that is! Your task to is to write a program that receives a year, a month, a day of the week, and an integer, and output that date.
Challenge
For input, you will get a year, a month, a day of week, and a number.
You can take input in any reasonable format, like using a string for the day of week or using a zero indexed weekday, or even take the year and month in a single string. Do explain your input format in your answer, though.
The integer that tells you which day of week in the month to target will be an integer from 1-5. The integer will never refer to a day of week that does not exist(e.g. the fifth Friday of February 2019, which doesn't exist).
Years will always be positive.
Your output can be in any reasonable format, including printing your final date. However, please explain your output format un your answer.
Providing the year and month in the output is optional. Also, you may assume the date is valid.
Example Input and Output
Consider this input, with the format being taking in the year as a 4 digit number, month as an integer, day of week as string, and the ordinal number as an integer:
2019, 3, Saturday, 2
2019, 12, Sunday, 1
2019, 9 Saturday, 1
Output:
March 9
December 1
September 7
This is code-golf, so shortest answer wins.
code-golf date
Introduction
Often, people refer to dates as the "second Friday in August, 2018" or the "fourth Sunday in March, 2012". But it's hard to tell what date that is! Your task to is to write a program that receives a year, a month, a day of the week, and an integer, and output that date.
Challenge
For input, you will get a year, a month, a day of week, and a number.
You can take input in any reasonable format, like using a string for the day of week or using a zero indexed weekday, or even take the year and month in a single string. Do explain your input format in your answer, though.
The integer that tells you which day of week in the month to target will be an integer from 1-5. The integer will never refer to a day of week that does not exist(e.g. the fifth Friday of February 2019, which doesn't exist).
Years will always be positive.
Your output can be in any reasonable format, including printing your final date. However, please explain your output format un your answer.
Providing the year and month in the output is optional. Also, you may assume the date is valid.
Example Input and Output
Consider this input, with the format being taking in the year as a 4 digit number, month as an integer, day of week as string, and the ordinal number as an integer:
2019, 3, Saturday, 2
2019, 12, Sunday, 1
2019, 9 Saturday, 1
Output:
March 9
December 1
September 7
This is code-golf, so shortest answer wins.
code-golf date
code-golf date
edited Jan 3 at 10:20
Kevin Cruijssen
36.1k554190
36.1k554190
asked Jan 1 at 20:28
Embodiment of IgnoranceEmbodiment of Ignorance
596115
596115
1
I thought I might be able to use GNUdate
, but amusingly, the parser has an... interesting interpretation here.
– Doorknob♦
Jan 1 at 20:44
May week number be 0-indexed?
– Jo King
Jan 2 at 1:52
Sure, if it suits your program
– Embodiment of Ignorance
Jan 2 at 2:12
Suggested test case: 2018, 12, Sunday 1
– Adám
Jan 2 at 10:55
Suggested test case: the first Saturday in September 2019
– Shaggy
Jan 2 at 12:07
|
show 1 more comment
1
I thought I might be able to use GNUdate
, but amusingly, the parser has an... interesting interpretation here.
– Doorknob♦
Jan 1 at 20:44
May week number be 0-indexed?
– Jo King
Jan 2 at 1:52
Sure, if it suits your program
– Embodiment of Ignorance
Jan 2 at 2:12
Suggested test case: 2018, 12, Sunday 1
– Adám
Jan 2 at 10:55
Suggested test case: the first Saturday in September 2019
– Shaggy
Jan 2 at 12:07
1
1
I thought I might be able to use GNU
date
, but amusingly, the parser has an... interesting interpretation here.– Doorknob♦
Jan 1 at 20:44
I thought I might be able to use GNU
date
, but amusingly, the parser has an... interesting interpretation here.– Doorknob♦
Jan 1 at 20:44
May week number be 0-indexed?
– Jo King
Jan 2 at 1:52
May week number be 0-indexed?
– Jo King
Jan 2 at 1:52
Sure, if it suits your program
– Embodiment of Ignorance
Jan 2 at 2:12
Sure, if it suits your program
– Embodiment of Ignorance
Jan 2 at 2:12
Suggested test case: 2018, 12, Sunday 1
– Adám
Jan 2 at 10:55
Suggested test case: 2018, 12, Sunday 1
– Adám
Jan 2 at 10:55
Suggested test case: the first Saturday in September 2019
– Shaggy
Jan 2 at 12:07
Suggested test case: the first Saturday in September 2019
– Shaggy
Jan 2 at 12:07
|
show 1 more comment
16 Answers
16
active
oldest
votes
MediaWiki Template, 19 bytes
{{#time:r|{{{1}}}}}
This is a MediaWiki Template ParserFunctions port of this PHP answer
Note: #time
use PHP strtotime
internally.
Sample Input
{{#time:r|second saturday of March 2019}}
Sample Output
Sat, 09 Mar 2019 00:00:00 +0000
add a comment |
Japt, 19 15 bytes
Input is: year, 0-based index of month, 0-based index of day of the week (0
is Sunday) & n
.
_XµW¶Ze}f@ÐUVYÄ
Try it
:Implicit input of U=year, V=month, W=weekday & X=n
}f :Output the first element generated by the right function that returns
: falsey (0) when passed through the left function
@ :Right function. Y=0-based index of current iteration
ÐUVYÄ : new Date(U,V,Y+1)
_ :Left function. Z=date being passed
Xµ : Decrement X by
W¶ : Test W for equality with
Ze : Z.getDay()
add a comment |
C# (Visual C# Interactive Compiler), 59 bytes
(y,m,d,w)=>1+(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+7*w
Try it online!
-27 bytes thanks to @EmbodimentOfIgnorance!
Less golfed code...
// y: year
// m: month
// d: day of week (0 is Sunday)
// w: week number (0 based)
(y,m,d,w)=>
// start on the first day of the month
1+
// determine the number of days to
// the first occurrence of the
// requested weekday
(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+
// add the number of requested weeks
7*w
The return value is an integer for the day of the month of the requested date.
If you only return the day of the month, which is allowed, you can golf this down to 59 bytes
– Embodiment of Ignorance
Jan 2 at 3:04
Thanks for the tip again - (that's 2x today ;) I will update my answers.
– dana
Jan 2 at 4:14
add a comment |
SmileBASIC, 58 51 48 46 45 bytes
READ N,M$DTREAD M$OUT,,D,W?(5-W+D*2)MOD 7+N*7
Input is in the form: week,year/month/weekday
week
: week number, 0-indexed
year
: year, 4 digits
month
: month (1-indexed), 2 digits0
-padded
weekday
: day of the week (1-indexed, 1=Sunday), 2 digits0
-padded
Output is the day of the month, 0 indexed.
The 2nd Saturday in March 2019 would be 1,2019/03/07
→ 8
(March 9th)
Ungolfed
READ WEEK,M$
DTREAD M$ OUT ,,D,W
'Functions in SB can output multiple values using OUT.
'In this case, we don't need the first 2 values, so no variables are given.
PRINT (5-W+ D*2) MOD 7 +WEEK*7
Explanation
The input form was specifically chosen so that the year/month/weekday could be passed directly to DTREAD
, which is a function which parses a YYYY/MM/DD
string and returns the year/month/day as numbers, as well as calculating the day of the week.
However, notice that, where DTREAD
expects the day of the month, we're giving it the day of the week instead. This complicates things, but also means that there are fewer input values and we don't need to add /01
to the end of the date string.
DTREAD
outputs W
and D
.W
is the weekday, and D
is the day-of-the-week of the W
th day of the month.
(So, if you input 7
as the weekday, D
will be 7
and W
will be whatever day-of-the-week the 7th day of the month is)
The expression (5-W+D*2)MOD 7
is used to get the 1st occurrence of the input weekday, as a 0-indexed day of the month. (I figured this one out mostly through trial and error)
After that, the program just adds WEEK*7
.
I really wish there were separate words for "day of the week" and "day of the month".
add a comment |
Perl 6, 52 48 bytes
{1+($^c+4-Date.new($^a,$^b,1).daycount)%7+$^d*7}
Try it online!
Anonymous code block that takes input as year, month, day of week (Sunday is 0) and week number (0 indexed). Output is a date object.
Old Explanation:
{ } # Anonymous code block
( Date.new( )) # Create a new date with
$^a,$^b,1 # With year, month and the first day
$_= # Save in the variable $_
+ # Add to this
$^c+4 # The given day plus 4
( -.daycount) # The number of days since 17 Nov, 1858
%7 # Modulo 7 to get the correct day
+$^d*7 # Plus the 7 times the number of weeks
I think the week number needs to be 1-indexed
– 12Me21
Jan 2 at 1:37
@12Me21 The question says that input is flexible, but I've asked whether we an take week number as 0 indexed
– Jo King
Jan 2 at 1:55
This doesn't seem to work for other months. I think you need$^c+4-$!.daycount
.
– nwellnhof
Jan 2 at 2:24
@nwellnhof Ah, I should have thought of that instead of trial and error, lol. Should be fixed now
– Jo King
Jan 2 at 3:00
add a comment |
MATL, 28 27 bytes
'1 'ihYO31:q+t8XOi=!A)i)1XO
This uses three inputs:
- String with month and year:
'March 2019'
- String with three letters, first capitalized, indicating day of the week: '
Sat
' - Number:
2
.
Output is a string with day, year and month separated with dashes: 09-Mar-2019
.
Try it online!
Explanation
Consider inputs 'March 2019'
, 'Sat'
, 2
.
'1 ' % Push this string
% STACK: '1 '
ih % Input string: month and year. Concatenate
% STACK: '1 March 2019'
YO % Convert to serial date number
% STACK: 737485
31:q+ % Create vector [0 1 2 ... 30] and add element-wise
% STACK: [737485 737486 737487 ... 737515]
t8XO % Duplicate. Convert to date format 'ddd': day of week in three letters. Gives
% a 2D char array
% STACK: [737485 737486 737487 ... 737515], ['Fri'; 'Sat'; 'Sun'; ...; 'Sun']
i= % Input string: day of week in three letters. Compare element-wise with broadcast
% STACK: [737485 737486 737487 ... 737515],
% [0 0 0; 0 0 0; ...; 1 1 1; 0 0 0; ... 1 1 1; ...]
!A % True for rows containing only 1
% STACK: [737485 737486 737487 ... 737515], [0 0 ... 1 ... 0 ... 1 ...]
) % Index: uses the second vector as a mask into the first
% STACK: [737486 737493 737500 737507 737514]
i) % Input number. Index
% STACK: 737493
1XO % Convert to date format 'dd-mmm-yyyy'. Implicit display
% STACK: '09-Mar-2019'
add a comment |
Python 3, 92 82 bytes
82 bytes thanks to @Jo King
lambda y,m,d,w:z(y,m)[w-(z(y,m)[0][d]>0)][d]
from calendar import*
z=monthcalendar
Try it online!
Original version, 92 bytes
from calendar import*
def f(y,m,d,w):
x=monthcalendar(y,m)
if x[0][d]:w-=1
return x[w][d]
Try it online!
Takes the year as an integer, month as a 1-indexed integer, day of the week as a 0-index integer where Monday is 0
and Sunday is 6
, and week of the month as a 1-indexed integer.
How it works:
# import monthcalendar
from calendar import*
# function with 4 inputs
def f(y,m,d,w):
# get a 2-D array representing the specified month
# each week is represented by an array
# and the value of each element is its date of the month
# Monday is the 0th element of each week, Sunday is the 6th element
# days of the first week of the month before the 1st are 0s
x=monthcalendar(y,m)
# if the first week of the month includes the desired day of week
# convert the week of month to 0-index so that it takes into account the first week
if x[0][d]:w-=1
# return the weekday of the week number specified
return x[w][d]
add a comment |
R, 72 69 bytes
function(y,m,d,n,D=as.Date(paste0(y,-m,-1))+0:31)D[weekdays(D)==d][n]
Try it online!
Taking input as :
- Year number
- Month number (1-12)
- Weekday string in the current locale (TIO requires english name with capital letter)
- Ordinal number (1-indexed)
add a comment |
PHP, 46, 43, 31 bytes
<?=date(d,strtotime($argv[1]));
Try it online!
The program receives as input a string like "second saturday of March 2019"
The program prints the day number.
-12 bytes thanks to Shaggy.
1
31 bytes
– Shaggy
Jan 1 at 23:42
Also, I don't think you need theof
in the input.
– Shaggy
Jan 1 at 23:58
@Shaggy, I think these are details. There may be many options.
– Кирилл Малышев
Jan 2 at 0:00
add a comment |
Groovy, 46 bytes
{y,m,d,w->r=new Date(y,m,7*w)
r-(r.day+7-d)%7}
Try it online!
Input as years since 1900, 0-indexed month, 0-indexed day (Sunday being 0) and week number
add a comment |
Scala, 86 bytes
(y,m,d,w)=>{var r=new java.util.Date(y,m,7*w)
r.setDate(r.getDate-(r.getDay+7-d)%7)
r}
Try it online!
add a comment |
Red, 64 60 bytes
func[y m d n][a: to-date[1 m y]d - a/10 - 7 % 7 +(n * 7)+ a]
Try it online!
Takes the year, the month and the weekday as numbers, 1-indexed, Monday is the first day of the week.
huh,to-date
is a thing. maybe this would be useful in the christmase eve format challenge
– ASCII-only
Jan 2 at 9:18
@ASCII-only We usednow
for that challenge
– Galen Ivanov
Jan 2 at 9:21
1
yeah, but i was thinking of alternative methods (namely, the <repeat" Eve"
date2-date1 times> method iirc)
– ASCII-only
Jan 2 at 9:21
@ASCII-only>> 25-Dec-2019 - 1-1-2019 == 358
– Galen Ivanov
Jan 2 at 9:24
add a comment |
APL (Dyalog Unicode), 36 bytesSBCS
Full program. Prompts for [year,month]
(January is 1), then for day (Sunday is 0), then for n (first is 1).
date⎕⊃d/⍨⎕=7|d←(⍳31)+days⎕⊣⎕CY'dfns'
Try it online!
⎕CY'dfns'
copy in the dfns library
⊣
discard the result of that in favour of…
⎕
prompt console for [year,month]
numbers
days
[c] days[n] since 1899-12-31 of the 0th of that month
(⍳31)+
add the integers 1…31 to that
d←
store in d
(for day numbers)
7|
division remainder when dividing by 7 (day-of-week with Sunday being 0 due to good epoch)
⎕=
prompt console for day-of-week number and get mask where equal the day-of-week numbers
d/⍨
filter d
with that mask
⎕⊃
prompt console for n and use that to pick from the list of day numbers
date
[c] datetime stamp[n] (has trailing zeros for hours, minutes, seconds, milliseconds)
Click [c] for code and [n] for notes.
add a comment |
JavaScript (ES6), 49 48 bytes
f=
(a,d,n)=>(d+6-new Date(...a,7).getDay())%7+n*7-6
<div oninput=o.textContent=f([+y.value,+m.value],+d.value,+n.value)>Year: <input id=y type=number value=2019><br>Month: <select id=m><option value=0>Jan<option value=1>Feb<option value=2>Mar<option value=3>Apr<option value=4>May<option value=5>Jun<option value=6>Jul<option value=7>Aug<option value=8>Sep<option value=9>Oct<option value=10>Nov<option value=11>Dec</select><br>Day: <select id=d><option value=0>Sun<option value=1>Mon<option value=2>Tue<option value=3>Wed<option value=4>Thu<option value=5>Fri<option value=6>Sat</select><br>Count: <input id=n type=number value=1 min=1 max=5><pre id=o>
Takes parameters as f([year, month], day, number)
. Month and day of week (starting on Sunday) are zero-indexed. Edit: Saved 1 byte thanks to @Shaggy.
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
Jan 1 at 22:48
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
Jan 1 at 22:51
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
Jan 1 at 22:52
Oh. I didn't know that, I'm not that familiar with Javascript.
– Embodiment of Ignorance
Jan 1 at 22:52
48 bytes
– Shaggy
Jan 2 at 10:51
add a comment |
TSQL, 106 bytes
DECLARE @ datetime='2019',@m int=3,@w char(2)='saturday',@p int=2
SELECT dateadd(wk,datediff(d,x,dateadd(m,@m-1,@)-1)/7,x)+@p*7FROM(SELECT
charindex(@w,' tuwethfrsasu')/2x)x
Output:
2019-03-09 00:00:00.000
Try it out
add a comment |
Kotlin, 131 84 bytes
47 bytes thanks to ASCII-only's code and comment.
Input: year, monthNumber, weekdayNumber, weekNumber
All integers 1 to maximum on single line. Week day number is Sunday of 1 to Saturday of 7. The main program makes the year lose 1900 for Date class and month and weekday are shifted to start with zero instead of one before calling the lambda. You can enter your arguments in the input text box to try your own dates.
Output: a Date class instance. The main program displays the result like: Sat Mar 09 00:00:00 UTC 2019.
There is an expanded version with comments explaining the code for those wishing to learn more.
Note: the import command required for the lambda to work can't be coded there so I replaced the first two characters with the start of an end of line comment in the "Try it online!".
{y,m,d,w->val r=java.util.Date(y,m,7*w)
r.setDate(r.getDate()-(r.getDay()+7-d)%7)
r}
Try it online!
1
74 - uses same method as Scala and Groovy answers
– ASCII-only
2 days ago
1
Fixed, 84
– ASCII-only
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178227%2fget-the-date-of-the-nth-day-of-week-in-a-given-year-and-month%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
16 Answers
16
active
oldest
votes
16 Answers
16
active
oldest
votes
active
oldest
votes
active
oldest
votes
MediaWiki Template, 19 bytes
{{#time:r|{{{1}}}}}
This is a MediaWiki Template ParserFunctions port of this PHP answer
Note: #time
use PHP strtotime
internally.
Sample Input
{{#time:r|second saturday of March 2019}}
Sample Output
Sat, 09 Mar 2019 00:00:00 +0000
add a comment |
MediaWiki Template, 19 bytes
{{#time:r|{{{1}}}}}
This is a MediaWiki Template ParserFunctions port of this PHP answer
Note: #time
use PHP strtotime
internally.
Sample Input
{{#time:r|second saturday of March 2019}}
Sample Output
Sat, 09 Mar 2019 00:00:00 +0000
add a comment |
MediaWiki Template, 19 bytes
{{#time:r|{{{1}}}}}
This is a MediaWiki Template ParserFunctions port of this PHP answer
Note: #time
use PHP strtotime
internally.
Sample Input
{{#time:r|second saturday of March 2019}}
Sample Output
Sat, 09 Mar 2019 00:00:00 +0000
MediaWiki Template, 19 bytes
{{#time:r|{{{1}}}}}
This is a MediaWiki Template ParserFunctions port of this PHP answer
Note: #time
use PHP strtotime
internally.
Sample Input
{{#time:r|second saturday of March 2019}}
Sample Output
Sat, 09 Mar 2019 00:00:00 +0000
answered Jan 2 at 1:30
tshtsh
8,48511546
8,48511546
add a comment |
add a comment |
Japt, 19 15 bytes
Input is: year, 0-based index of month, 0-based index of day of the week (0
is Sunday) & n
.
_XµW¶Ze}f@ÐUVYÄ
Try it
:Implicit input of U=year, V=month, W=weekday & X=n
}f :Output the first element generated by the right function that returns
: falsey (0) when passed through the left function
@ :Right function. Y=0-based index of current iteration
ÐUVYÄ : new Date(U,V,Y+1)
_ :Left function. Z=date being passed
Xµ : Decrement X by
W¶ : Test W for equality with
Ze : Z.getDay()
add a comment |
Japt, 19 15 bytes
Input is: year, 0-based index of month, 0-based index of day of the week (0
is Sunday) & n
.
_XµW¶Ze}f@ÐUVYÄ
Try it
:Implicit input of U=year, V=month, W=weekday & X=n
}f :Output the first element generated by the right function that returns
: falsey (0) when passed through the left function
@ :Right function. Y=0-based index of current iteration
ÐUVYÄ : new Date(U,V,Y+1)
_ :Left function. Z=date being passed
Xµ : Decrement X by
W¶ : Test W for equality with
Ze : Z.getDay()
add a comment |
Japt, 19 15 bytes
Input is: year, 0-based index of month, 0-based index of day of the week (0
is Sunday) & n
.
_XµW¶Ze}f@ÐUVYÄ
Try it
:Implicit input of U=year, V=month, W=weekday & X=n
}f :Output the first element generated by the right function that returns
: falsey (0) when passed through the left function
@ :Right function. Y=0-based index of current iteration
ÐUVYÄ : new Date(U,V,Y+1)
_ :Left function. Z=date being passed
Xµ : Decrement X by
W¶ : Test W for equality with
Ze : Z.getDay()
Japt, 19 15 bytes
Input is: year, 0-based index of month, 0-based index of day of the week (0
is Sunday) & n
.
_XµW¶Ze}f@ÐUVYÄ
Try it
:Implicit input of U=year, V=month, W=weekday & X=n
}f :Output the first element generated by the right function that returns
: falsey (0) when passed through the left function
@ :Right function. Y=0-based index of current iteration
ÐUVYÄ : new Date(U,V,Y+1)
_ :Left function. Z=date being passed
Xµ : Decrement X by
W¶ : Test W for equality with
Ze : Z.getDay()
edited Jan 2 at 11:16
answered Jan 1 at 22:58
ShaggyShaggy
19.2k21666
19.2k21666
add a comment |
add a comment |
C# (Visual C# Interactive Compiler), 59 bytes
(y,m,d,w)=>1+(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+7*w
Try it online!
-27 bytes thanks to @EmbodimentOfIgnorance!
Less golfed code...
// y: year
// m: month
// d: day of week (0 is Sunday)
// w: week number (0 based)
(y,m,d,w)=>
// start on the first day of the month
1+
// determine the number of days to
// the first occurrence of the
// requested weekday
(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+
// add the number of requested weeks
7*w
The return value is an integer for the day of the month of the requested date.
If you only return the day of the month, which is allowed, you can golf this down to 59 bytes
– Embodiment of Ignorance
Jan 2 at 3:04
Thanks for the tip again - (that's 2x today ;) I will update my answers.
– dana
Jan 2 at 4:14
add a comment |
C# (Visual C# Interactive Compiler), 59 bytes
(y,m,d,w)=>1+(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+7*w
Try it online!
-27 bytes thanks to @EmbodimentOfIgnorance!
Less golfed code...
// y: year
// m: month
// d: day of week (0 is Sunday)
// w: week number (0 based)
(y,m,d,w)=>
// start on the first day of the month
1+
// determine the number of days to
// the first occurrence of the
// requested weekday
(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+
// add the number of requested weeks
7*w
The return value is an integer for the day of the month of the requested date.
If you only return the day of the month, which is allowed, you can golf this down to 59 bytes
– Embodiment of Ignorance
Jan 2 at 3:04
Thanks for the tip again - (that's 2x today ;) I will update my answers.
– dana
Jan 2 at 4:14
add a comment |
C# (Visual C# Interactive Compiler), 59 bytes
(y,m,d,w)=>1+(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+7*w
Try it online!
-27 bytes thanks to @EmbodimentOfIgnorance!
Less golfed code...
// y: year
// m: month
// d: day of week (0 is Sunday)
// w: week number (0 based)
(y,m,d,w)=>
// start on the first day of the month
1+
// determine the number of days to
// the first occurrence of the
// requested weekday
(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+
// add the number of requested weeks
7*w
The return value is an integer for the day of the month of the requested date.
C# (Visual C# Interactive Compiler), 59 bytes
(y,m,d,w)=>1+(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+7*w
Try it online!
-27 bytes thanks to @EmbodimentOfIgnorance!
Less golfed code...
// y: year
// m: month
// d: day of week (0 is Sunday)
// w: week number (0 based)
(y,m,d,w)=>
// start on the first day of the month
1+
// determine the number of days to
// the first occurrence of the
// requested weekday
(d-(int)new DateTime(y,m,1).DayOfWeek+7)%7+
// add the number of requested weeks
7*w
The return value is an integer for the day of the month of the requested date.
edited Jan 2 at 4:55
answered Jan 2 at 0:42
danadana
55135
55135
If you only return the day of the month, which is allowed, you can golf this down to 59 bytes
– Embodiment of Ignorance
Jan 2 at 3:04
Thanks for the tip again - (that's 2x today ;) I will update my answers.
– dana
Jan 2 at 4:14
add a comment |
If you only return the day of the month, which is allowed, you can golf this down to 59 bytes
– Embodiment of Ignorance
Jan 2 at 3:04
Thanks for the tip again - (that's 2x today ;) I will update my answers.
– dana
Jan 2 at 4:14
If you only return the day of the month, which is allowed, you can golf this down to 59 bytes
– Embodiment of Ignorance
Jan 2 at 3:04
If you only return the day of the month, which is allowed, you can golf this down to 59 bytes
– Embodiment of Ignorance
Jan 2 at 3:04
Thanks for the tip again - (that's 2x today ;) I will update my answers.
– dana
Jan 2 at 4:14
Thanks for the tip again - (that's 2x today ;) I will update my answers.
– dana
Jan 2 at 4:14
add a comment |
SmileBASIC, 58 51 48 46 45 bytes
READ N,M$DTREAD M$OUT,,D,W?(5-W+D*2)MOD 7+N*7
Input is in the form: week,year/month/weekday
week
: week number, 0-indexed
year
: year, 4 digits
month
: month (1-indexed), 2 digits0
-padded
weekday
: day of the week (1-indexed, 1=Sunday), 2 digits0
-padded
Output is the day of the month, 0 indexed.
The 2nd Saturday in March 2019 would be 1,2019/03/07
→ 8
(March 9th)
Ungolfed
READ WEEK,M$
DTREAD M$ OUT ,,D,W
'Functions in SB can output multiple values using OUT.
'In this case, we don't need the first 2 values, so no variables are given.
PRINT (5-W+ D*2) MOD 7 +WEEK*7
Explanation
The input form was specifically chosen so that the year/month/weekday could be passed directly to DTREAD
, which is a function which parses a YYYY/MM/DD
string and returns the year/month/day as numbers, as well as calculating the day of the week.
However, notice that, where DTREAD
expects the day of the month, we're giving it the day of the week instead. This complicates things, but also means that there are fewer input values and we don't need to add /01
to the end of the date string.
DTREAD
outputs W
and D
.W
is the weekday, and D
is the day-of-the-week of the W
th day of the month.
(So, if you input 7
as the weekday, D
will be 7
and W
will be whatever day-of-the-week the 7th day of the month is)
The expression (5-W+D*2)MOD 7
is used to get the 1st occurrence of the input weekday, as a 0-indexed day of the month. (I figured this one out mostly through trial and error)
After that, the program just adds WEEK*7
.
I really wish there were separate words for "day of the week" and "day of the month".
add a comment |
SmileBASIC, 58 51 48 46 45 bytes
READ N,M$DTREAD M$OUT,,D,W?(5-W+D*2)MOD 7+N*7
Input is in the form: week,year/month/weekday
week
: week number, 0-indexed
year
: year, 4 digits
month
: month (1-indexed), 2 digits0
-padded
weekday
: day of the week (1-indexed, 1=Sunday), 2 digits0
-padded
Output is the day of the month, 0 indexed.
The 2nd Saturday in March 2019 would be 1,2019/03/07
→ 8
(March 9th)
Ungolfed
READ WEEK,M$
DTREAD M$ OUT ,,D,W
'Functions in SB can output multiple values using OUT.
'In this case, we don't need the first 2 values, so no variables are given.
PRINT (5-W+ D*2) MOD 7 +WEEK*7
Explanation
The input form was specifically chosen so that the year/month/weekday could be passed directly to DTREAD
, which is a function which parses a YYYY/MM/DD
string and returns the year/month/day as numbers, as well as calculating the day of the week.
However, notice that, where DTREAD
expects the day of the month, we're giving it the day of the week instead. This complicates things, but also means that there are fewer input values and we don't need to add /01
to the end of the date string.
DTREAD
outputs W
and D
.W
is the weekday, and D
is the day-of-the-week of the W
th day of the month.
(So, if you input 7
as the weekday, D
will be 7
and W
will be whatever day-of-the-week the 7th day of the month is)
The expression (5-W+D*2)MOD 7
is used to get the 1st occurrence of the input weekday, as a 0-indexed day of the month. (I figured this one out mostly through trial and error)
After that, the program just adds WEEK*7
.
I really wish there were separate words for "day of the week" and "day of the month".
add a comment |
SmileBASIC, 58 51 48 46 45 bytes
READ N,M$DTREAD M$OUT,,D,W?(5-W+D*2)MOD 7+N*7
Input is in the form: week,year/month/weekday
week
: week number, 0-indexed
year
: year, 4 digits
month
: month (1-indexed), 2 digits0
-padded
weekday
: day of the week (1-indexed, 1=Sunday), 2 digits0
-padded
Output is the day of the month, 0 indexed.
The 2nd Saturday in March 2019 would be 1,2019/03/07
→ 8
(March 9th)
Ungolfed
READ WEEK,M$
DTREAD M$ OUT ,,D,W
'Functions in SB can output multiple values using OUT.
'In this case, we don't need the first 2 values, so no variables are given.
PRINT (5-W+ D*2) MOD 7 +WEEK*7
Explanation
The input form was specifically chosen so that the year/month/weekday could be passed directly to DTREAD
, which is a function which parses a YYYY/MM/DD
string and returns the year/month/day as numbers, as well as calculating the day of the week.
However, notice that, where DTREAD
expects the day of the month, we're giving it the day of the week instead. This complicates things, but also means that there are fewer input values and we don't need to add /01
to the end of the date string.
DTREAD
outputs W
and D
.W
is the weekday, and D
is the day-of-the-week of the W
th day of the month.
(So, if you input 7
as the weekday, D
will be 7
and W
will be whatever day-of-the-week the 7th day of the month is)
The expression (5-W+D*2)MOD 7
is used to get the 1st occurrence of the input weekday, as a 0-indexed day of the month. (I figured this one out mostly through trial and error)
After that, the program just adds WEEK*7
.
I really wish there were separate words for "day of the week" and "day of the month".
SmileBASIC, 58 51 48 46 45 bytes
READ N,M$DTREAD M$OUT,,D,W?(5-W+D*2)MOD 7+N*7
Input is in the form: week,year/month/weekday
week
: week number, 0-indexed
year
: year, 4 digits
month
: month (1-indexed), 2 digits0
-padded
weekday
: day of the week (1-indexed, 1=Sunday), 2 digits0
-padded
Output is the day of the month, 0 indexed.
The 2nd Saturday in March 2019 would be 1,2019/03/07
→ 8
(March 9th)
Ungolfed
READ WEEK,M$
DTREAD M$ OUT ,,D,W
'Functions in SB can output multiple values using OUT.
'In this case, we don't need the first 2 values, so no variables are given.
PRINT (5-W+ D*2) MOD 7 +WEEK*7
Explanation
The input form was specifically chosen so that the year/month/weekday could be passed directly to DTREAD
, which is a function which parses a YYYY/MM/DD
string and returns the year/month/day as numbers, as well as calculating the day of the week.
However, notice that, where DTREAD
expects the day of the month, we're giving it the day of the week instead. This complicates things, but also means that there are fewer input values and we don't need to add /01
to the end of the date string.
DTREAD
outputs W
and D
.W
is the weekday, and D
is the day-of-the-week of the W
th day of the month.
(So, if you input 7
as the weekday, D
will be 7
and W
will be whatever day-of-the-week the 7th day of the month is)
The expression (5-W+D*2)MOD 7
is used to get the 1st occurrence of the input weekday, as a 0-indexed day of the month. (I figured this one out mostly through trial and error)
After that, the program just adds WEEK*7
.
I really wish there were separate words for "day of the week" and "day of the month".
edited Jan 2 at 16:37
answered Jan 1 at 22:36
12Me2112Me21
5,48711236
5,48711236
add a comment |
add a comment |
Perl 6, 52 48 bytes
{1+($^c+4-Date.new($^a,$^b,1).daycount)%7+$^d*7}
Try it online!
Anonymous code block that takes input as year, month, day of week (Sunday is 0) and week number (0 indexed). Output is a date object.
Old Explanation:
{ } # Anonymous code block
( Date.new( )) # Create a new date with
$^a,$^b,1 # With year, month and the first day
$_= # Save in the variable $_
+ # Add to this
$^c+4 # The given day plus 4
( -.daycount) # The number of days since 17 Nov, 1858
%7 # Modulo 7 to get the correct day
+$^d*7 # Plus the 7 times the number of weeks
I think the week number needs to be 1-indexed
– 12Me21
Jan 2 at 1:37
@12Me21 The question says that input is flexible, but I've asked whether we an take week number as 0 indexed
– Jo King
Jan 2 at 1:55
This doesn't seem to work for other months. I think you need$^c+4-$!.daycount
.
– nwellnhof
Jan 2 at 2:24
@nwellnhof Ah, I should have thought of that instead of trial and error, lol. Should be fixed now
– Jo King
Jan 2 at 3:00
add a comment |
Perl 6, 52 48 bytes
{1+($^c+4-Date.new($^a,$^b,1).daycount)%7+$^d*7}
Try it online!
Anonymous code block that takes input as year, month, day of week (Sunday is 0) and week number (0 indexed). Output is a date object.
Old Explanation:
{ } # Anonymous code block
( Date.new( )) # Create a new date with
$^a,$^b,1 # With year, month and the first day
$_= # Save in the variable $_
+ # Add to this
$^c+4 # The given day plus 4
( -.daycount) # The number of days since 17 Nov, 1858
%7 # Modulo 7 to get the correct day
+$^d*7 # Plus the 7 times the number of weeks
I think the week number needs to be 1-indexed
– 12Me21
Jan 2 at 1:37
@12Me21 The question says that input is flexible, but I've asked whether we an take week number as 0 indexed
– Jo King
Jan 2 at 1:55
This doesn't seem to work for other months. I think you need$^c+4-$!.daycount
.
– nwellnhof
Jan 2 at 2:24
@nwellnhof Ah, I should have thought of that instead of trial and error, lol. Should be fixed now
– Jo King
Jan 2 at 3:00
add a comment |
Perl 6, 52 48 bytes
{1+($^c+4-Date.new($^a,$^b,1).daycount)%7+$^d*7}
Try it online!
Anonymous code block that takes input as year, month, day of week (Sunday is 0) and week number (0 indexed). Output is a date object.
Old Explanation:
{ } # Anonymous code block
( Date.new( )) # Create a new date with
$^a,$^b,1 # With year, month and the first day
$_= # Save in the variable $_
+ # Add to this
$^c+4 # The given day plus 4
( -.daycount) # The number of days since 17 Nov, 1858
%7 # Modulo 7 to get the correct day
+$^d*7 # Plus the 7 times the number of weeks
Perl 6, 52 48 bytes
{1+($^c+4-Date.new($^a,$^b,1).daycount)%7+$^d*7}
Try it online!
Anonymous code block that takes input as year, month, day of week (Sunday is 0) and week number (0 indexed). Output is a date object.
Old Explanation:
{ } # Anonymous code block
( Date.new( )) # Create a new date with
$^a,$^b,1 # With year, month and the first day
$_= # Save in the variable $_
+ # Add to this
$^c+4 # The given day plus 4
( -.daycount) # The number of days since 17 Nov, 1858
%7 # Modulo 7 to get the correct day
+$^d*7 # Plus the 7 times the number of weeks
edited 2 days ago
answered Jan 2 at 1:09
Jo KingJo King
21.1k248110
21.1k248110
I think the week number needs to be 1-indexed
– 12Me21
Jan 2 at 1:37
@12Me21 The question says that input is flexible, but I've asked whether we an take week number as 0 indexed
– Jo King
Jan 2 at 1:55
This doesn't seem to work for other months. I think you need$^c+4-$!.daycount
.
– nwellnhof
Jan 2 at 2:24
@nwellnhof Ah, I should have thought of that instead of trial and error, lol. Should be fixed now
– Jo King
Jan 2 at 3:00
add a comment |
I think the week number needs to be 1-indexed
– 12Me21
Jan 2 at 1:37
@12Me21 The question says that input is flexible, but I've asked whether we an take week number as 0 indexed
– Jo King
Jan 2 at 1:55
This doesn't seem to work for other months. I think you need$^c+4-$!.daycount
.
– nwellnhof
Jan 2 at 2:24
@nwellnhof Ah, I should have thought of that instead of trial and error, lol. Should be fixed now
– Jo King
Jan 2 at 3:00
I think the week number needs to be 1-indexed
– 12Me21
Jan 2 at 1:37
I think the week number needs to be 1-indexed
– 12Me21
Jan 2 at 1:37
@12Me21 The question says that input is flexible, but I've asked whether we an take week number as 0 indexed
– Jo King
Jan 2 at 1:55
@12Me21 The question says that input is flexible, but I've asked whether we an take week number as 0 indexed
– Jo King
Jan 2 at 1:55
This doesn't seem to work for other months. I think you need
$^c+4-$!.daycount
.– nwellnhof
Jan 2 at 2:24
This doesn't seem to work for other months. I think you need
$^c+4-$!.daycount
.– nwellnhof
Jan 2 at 2:24
@nwellnhof Ah, I should have thought of that instead of trial and error, lol. Should be fixed now
– Jo King
Jan 2 at 3:00
@nwellnhof Ah, I should have thought of that instead of trial and error, lol. Should be fixed now
– Jo King
Jan 2 at 3:00
add a comment |
MATL, 28 27 bytes
'1 'ihYO31:q+t8XOi=!A)i)1XO
This uses three inputs:
- String with month and year:
'March 2019'
- String with three letters, first capitalized, indicating day of the week: '
Sat
' - Number:
2
.
Output is a string with day, year and month separated with dashes: 09-Mar-2019
.
Try it online!
Explanation
Consider inputs 'March 2019'
, 'Sat'
, 2
.
'1 ' % Push this string
% STACK: '1 '
ih % Input string: month and year. Concatenate
% STACK: '1 March 2019'
YO % Convert to serial date number
% STACK: 737485
31:q+ % Create vector [0 1 2 ... 30] and add element-wise
% STACK: [737485 737486 737487 ... 737515]
t8XO % Duplicate. Convert to date format 'ddd': day of week in three letters. Gives
% a 2D char array
% STACK: [737485 737486 737487 ... 737515], ['Fri'; 'Sat'; 'Sun'; ...; 'Sun']
i= % Input string: day of week in three letters. Compare element-wise with broadcast
% STACK: [737485 737486 737487 ... 737515],
% [0 0 0; 0 0 0; ...; 1 1 1; 0 0 0; ... 1 1 1; ...]
!A % True for rows containing only 1
% STACK: [737485 737486 737487 ... 737515], [0 0 ... 1 ... 0 ... 1 ...]
) % Index: uses the second vector as a mask into the first
% STACK: [737486 737493 737500 737507 737514]
i) % Input number. Index
% STACK: 737493
1XO % Convert to date format 'dd-mmm-yyyy'. Implicit display
% STACK: '09-Mar-2019'
add a comment |
MATL, 28 27 bytes
'1 'ihYO31:q+t8XOi=!A)i)1XO
This uses three inputs:
- String with month and year:
'March 2019'
- String with three letters, first capitalized, indicating day of the week: '
Sat
' - Number:
2
.
Output is a string with day, year and month separated with dashes: 09-Mar-2019
.
Try it online!
Explanation
Consider inputs 'March 2019'
, 'Sat'
, 2
.
'1 ' % Push this string
% STACK: '1 '
ih % Input string: month and year. Concatenate
% STACK: '1 March 2019'
YO % Convert to serial date number
% STACK: 737485
31:q+ % Create vector [0 1 2 ... 30] and add element-wise
% STACK: [737485 737486 737487 ... 737515]
t8XO % Duplicate. Convert to date format 'ddd': day of week in three letters. Gives
% a 2D char array
% STACK: [737485 737486 737487 ... 737515], ['Fri'; 'Sat'; 'Sun'; ...; 'Sun']
i= % Input string: day of week in three letters. Compare element-wise with broadcast
% STACK: [737485 737486 737487 ... 737515],
% [0 0 0; 0 0 0; ...; 1 1 1; 0 0 0; ... 1 1 1; ...]
!A % True for rows containing only 1
% STACK: [737485 737486 737487 ... 737515], [0 0 ... 1 ... 0 ... 1 ...]
) % Index: uses the second vector as a mask into the first
% STACK: [737486 737493 737500 737507 737514]
i) % Input number. Index
% STACK: 737493
1XO % Convert to date format 'dd-mmm-yyyy'. Implicit display
% STACK: '09-Mar-2019'
add a comment |
MATL, 28 27 bytes
'1 'ihYO31:q+t8XOi=!A)i)1XO
This uses three inputs:
- String with month and year:
'March 2019'
- String with three letters, first capitalized, indicating day of the week: '
Sat
' - Number:
2
.
Output is a string with day, year and month separated with dashes: 09-Mar-2019
.
Try it online!
Explanation
Consider inputs 'March 2019'
, 'Sat'
, 2
.
'1 ' % Push this string
% STACK: '1 '
ih % Input string: month and year. Concatenate
% STACK: '1 March 2019'
YO % Convert to serial date number
% STACK: 737485
31:q+ % Create vector [0 1 2 ... 30] and add element-wise
% STACK: [737485 737486 737487 ... 737515]
t8XO % Duplicate. Convert to date format 'ddd': day of week in three letters. Gives
% a 2D char array
% STACK: [737485 737486 737487 ... 737515], ['Fri'; 'Sat'; 'Sun'; ...; 'Sun']
i= % Input string: day of week in three letters. Compare element-wise with broadcast
% STACK: [737485 737486 737487 ... 737515],
% [0 0 0; 0 0 0; ...; 1 1 1; 0 0 0; ... 1 1 1; ...]
!A % True for rows containing only 1
% STACK: [737485 737486 737487 ... 737515], [0 0 ... 1 ... 0 ... 1 ...]
) % Index: uses the second vector as a mask into the first
% STACK: [737486 737493 737500 737507 737514]
i) % Input number. Index
% STACK: 737493
1XO % Convert to date format 'dd-mmm-yyyy'. Implicit display
% STACK: '09-Mar-2019'
MATL, 28 27 bytes
'1 'ihYO31:q+t8XOi=!A)i)1XO
This uses three inputs:
- String with month and year:
'March 2019'
- String with three letters, first capitalized, indicating day of the week: '
Sat
' - Number:
2
.
Output is a string with day, year and month separated with dashes: 09-Mar-2019
.
Try it online!
Explanation
Consider inputs 'March 2019'
, 'Sat'
, 2
.
'1 ' % Push this string
% STACK: '1 '
ih % Input string: month and year. Concatenate
% STACK: '1 March 2019'
YO % Convert to serial date number
% STACK: 737485
31:q+ % Create vector [0 1 2 ... 30] and add element-wise
% STACK: [737485 737486 737487 ... 737515]
t8XO % Duplicate. Convert to date format 'ddd': day of week in three letters. Gives
% a 2D char array
% STACK: [737485 737486 737487 ... 737515], ['Fri'; 'Sat'; 'Sun'; ...; 'Sun']
i= % Input string: day of week in three letters. Compare element-wise with broadcast
% STACK: [737485 737486 737487 ... 737515],
% [0 0 0; 0 0 0; ...; 1 1 1; 0 0 0; ... 1 1 1; ...]
!A % True for rows containing only 1
% STACK: [737485 737486 737487 ... 737515], [0 0 ... 1 ... 0 ... 1 ...]
) % Index: uses the second vector as a mask into the first
% STACK: [737486 737493 737500 737507 737514]
i) % Input number. Index
% STACK: 737493
1XO % Convert to date format 'dd-mmm-yyyy'. Implicit display
% STACK: '09-Mar-2019'
edited Jan 1 at 23:29
answered Jan 1 at 22:00
Luis MendoLuis Mendo
74k886291
74k886291
add a comment |
add a comment |
Python 3, 92 82 bytes
82 bytes thanks to @Jo King
lambda y,m,d,w:z(y,m)[w-(z(y,m)[0][d]>0)][d]
from calendar import*
z=monthcalendar
Try it online!
Original version, 92 bytes
from calendar import*
def f(y,m,d,w):
x=monthcalendar(y,m)
if x[0][d]:w-=1
return x[w][d]
Try it online!
Takes the year as an integer, month as a 1-indexed integer, day of the week as a 0-index integer where Monday is 0
and Sunday is 6
, and week of the month as a 1-indexed integer.
How it works:
# import monthcalendar
from calendar import*
# function with 4 inputs
def f(y,m,d,w):
# get a 2-D array representing the specified month
# each week is represented by an array
# and the value of each element is its date of the month
# Monday is the 0th element of each week, Sunday is the 6th element
# days of the first week of the month before the 1st are 0s
x=monthcalendar(y,m)
# if the first week of the month includes the desired day of week
# convert the week of month to 0-index so that it takes into account the first week
if x[0][d]:w-=1
# return the weekday of the week number specified
return x[w][d]
add a comment |
Python 3, 92 82 bytes
82 bytes thanks to @Jo King
lambda y,m,d,w:z(y,m)[w-(z(y,m)[0][d]>0)][d]
from calendar import*
z=monthcalendar
Try it online!
Original version, 92 bytes
from calendar import*
def f(y,m,d,w):
x=monthcalendar(y,m)
if x[0][d]:w-=1
return x[w][d]
Try it online!
Takes the year as an integer, month as a 1-indexed integer, day of the week as a 0-index integer where Monday is 0
and Sunday is 6
, and week of the month as a 1-indexed integer.
How it works:
# import monthcalendar
from calendar import*
# function with 4 inputs
def f(y,m,d,w):
# get a 2-D array representing the specified month
# each week is represented by an array
# and the value of each element is its date of the month
# Monday is the 0th element of each week, Sunday is the 6th element
# days of the first week of the month before the 1st are 0s
x=monthcalendar(y,m)
# if the first week of the month includes the desired day of week
# convert the week of month to 0-index so that it takes into account the first week
if x[0][d]:w-=1
# return the weekday of the week number specified
return x[w][d]
add a comment |
Python 3, 92 82 bytes
82 bytes thanks to @Jo King
lambda y,m,d,w:z(y,m)[w-(z(y,m)[0][d]>0)][d]
from calendar import*
z=monthcalendar
Try it online!
Original version, 92 bytes
from calendar import*
def f(y,m,d,w):
x=monthcalendar(y,m)
if x[0][d]:w-=1
return x[w][d]
Try it online!
Takes the year as an integer, month as a 1-indexed integer, day of the week as a 0-index integer where Monday is 0
and Sunday is 6
, and week of the month as a 1-indexed integer.
How it works:
# import monthcalendar
from calendar import*
# function with 4 inputs
def f(y,m,d,w):
# get a 2-D array representing the specified month
# each week is represented by an array
# and the value of each element is its date of the month
# Monday is the 0th element of each week, Sunday is the 6th element
# days of the first week of the month before the 1st are 0s
x=monthcalendar(y,m)
# if the first week of the month includes the desired day of week
# convert the week of month to 0-index so that it takes into account the first week
if x[0][d]:w-=1
# return the weekday of the week number specified
return x[w][d]
Python 3, 92 82 bytes
82 bytes thanks to @Jo King
lambda y,m,d,w:z(y,m)[w-(z(y,m)[0][d]>0)][d]
from calendar import*
z=monthcalendar
Try it online!
Original version, 92 bytes
from calendar import*
def f(y,m,d,w):
x=monthcalendar(y,m)
if x[0][d]:w-=1
return x[w][d]
Try it online!
Takes the year as an integer, month as a 1-indexed integer, day of the week as a 0-index integer where Monday is 0
and Sunday is 6
, and week of the month as a 1-indexed integer.
How it works:
# import monthcalendar
from calendar import*
# function with 4 inputs
def f(y,m,d,w):
# get a 2-D array representing the specified month
# each week is represented by an array
# and the value of each element is its date of the month
# Monday is the 0th element of each week, Sunday is the 6th element
# days of the first week of the month before the 1st are 0s
x=monthcalendar(y,m)
# if the first week of the month includes the desired day of week
# convert the week of month to 0-index so that it takes into account the first week
if x[0][d]:w-=1
# return the weekday of the week number specified
return x[w][d]
edited Jan 2 at 22:32
answered Jan 2 at 21:39
Neil A.Neil A.
1,218120
1,218120
add a comment |
add a comment |
R, 72 69 bytes
function(y,m,d,n,D=as.Date(paste0(y,-m,-1))+0:31)D[weekdays(D)==d][n]
Try it online!
Taking input as :
- Year number
- Month number (1-12)
- Weekday string in the current locale (TIO requires english name with capital letter)
- Ordinal number (1-indexed)
add a comment |
R, 72 69 bytes
function(y,m,d,n,D=as.Date(paste0(y,-m,-1))+0:31)D[weekdays(D)==d][n]
Try it online!
Taking input as :
- Year number
- Month number (1-12)
- Weekday string in the current locale (TIO requires english name with capital letter)
- Ordinal number (1-indexed)
add a comment |
R, 72 69 bytes
function(y,m,d,n,D=as.Date(paste0(y,-m,-1))+0:31)D[weekdays(D)==d][n]
Try it online!
Taking input as :
- Year number
- Month number (1-12)
- Weekday string in the current locale (TIO requires english name with capital letter)
- Ordinal number (1-indexed)
R, 72 69 bytes
function(y,m,d,n,D=as.Date(paste0(y,-m,-1))+0:31)D[weekdays(D)==d][n]
Try it online!
Taking input as :
- Year number
- Month number (1-12)
- Weekday string in the current locale (TIO requires english name with capital letter)
- Ordinal number (1-indexed)
edited Jan 3 at 10:46
answered Jan 2 at 13:07
digEmAlldigEmAll
2,664413
2,664413
add a comment |
add a comment |
PHP, 46, 43, 31 bytes
<?=date(d,strtotime($argv[1]));
Try it online!
The program receives as input a string like "second saturday of March 2019"
The program prints the day number.
-12 bytes thanks to Shaggy.
1
31 bytes
– Shaggy
Jan 1 at 23:42
Also, I don't think you need theof
in the input.
– Shaggy
Jan 1 at 23:58
@Shaggy, I think these are details. There may be many options.
– Кирилл Малышев
Jan 2 at 0:00
add a comment |
PHP, 46, 43, 31 bytes
<?=date(d,strtotime($argv[1]));
Try it online!
The program receives as input a string like "second saturday of March 2019"
The program prints the day number.
-12 bytes thanks to Shaggy.
1
31 bytes
– Shaggy
Jan 1 at 23:42
Also, I don't think you need theof
in the input.
– Shaggy
Jan 1 at 23:58
@Shaggy, I think these are details. There may be many options.
– Кирилл Малышев
Jan 2 at 0:00
add a comment |
PHP, 46, 43, 31 bytes
<?=date(d,strtotime($argv[1]));
Try it online!
The program receives as input a string like "second saturday of March 2019"
The program prints the day number.
-12 bytes thanks to Shaggy.
PHP, 46, 43, 31 bytes
<?=date(d,strtotime($argv[1]));
Try it online!
The program receives as input a string like "second saturday of March 2019"
The program prints the day number.
-12 bytes thanks to Shaggy.
edited Jan 1 at 23:48
answered Jan 1 at 21:04
Кирилл МалышевКирилл Малышев
40115
40115
1
31 bytes
– Shaggy
Jan 1 at 23:42
Also, I don't think you need theof
in the input.
– Shaggy
Jan 1 at 23:58
@Shaggy, I think these are details. There may be many options.
– Кирилл Малышев
Jan 2 at 0:00
add a comment |
1
31 bytes
– Shaggy
Jan 1 at 23:42
Also, I don't think you need theof
in the input.
– Shaggy
Jan 1 at 23:58
@Shaggy, I think these are details. There may be many options.
– Кирилл Малышев
Jan 2 at 0:00
1
1
31 bytes
– Shaggy
Jan 1 at 23:42
31 bytes
– Shaggy
Jan 1 at 23:42
Also, I don't think you need the
of
in the input.– Shaggy
Jan 1 at 23:58
Also, I don't think you need the
of
in the input.– Shaggy
Jan 1 at 23:58
@Shaggy, I think these are details. There may be many options.
– Кирилл Малышев
Jan 2 at 0:00
@Shaggy, I think these are details. There may be many options.
– Кирилл Малышев
Jan 2 at 0:00
add a comment |
Groovy, 46 bytes
{y,m,d,w->r=new Date(y,m,7*w)
r-(r.day+7-d)%7}
Try it online!
Input as years since 1900, 0-indexed month, 0-indexed day (Sunday being 0) and week number
add a comment |
Groovy, 46 bytes
{y,m,d,w->r=new Date(y,m,7*w)
r-(r.day+7-d)%7}
Try it online!
Input as years since 1900, 0-indexed month, 0-indexed day (Sunday being 0) and week number
add a comment |
Groovy, 46 bytes
{y,m,d,w->r=new Date(y,m,7*w)
r-(r.day+7-d)%7}
Try it online!
Input as years since 1900, 0-indexed month, 0-indexed day (Sunday being 0) and week number
Groovy, 46 bytes
{y,m,d,w->r=new Date(y,m,7*w)
r-(r.day+7-d)%7}
Try it online!
Input as years since 1900, 0-indexed month, 0-indexed day (Sunday being 0) and week number
answered Jan 2 at 8:13
ASCII-onlyASCII-only
3,2341136
3,2341136
add a comment |
add a comment |
Scala, 86 bytes
(y,m,d,w)=>{var r=new java.util.Date(y,m,7*w)
r.setDate(r.getDate-(r.getDay+7-d)%7)
r}
Try it online!
add a comment |
Scala, 86 bytes
(y,m,d,w)=>{var r=new java.util.Date(y,m,7*w)
r.setDate(r.getDate-(r.getDay+7-d)%7)
r}
Try it online!
add a comment |
Scala, 86 bytes
(y,m,d,w)=>{var r=new java.util.Date(y,m,7*w)
r.setDate(r.getDate-(r.getDay+7-d)%7)
r}
Try it online!
Scala, 86 bytes
(y,m,d,w)=>{var r=new java.util.Date(y,m,7*w)
r.setDate(r.getDate-(r.getDay+7-d)%7)
r}
Try it online!
answered Jan 2 at 8:43
ASCII-onlyASCII-only
3,2341136
3,2341136
add a comment |
add a comment |
Red, 64 60 bytes
func[y m d n][a: to-date[1 m y]d - a/10 - 7 % 7 +(n * 7)+ a]
Try it online!
Takes the year, the month and the weekday as numbers, 1-indexed, Monday is the first day of the week.
huh,to-date
is a thing. maybe this would be useful in the christmase eve format challenge
– ASCII-only
Jan 2 at 9:18
@ASCII-only We usednow
for that challenge
– Galen Ivanov
Jan 2 at 9:21
1
yeah, but i was thinking of alternative methods (namely, the <repeat" Eve"
date2-date1 times> method iirc)
– ASCII-only
Jan 2 at 9:21
@ASCII-only>> 25-Dec-2019 - 1-1-2019 == 358
– Galen Ivanov
Jan 2 at 9:24
add a comment |
Red, 64 60 bytes
func[y m d n][a: to-date[1 m y]d - a/10 - 7 % 7 +(n * 7)+ a]
Try it online!
Takes the year, the month and the weekday as numbers, 1-indexed, Monday is the first day of the week.
huh,to-date
is a thing. maybe this would be useful in the christmase eve format challenge
– ASCII-only
Jan 2 at 9:18
@ASCII-only We usednow
for that challenge
– Galen Ivanov
Jan 2 at 9:21
1
yeah, but i was thinking of alternative methods (namely, the <repeat" Eve"
date2-date1 times> method iirc)
– ASCII-only
Jan 2 at 9:21
@ASCII-only>> 25-Dec-2019 - 1-1-2019 == 358
– Galen Ivanov
Jan 2 at 9:24
add a comment |
Red, 64 60 bytes
func[y m d n][a: to-date[1 m y]d - a/10 - 7 % 7 +(n * 7)+ a]
Try it online!
Takes the year, the month and the weekday as numbers, 1-indexed, Monday is the first day of the week.
Red, 64 60 bytes
func[y m d n][a: to-date[1 m y]d - a/10 - 7 % 7 +(n * 7)+ a]
Try it online!
Takes the year, the month and the weekday as numbers, 1-indexed, Monday is the first day of the week.
edited Jan 2 at 8:51
answered Jan 2 at 8:36
Galen IvanovGalen Ivanov
6,43711032
6,43711032
huh,to-date
is a thing. maybe this would be useful in the christmase eve format challenge
– ASCII-only
Jan 2 at 9:18
@ASCII-only We usednow
for that challenge
– Galen Ivanov
Jan 2 at 9:21
1
yeah, but i was thinking of alternative methods (namely, the <repeat" Eve"
date2-date1 times> method iirc)
– ASCII-only
Jan 2 at 9:21
@ASCII-only>> 25-Dec-2019 - 1-1-2019 == 358
– Galen Ivanov
Jan 2 at 9:24
add a comment |
huh,to-date
is a thing. maybe this would be useful in the christmase eve format challenge
– ASCII-only
Jan 2 at 9:18
@ASCII-only We usednow
for that challenge
– Galen Ivanov
Jan 2 at 9:21
1
yeah, but i was thinking of alternative methods (namely, the <repeat" Eve"
date2-date1 times> method iirc)
– ASCII-only
Jan 2 at 9:21
@ASCII-only>> 25-Dec-2019 - 1-1-2019 == 358
– Galen Ivanov
Jan 2 at 9:24
huh,
to-date
is a thing. maybe this would be useful in the christmase eve format challenge– ASCII-only
Jan 2 at 9:18
huh,
to-date
is a thing. maybe this would be useful in the christmase eve format challenge– ASCII-only
Jan 2 at 9:18
@ASCII-only We used
now
for that challenge– Galen Ivanov
Jan 2 at 9:21
@ASCII-only We used
now
for that challenge– Galen Ivanov
Jan 2 at 9:21
1
1
yeah, but i was thinking of alternative methods (namely, the <repeat
" Eve"
date2-date1 times> method iirc)– ASCII-only
Jan 2 at 9:21
yeah, but i was thinking of alternative methods (namely, the <repeat
" Eve"
date2-date1 times> method iirc)– ASCII-only
Jan 2 at 9:21
@ASCII-only
>> 25-Dec-2019 - 1-1-2019 == 358
– Galen Ivanov
Jan 2 at 9:24
@ASCII-only
>> 25-Dec-2019 - 1-1-2019 == 358
– Galen Ivanov
Jan 2 at 9:24
add a comment |
APL (Dyalog Unicode), 36 bytesSBCS
Full program. Prompts for [year,month]
(January is 1), then for day (Sunday is 0), then for n (first is 1).
date⎕⊃d/⍨⎕=7|d←(⍳31)+days⎕⊣⎕CY'dfns'
Try it online!
⎕CY'dfns'
copy in the dfns library
⊣
discard the result of that in favour of…
⎕
prompt console for [year,month]
numbers
days
[c] days[n] since 1899-12-31 of the 0th of that month
(⍳31)+
add the integers 1…31 to that
d←
store in d
(for day numbers)
7|
division remainder when dividing by 7 (day-of-week with Sunday being 0 due to good epoch)
⎕=
prompt console for day-of-week number and get mask where equal the day-of-week numbers
d/⍨
filter d
with that mask
⎕⊃
prompt console for n and use that to pick from the list of day numbers
date
[c] datetime stamp[n] (has trailing zeros for hours, minutes, seconds, milliseconds)
Click [c] for code and [n] for notes.
add a comment |
APL (Dyalog Unicode), 36 bytesSBCS
Full program. Prompts for [year,month]
(January is 1), then for day (Sunday is 0), then for n (first is 1).
date⎕⊃d/⍨⎕=7|d←(⍳31)+days⎕⊣⎕CY'dfns'
Try it online!
⎕CY'dfns'
copy in the dfns library
⊣
discard the result of that in favour of…
⎕
prompt console for [year,month]
numbers
days
[c] days[n] since 1899-12-31 of the 0th of that month
(⍳31)+
add the integers 1…31 to that
d←
store in d
(for day numbers)
7|
division remainder when dividing by 7 (day-of-week with Sunday being 0 due to good epoch)
⎕=
prompt console for day-of-week number and get mask where equal the day-of-week numbers
d/⍨
filter d
with that mask
⎕⊃
prompt console for n and use that to pick from the list of day numbers
date
[c] datetime stamp[n] (has trailing zeros for hours, minutes, seconds, milliseconds)
Click [c] for code and [n] for notes.
add a comment |
APL (Dyalog Unicode), 36 bytesSBCS
Full program. Prompts for [year,month]
(January is 1), then for day (Sunday is 0), then for n (first is 1).
date⎕⊃d/⍨⎕=7|d←(⍳31)+days⎕⊣⎕CY'dfns'
Try it online!
⎕CY'dfns'
copy in the dfns library
⊣
discard the result of that in favour of…
⎕
prompt console for [year,month]
numbers
days
[c] days[n] since 1899-12-31 of the 0th of that month
(⍳31)+
add the integers 1…31 to that
d←
store in d
(for day numbers)
7|
division remainder when dividing by 7 (day-of-week with Sunday being 0 due to good epoch)
⎕=
prompt console for day-of-week number and get mask where equal the day-of-week numbers
d/⍨
filter d
with that mask
⎕⊃
prompt console for n and use that to pick from the list of day numbers
date
[c] datetime stamp[n] (has trailing zeros for hours, minutes, seconds, milliseconds)
Click [c] for code and [n] for notes.
APL (Dyalog Unicode), 36 bytesSBCS
Full program. Prompts for [year,month]
(January is 1), then for day (Sunday is 0), then for n (first is 1).
date⎕⊃d/⍨⎕=7|d←(⍳31)+days⎕⊣⎕CY'dfns'
Try it online!
⎕CY'dfns'
copy in the dfns library
⊣
discard the result of that in favour of…
⎕
prompt console for [year,month]
numbers
days
[c] days[n] since 1899-12-31 of the 0th of that month
(⍳31)+
add the integers 1…31 to that
d←
store in d
(for day numbers)
7|
division remainder when dividing by 7 (day-of-week with Sunday being 0 due to good epoch)
⎕=
prompt console for day-of-week number and get mask where equal the day-of-week numbers
d/⍨
filter d
with that mask
⎕⊃
prompt console for n and use that to pick from the list of day numbers
date
[c] datetime stamp[n] (has trailing zeros for hours, minutes, seconds, milliseconds)
Click [c] for code and [n] for notes.
answered Jan 2 at 10:52
AdámAdám
29k269192
29k269192
add a comment |
add a comment |
JavaScript (ES6), 49 48 bytes
f=
(a,d,n)=>(d+6-new Date(...a,7).getDay())%7+n*7-6
<div oninput=o.textContent=f([+y.value,+m.value],+d.value,+n.value)>Year: <input id=y type=number value=2019><br>Month: <select id=m><option value=0>Jan<option value=1>Feb<option value=2>Mar<option value=3>Apr<option value=4>May<option value=5>Jun<option value=6>Jul<option value=7>Aug<option value=8>Sep<option value=9>Oct<option value=10>Nov<option value=11>Dec</select><br>Day: <select id=d><option value=0>Sun<option value=1>Mon<option value=2>Tue<option value=3>Wed<option value=4>Thu<option value=5>Fri<option value=6>Sat</select><br>Count: <input id=n type=number value=1 min=1 max=5><pre id=o>
Takes parameters as f([year, month], day, number)
. Month and day of week (starting on Sunday) are zero-indexed. Edit: Saved 1 byte thanks to @Shaggy.
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
Jan 1 at 22:48
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
Jan 1 at 22:51
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
Jan 1 at 22:52
Oh. I didn't know that, I'm not that familiar with Javascript.
– Embodiment of Ignorance
Jan 1 at 22:52
48 bytes
– Shaggy
Jan 2 at 10:51
add a comment |
JavaScript (ES6), 49 48 bytes
f=
(a,d,n)=>(d+6-new Date(...a,7).getDay())%7+n*7-6
<div oninput=o.textContent=f([+y.value,+m.value],+d.value,+n.value)>Year: <input id=y type=number value=2019><br>Month: <select id=m><option value=0>Jan<option value=1>Feb<option value=2>Mar<option value=3>Apr<option value=4>May<option value=5>Jun<option value=6>Jul<option value=7>Aug<option value=8>Sep<option value=9>Oct<option value=10>Nov<option value=11>Dec</select><br>Day: <select id=d><option value=0>Sun<option value=1>Mon<option value=2>Tue<option value=3>Wed<option value=4>Thu<option value=5>Fri<option value=6>Sat</select><br>Count: <input id=n type=number value=1 min=1 max=5><pre id=o>
Takes parameters as f([year, month], day, number)
. Month and day of week (starting on Sunday) are zero-indexed. Edit: Saved 1 byte thanks to @Shaggy.
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
Jan 1 at 22:48
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
Jan 1 at 22:51
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
Jan 1 at 22:52
Oh. I didn't know that, I'm not that familiar with Javascript.
– Embodiment of Ignorance
Jan 1 at 22:52
48 bytes
– Shaggy
Jan 2 at 10:51
add a comment |
JavaScript (ES6), 49 48 bytes
f=
(a,d,n)=>(d+6-new Date(...a,7).getDay())%7+n*7-6
<div oninput=o.textContent=f([+y.value,+m.value],+d.value,+n.value)>Year: <input id=y type=number value=2019><br>Month: <select id=m><option value=0>Jan<option value=1>Feb<option value=2>Mar<option value=3>Apr<option value=4>May<option value=5>Jun<option value=6>Jul<option value=7>Aug<option value=8>Sep<option value=9>Oct<option value=10>Nov<option value=11>Dec</select><br>Day: <select id=d><option value=0>Sun<option value=1>Mon<option value=2>Tue<option value=3>Wed<option value=4>Thu<option value=5>Fri<option value=6>Sat</select><br>Count: <input id=n type=number value=1 min=1 max=5><pre id=o>
Takes parameters as f([year, month], day, number)
. Month and day of week (starting on Sunday) are zero-indexed. Edit: Saved 1 byte thanks to @Shaggy.
JavaScript (ES6), 49 48 bytes
f=
(a,d,n)=>(d+6-new Date(...a,7).getDay())%7+n*7-6
<div oninput=o.textContent=f([+y.value,+m.value],+d.value,+n.value)>Year: <input id=y type=number value=2019><br>Month: <select id=m><option value=0>Jan<option value=1>Feb<option value=2>Mar<option value=3>Apr<option value=4>May<option value=5>Jun<option value=6>Jul<option value=7>Aug<option value=8>Sep<option value=9>Oct<option value=10>Nov<option value=11>Dec</select><br>Day: <select id=d><option value=0>Sun<option value=1>Mon<option value=2>Tue<option value=3>Wed<option value=4>Thu<option value=5>Fri<option value=6>Sat</select><br>Count: <input id=n type=number value=1 min=1 max=5><pre id=o>
Takes parameters as f([year, month], day, number)
. Month and day of week (starting on Sunday) are zero-indexed. Edit: Saved 1 byte thanks to @Shaggy.
f=
(a,d,n)=>(d+6-new Date(...a,7).getDay())%7+n*7-6
<div oninput=o.textContent=f([+y.value,+m.value],+d.value,+n.value)>Year: <input id=y type=number value=2019><br>Month: <select id=m><option value=0>Jan<option value=1>Feb<option value=2>Mar<option value=3>Apr<option value=4>May<option value=5>Jun<option value=6>Jul<option value=7>Aug<option value=8>Sep<option value=9>Oct<option value=10>Nov<option value=11>Dec</select><br>Day: <select id=d><option value=0>Sun<option value=1>Mon<option value=2>Tue<option value=3>Wed<option value=4>Thu<option value=5>Fri<option value=6>Sat</select><br>Count: <input id=n type=number value=1 min=1 max=5><pre id=o>
f=
(a,d,n)=>(d+6-new Date(...a,7).getDay())%7+n*7-6
<div oninput=o.textContent=f([+y.value,+m.value],+d.value,+n.value)>Year: <input id=y type=number value=2019><br>Month: <select id=m><option value=0>Jan<option value=1>Feb<option value=2>Mar<option value=3>Apr<option value=4>May<option value=5>Jun<option value=6>Jul<option value=7>Aug<option value=8>Sep<option value=9>Oct<option value=10>Nov<option value=11>Dec</select><br>Day: <select id=d><option value=0>Sun<option value=1>Mon<option value=2>Tue<option value=3>Wed<option value=4>Thu<option value=5>Fri<option value=6>Sat</select><br>Count: <input id=n type=number value=1 min=1 max=5><pre id=o>
edited Jan 2 at 13:17
answered Jan 1 at 22:46
NeilNeil
79.6k744177
79.6k744177
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
Jan 1 at 22:48
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
Jan 1 at 22:51
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
Jan 1 at 22:52
Oh. I didn't know that, I'm not that familiar with Javascript.
– Embodiment of Ignorance
Jan 1 at 22:52
48 bytes
– Shaggy
Jan 2 at 10:51
add a comment |
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
Jan 1 at 22:48
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
Jan 1 at 22:51
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
Jan 1 at 22:52
Oh. I didn't know that, I'm not that familiar with Javascript.
– Embodiment of Ignorance
Jan 1 at 22:52
48 bytes
– Shaggy
Jan 2 at 10:51
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
Jan 1 at 22:48
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
Jan 1 at 22:48
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
Jan 1 at 22:51
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
Jan 1 at 22:51
@EmbodimentofIgnorance, Sunday is
0
in JS.– Shaggy
Jan 1 at 22:52
@EmbodimentofIgnorance, Sunday is
0
in JS.– Shaggy
Jan 1 at 22:52
Oh. I didn't know that, I'm not that familiar with Javascript.
– Embodiment of Ignorance
Jan 1 at 22:52
Oh. I didn't know that, I'm not that familiar with Javascript.
– Embodiment of Ignorance
Jan 1 at 22:52
48 bytes
– Shaggy
Jan 2 at 10:51
48 bytes
– Shaggy
Jan 2 at 10:51
add a comment |
TSQL, 106 bytes
DECLARE @ datetime='2019',@m int=3,@w char(2)='saturday',@p int=2
SELECT dateadd(wk,datediff(d,x,dateadd(m,@m-1,@)-1)/7,x)+@p*7FROM(SELECT
charindex(@w,' tuwethfrsasu')/2x)x
Output:
2019-03-09 00:00:00.000
Try it out
add a comment |
TSQL, 106 bytes
DECLARE @ datetime='2019',@m int=3,@w char(2)='saturday',@p int=2
SELECT dateadd(wk,datediff(d,x,dateadd(m,@m-1,@)-1)/7,x)+@p*7FROM(SELECT
charindex(@w,' tuwethfrsasu')/2x)x
Output:
2019-03-09 00:00:00.000
Try it out
add a comment |
TSQL, 106 bytes
DECLARE @ datetime='2019',@m int=3,@w char(2)='saturday',@p int=2
SELECT dateadd(wk,datediff(d,x,dateadd(m,@m-1,@)-1)/7,x)+@p*7FROM(SELECT
charindex(@w,' tuwethfrsasu')/2x)x
Output:
2019-03-09 00:00:00.000
Try it out
TSQL, 106 bytes
DECLARE @ datetime='2019',@m int=3,@w char(2)='saturday',@p int=2
SELECT dateadd(wk,datediff(d,x,dateadd(m,@m-1,@)-1)/7,x)+@p*7FROM(SELECT
charindex(@w,' tuwethfrsasu')/2x)x
Output:
2019-03-09 00:00:00.000
Try it out
answered Jan 3 at 13:42
t-clausen.dkt-clausen.dk
1,784314
1,784314
add a comment |
add a comment |
Kotlin, 131 84 bytes
47 bytes thanks to ASCII-only's code and comment.
Input: year, monthNumber, weekdayNumber, weekNumber
All integers 1 to maximum on single line. Week day number is Sunday of 1 to Saturday of 7. The main program makes the year lose 1900 for Date class and month and weekday are shifted to start with zero instead of one before calling the lambda. You can enter your arguments in the input text box to try your own dates.
Output: a Date class instance. The main program displays the result like: Sat Mar 09 00:00:00 UTC 2019.
There is an expanded version with comments explaining the code for those wishing to learn more.
Note: the import command required for the lambda to work can't be coded there so I replaced the first two characters with the start of an end of line comment in the "Try it online!".
{y,m,d,w->val r=java.util.Date(y,m,7*w)
r.setDate(r.getDate()-(r.getDay()+7-d)%7)
r}
Try it online!
1
74 - uses same method as Scala and Groovy answers
– ASCII-only
2 days ago
1
Fixed, 84
– ASCII-only
2 days ago
add a comment |
Kotlin, 131 84 bytes
47 bytes thanks to ASCII-only's code and comment.
Input: year, monthNumber, weekdayNumber, weekNumber
All integers 1 to maximum on single line. Week day number is Sunday of 1 to Saturday of 7. The main program makes the year lose 1900 for Date class and month and weekday are shifted to start with zero instead of one before calling the lambda. You can enter your arguments in the input text box to try your own dates.
Output: a Date class instance. The main program displays the result like: Sat Mar 09 00:00:00 UTC 2019.
There is an expanded version with comments explaining the code for those wishing to learn more.
Note: the import command required for the lambda to work can't be coded there so I replaced the first two characters with the start of an end of line comment in the "Try it online!".
{y,m,d,w->val r=java.util.Date(y,m,7*w)
r.setDate(r.getDate()-(r.getDay()+7-d)%7)
r}
Try it online!
1
74 - uses same method as Scala and Groovy answers
– ASCII-only
2 days ago
1
Fixed, 84
– ASCII-only
2 days ago
add a comment |
Kotlin, 131 84 bytes
47 bytes thanks to ASCII-only's code and comment.
Input: year, monthNumber, weekdayNumber, weekNumber
All integers 1 to maximum on single line. Week day number is Sunday of 1 to Saturday of 7. The main program makes the year lose 1900 for Date class and month and weekday are shifted to start with zero instead of one before calling the lambda. You can enter your arguments in the input text box to try your own dates.
Output: a Date class instance. The main program displays the result like: Sat Mar 09 00:00:00 UTC 2019.
There is an expanded version with comments explaining the code for those wishing to learn more.
Note: the import command required for the lambda to work can't be coded there so I replaced the first two characters with the start of an end of line comment in the "Try it online!".
{y,m,d,w->val r=java.util.Date(y,m,7*w)
r.setDate(r.getDate()-(r.getDay()+7-d)%7)
r}
Try it online!
Kotlin, 131 84 bytes
47 bytes thanks to ASCII-only's code and comment.
Input: year, monthNumber, weekdayNumber, weekNumber
All integers 1 to maximum on single line. Week day number is Sunday of 1 to Saturday of 7. The main program makes the year lose 1900 for Date class and month and weekday are shifted to start with zero instead of one before calling the lambda. You can enter your arguments in the input text box to try your own dates.
Output: a Date class instance. The main program displays the result like: Sat Mar 09 00:00:00 UTC 2019.
There is an expanded version with comments explaining the code for those wishing to learn more.
Note: the import command required for the lambda to work can't be coded there so I replaced the first two characters with the start of an end of line comment in the "Try it online!".
{y,m,d,w->val r=java.util.Date(y,m,7*w)
r.setDate(r.getDate()-(r.getDay()+7-d)%7)
r}
Try it online!
edited 2 days ago
answered 2 days ago
JohnWellsJohnWells
4816
4816
1
74 - uses same method as Scala and Groovy answers
– ASCII-only
2 days ago
1
Fixed, 84
– ASCII-only
2 days ago
add a comment |
1
74 - uses same method as Scala and Groovy answers
– ASCII-only
2 days ago
1
Fixed, 84
– ASCII-only
2 days ago
1
1
74 - uses same method as Scala and Groovy answers
– ASCII-only
2 days ago
74 - uses same method as Scala and Groovy answers
– ASCII-only
2 days ago
1
1
Fixed, 84
– ASCII-only
2 days ago
Fixed, 84
– ASCII-only
2 days ago
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178227%2fget-the-date-of-the-nth-day-of-week-in-a-given-year-and-month%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I thought I might be able to use GNU
date
, but amusingly, the parser has an... interesting interpretation here.– Doorknob♦
Jan 1 at 20:44
May week number be 0-indexed?
– Jo King
Jan 2 at 1:52
Sure, if it suits your program
– Embodiment of Ignorance
Jan 2 at 2:12
Suggested test case: 2018, 12, Sunday 1
– Adám
Jan 2 at 10:55
Suggested test case: the first Saturday in September 2019
– Shaggy
Jan 2 at 12:07