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
Output:
March 9
This is code-golf, so shortest answer wins.
code-golf date
add a 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
Output:
March 9
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♦
2 hours ago
add a 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
Output:
March 9
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
Output:
March 9
This is code-golf, so shortest answer wins.
code-golf date
code-golf date
edited 15 mins ago
asked 2 hours ago
Embodiment of Ignorance
39212
39212
1
I thought I might be able to use GNUdate
, but amusingly, the parser has an... interesting interpretation here.
– Doorknob♦
2 hours ago
add a comment |
1
I thought I might be able to use GNUdate
, but amusingly, the parser has an... interesting interpretation here.
– Doorknob♦
2 hours ago
1
1
I thought I might be able to use GNU
date
, but amusingly, the parser has an... interesting interpretation here.– Doorknob♦
2 hours ago
I thought I might be able to use GNU
date
, but amusingly, the parser has an... interesting interpretation here.– Doorknob♦
2 hours ago
add a comment |
4 Answers
4
active
oldest
votes
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 ... 31] 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 |
SmileBASIC, 58 bytes
INPUT M$,D,N
DTREAD M$+"/01"OUT,,,W?(D-W+7)MOD 7+1+7*(N-1)
Input is in the form YYYY/MM,weekday,n
. The test case would be 2019/03,4,1
.
Ungolfed:
INPUT YEAR_MONTH$,WEEKDAY,N 'get input
DTREAD YEAR_MONTH$+"/01" OUT y,m,d,WEEKDAY_1 'get the day of the week of the first day in the month
FIRST = (WEEKDAY-WEEKDAY_1+7) MOD 7+1 'get the date of the first <WEEKDAY> in the month
PRINT FIRST+N*7 'output the date of the <N>th <WEEKDAY> in the month
add a comment |
PHP, 46 bytes
function f($s){return date(d,strtotime($s));};
Try it online!
The function receives as input a string like "second saturday of March 2019"
The function returns the day number.
add a comment |
JavaScript (ES6), 49 bytes
f=
(y,m,d,n)=>(d+6-new Date(y,m,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>
Month and day of week (starting on Sunday) are zero-indexed.
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
4 mins ago
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
1 min ago
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
28 secs 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
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 ... 31] 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 ... 31] 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 ... 31] 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 ... 31] 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 33 mins ago
answered 52 mins ago
Luis Mendo
74k886291
74k886291
add a comment |
add a comment |
SmileBASIC, 58 bytes
INPUT M$,D,N
DTREAD M$+"/01"OUT,,,W?(D-W+7)MOD 7+1+7*(N-1)
Input is in the form YYYY/MM,weekday,n
. The test case would be 2019/03,4,1
.
Ungolfed:
INPUT YEAR_MONTH$,WEEKDAY,N 'get input
DTREAD YEAR_MONTH$+"/01" OUT y,m,d,WEEKDAY_1 'get the day of the week of the first day in the month
FIRST = (WEEKDAY-WEEKDAY_1+7) MOD 7+1 'get the date of the first <WEEKDAY> in the month
PRINT FIRST+N*7 'output the date of the <N>th <WEEKDAY> in the month
add a comment |
SmileBASIC, 58 bytes
INPUT M$,D,N
DTREAD M$+"/01"OUT,,,W?(D-W+7)MOD 7+1+7*(N-1)
Input is in the form YYYY/MM,weekday,n
. The test case would be 2019/03,4,1
.
Ungolfed:
INPUT YEAR_MONTH$,WEEKDAY,N 'get input
DTREAD YEAR_MONTH$+"/01" OUT y,m,d,WEEKDAY_1 'get the day of the week of the first day in the month
FIRST = (WEEKDAY-WEEKDAY_1+7) MOD 7+1 'get the date of the first <WEEKDAY> in the month
PRINT FIRST+N*7 'output the date of the <N>th <WEEKDAY> in the month
add a comment |
SmileBASIC, 58 bytes
INPUT M$,D,N
DTREAD M$+"/01"OUT,,,W?(D-W+7)MOD 7+1+7*(N-1)
Input is in the form YYYY/MM,weekday,n
. The test case would be 2019/03,4,1
.
Ungolfed:
INPUT YEAR_MONTH$,WEEKDAY,N 'get input
DTREAD YEAR_MONTH$+"/01" OUT y,m,d,WEEKDAY_1 'get the day of the week of the first day in the month
FIRST = (WEEKDAY-WEEKDAY_1+7) MOD 7+1 'get the date of the first <WEEKDAY> in the month
PRINT FIRST+N*7 'output the date of the <N>th <WEEKDAY> in the month
SmileBASIC, 58 bytes
INPUT M$,D,N
DTREAD M$+"/01"OUT,,,W?(D-W+7)MOD 7+1+7*(N-1)
Input is in the form YYYY/MM,weekday,n
. The test case would be 2019/03,4,1
.
Ungolfed:
INPUT YEAR_MONTH$,WEEKDAY,N 'get input
DTREAD YEAR_MONTH$+"/01" OUT y,m,d,WEEKDAY_1 'get the day of the week of the first day in the month
FIRST = (WEEKDAY-WEEKDAY_1+7) MOD 7+1 'get the date of the first <WEEKDAY> in the month
PRINT FIRST+N*7 'output the date of the <N>th <WEEKDAY> in the month
answered 15 mins ago
12Me21
5,44711236
5,44711236
add a comment |
add a comment |
PHP, 46 bytes
function f($s){return date(d,strtotime($s));};
Try it online!
The function receives as input a string like "second saturday of March 2019"
The function returns the day number.
add a comment |
PHP, 46 bytes
function f($s){return date(d,strtotime($s));};
Try it online!
The function receives as input a string like "second saturday of March 2019"
The function returns the day number.
add a comment |
PHP, 46 bytes
function f($s){return date(d,strtotime($s));};
Try it online!
The function receives as input a string like "second saturday of March 2019"
The function returns the day number.
PHP, 46 bytes
function f($s){return date(d,strtotime($s));};
Try it online!
The function receives as input a string like "second saturday of March 2019"
The function returns the day number.
edited 1 hour ago
answered 1 hour ago
Кирилл Малышев
40115
40115
add a comment |
add a comment |
JavaScript (ES6), 49 bytes
f=
(y,m,d,n)=>(d+6-new Date(y,m,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>
Month and day of week (starting on Sunday) are zero-indexed.
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
4 mins ago
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
1 min ago
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
28 secs ago
add a comment |
JavaScript (ES6), 49 bytes
f=
(y,m,d,n)=>(d+6-new Date(y,m,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>
Month and day of week (starting on Sunday) are zero-indexed.
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
4 mins ago
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
1 min ago
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
28 secs ago
add a comment |
JavaScript (ES6), 49 bytes
f=
(y,m,d,n)=>(d+6-new Date(y,m,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>
Month and day of week (starting on Sunday) are zero-indexed.
JavaScript (ES6), 49 bytes
f=
(y,m,d,n)=>(d+6-new Date(y,m,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>
Month and day of week (starting on Sunday) are zero-indexed.
f=
(y,m,d,n)=>(d+6-new Date(y,m,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=
(y,m,d,n)=>(d+6-new Date(y,m,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 2 mins ago
answered 5 mins ago
Neil
79.4k744177
79.4k744177
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
4 mins ago
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
1 min ago
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
28 secs ago
add a comment |
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
4 mins ago
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
1 min ago
@EmbodimentofIgnorance, Sunday is0
in JS.
– Shaggy
28 secs ago
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
4 mins ago
Is the day of week in the format Sunday-Saturday or Monday-Sunday?
– Embodiment of Ignorance
4 mins ago
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
1 min ago
@EmbodimentofIgnorance Sorry, I knew I had forgotten something. (The snippet now uses select to make it easier.)
– Neil
1 min ago
@EmbodimentofIgnorance, Sunday is
0
in JS.– Shaggy
28 secs ago
@EmbodimentofIgnorance, Sunday is
0
in JS.– Shaggy
28 secs 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♦
2 hours ago