How to get first day of particular week(week 2 in november) and last day of particular week(week 4 in...











up vote
-2
down vote

favorite












I need to implement a functionality in javascript which takes from week and to week as inputs i.e it takes week number of current month (ex : 1,2,3,4,5,6) and get the start date of the week and end day of other week in current month and current year



Ex: selecting 3 and 4 should give current month 3rd week first day and 4th week last day
In Nov:2018 output should be as 11/11/2018 and 24/11/2018










share|improve this question




















  • 2




    Welcome to SO. Please take a tour of the help centre to see how to ask a good question and what types of question are on topic for the site. We cannot help you if you do not provide any code - see how to create a Minimal, Complete, and Verifiable example
    – Pete
    Nov 21 at 16:47










  • How do you work out the week in the month? Different cultures use a different first day of the week.
    – RobG
    Nov 21 at 20:15















up vote
-2
down vote

favorite












I need to implement a functionality in javascript which takes from week and to week as inputs i.e it takes week number of current month (ex : 1,2,3,4,5,6) and get the start date of the week and end day of other week in current month and current year



Ex: selecting 3 and 4 should give current month 3rd week first day and 4th week last day
In Nov:2018 output should be as 11/11/2018 and 24/11/2018










share|improve this question




















  • 2




    Welcome to SO. Please take a tour of the help centre to see how to ask a good question and what types of question are on topic for the site. We cannot help you if you do not provide any code - see how to create a Minimal, Complete, and Verifiable example
    – Pete
    Nov 21 at 16:47










  • How do you work out the week in the month? Different cultures use a different first day of the week.
    – RobG
    Nov 21 at 20:15













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I need to implement a functionality in javascript which takes from week and to week as inputs i.e it takes week number of current month (ex : 1,2,3,4,5,6) and get the start date of the week and end day of other week in current month and current year



Ex: selecting 3 and 4 should give current month 3rd week first day and 4th week last day
In Nov:2018 output should be as 11/11/2018 and 24/11/2018










share|improve this question















I need to implement a functionality in javascript which takes from week and to week as inputs i.e it takes week number of current month (ex : 1,2,3,4,5,6) and get the start date of the week and end day of other week in current month and current year



Ex: selecting 3 and 4 should give current month 3rd week first day and 4th week last day
In Nov:2018 output should be as 11/11/2018 and 24/11/2018







javascript date week-number weekday






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 16:40

























asked Nov 21 at 16:34









Sai Krishna

33




33








  • 2




    Welcome to SO. Please take a tour of the help centre to see how to ask a good question and what types of question are on topic for the site. We cannot help you if you do not provide any code - see how to create a Minimal, Complete, and Verifiable example
    – Pete
    Nov 21 at 16:47










  • How do you work out the week in the month? Different cultures use a different first day of the week.
    – RobG
    Nov 21 at 20:15














  • 2




    Welcome to SO. Please take a tour of the help centre to see how to ask a good question and what types of question are on topic for the site. We cannot help you if you do not provide any code - see how to create a Minimal, Complete, and Verifiable example
    – Pete
    Nov 21 at 16:47










  • How do you work out the week in the month? Different cultures use a different first day of the week.
    – RobG
    Nov 21 at 20:15








2




2




Welcome to SO. Please take a tour of the help centre to see how to ask a good question and what types of question are on topic for the site. We cannot help you if you do not provide any code - see how to create a Minimal, Complete, and Verifiable example
– Pete
Nov 21 at 16:47




Welcome to SO. Please take a tour of the help centre to see how to ask a good question and what types of question are on topic for the site. We cannot help you if you do not provide any code - see how to create a Minimal, Complete, and Verifiable example
– Pete
Nov 21 at 16:47












How do you work out the week in the month? Different cultures use a different first day of the week.
– RobG
Nov 21 at 20:15




How do you work out the week in the month? Different cultures use a different first day of the week.
– RobG
Nov 21 at 20:15












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You can try below approach to figure out your expected result. Solution below takes month as an input so that we can verify for any month






function getDates(m, a, b) {
var date = new Date()
, y = date.getFullYear()
, lastDate = new Date(y, m, 0).getDate()

var offset = new Date(new Date().setDate(1)).getDay()

return [(a - 1) * 7 + 1, b * 7].map(d => {
d -= offset
if(d < 1) d = 1
if(d > lastDate) d = lastDate
return d + '/' + m + '/' + y
})
}

console.log(getDates(11,1,2))
console.log(getDates(11,2,3))
console.log(getDates(11,3,4))
console.log(getDates(11,1,5))








share|improve this answer























  • toLocaleDateString() probably shouldn't be used. In my locale, getDates(3, 4) returned ["11/21/2018", "24/21/2018"], one of which isn't an actual date. But changing it to toLocaleDateString('en-IN') the answer comes out correct.
    – Khauri McClain
    Nov 21 at 16:55












  • I realised adding 'en-In' would do the trick. But I have updated solution li'l differently. Pls try now.
    – Nitish Narang
    Nov 21 at 16:59












  • @SaiKrishna Can you pls try now!!
    – Nitish Narang
    Nov 21 at 17:07






  • 1




    @NitishNarang for getDates(1,5) output is ["1/11/2018", "31/11/2018"] but it should be ["1/11/2018", "30/11/2018"]
    – Sai Krishna
    Nov 21 at 17:08








  • 1




    @NitishNarang Thanks for the answer. it works perfectly :D
    – Sai Krishna
    Nov 21 at 17:19


















up vote
0
down vote













function getRange(week1, week2){
var start = (moment().startOf('month') > moment().startOf('month').add(week1 - 1, 'week').startOf('week')) ? moment().startOf('month') : moment().startOf('month').add(week1 - 1, 'week').startOf('week');
var end = ( moment().endOf('month') < moment().startOf('month').add(week2 - 1 , 'week').endOf('week')) ? moment().endOf('month'): moment().startOf('month').add(week2 - 1 , 'week').endOf('week');

console.log(start.format('D/MM/YYYY'))
console.log(end.format('D/MM/YYYY'))

}


If you can use moment.js






share|improve this answer























  • For getRange(1,5) the output is 28/10/2018 01/12/2018 but output should be as 1/11/2018 30/11/2018 My requirement is it should be in current month should not jump to other month
    – Sai Krishna
    Nov 21 at 17:07










  • You could add conditionals to check if the start and end of the week are outside the current month
    – Keith A Young
    Nov 21 at 17:20










  • @SaiKrishna—you should put your requirements in the question, not in comments for an answer.
    – RobG
    Nov 21 at 20:34











Your Answer






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: "1"
};
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',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416639%2fhow-to-get-first-day-of-particular-weekweek-2-in-november-and-last-day-of-part%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You can try below approach to figure out your expected result. Solution below takes month as an input so that we can verify for any month






function getDates(m, a, b) {
var date = new Date()
, y = date.getFullYear()
, lastDate = new Date(y, m, 0).getDate()

var offset = new Date(new Date().setDate(1)).getDay()

return [(a - 1) * 7 + 1, b * 7].map(d => {
d -= offset
if(d < 1) d = 1
if(d > lastDate) d = lastDate
return d + '/' + m + '/' + y
})
}

console.log(getDates(11,1,2))
console.log(getDates(11,2,3))
console.log(getDates(11,3,4))
console.log(getDates(11,1,5))








share|improve this answer























  • toLocaleDateString() probably shouldn't be used. In my locale, getDates(3, 4) returned ["11/21/2018", "24/21/2018"], one of which isn't an actual date. But changing it to toLocaleDateString('en-IN') the answer comes out correct.
    – Khauri McClain
    Nov 21 at 16:55












  • I realised adding 'en-In' would do the trick. But I have updated solution li'l differently. Pls try now.
    – Nitish Narang
    Nov 21 at 16:59












  • @SaiKrishna Can you pls try now!!
    – Nitish Narang
    Nov 21 at 17:07






  • 1




    @NitishNarang for getDates(1,5) output is ["1/11/2018", "31/11/2018"] but it should be ["1/11/2018", "30/11/2018"]
    – Sai Krishna
    Nov 21 at 17:08








  • 1




    @NitishNarang Thanks for the answer. it works perfectly :D
    – Sai Krishna
    Nov 21 at 17:19















up vote
0
down vote



accepted










You can try below approach to figure out your expected result. Solution below takes month as an input so that we can verify for any month






function getDates(m, a, b) {
var date = new Date()
, y = date.getFullYear()
, lastDate = new Date(y, m, 0).getDate()

var offset = new Date(new Date().setDate(1)).getDay()

return [(a - 1) * 7 + 1, b * 7].map(d => {
d -= offset
if(d < 1) d = 1
if(d > lastDate) d = lastDate
return d + '/' + m + '/' + y
})
}

console.log(getDates(11,1,2))
console.log(getDates(11,2,3))
console.log(getDates(11,3,4))
console.log(getDates(11,1,5))








share|improve this answer























  • toLocaleDateString() probably shouldn't be used. In my locale, getDates(3, 4) returned ["11/21/2018", "24/21/2018"], one of which isn't an actual date. But changing it to toLocaleDateString('en-IN') the answer comes out correct.
    – Khauri McClain
    Nov 21 at 16:55












  • I realised adding 'en-In' would do the trick. But I have updated solution li'l differently. Pls try now.
    – Nitish Narang
    Nov 21 at 16:59












  • @SaiKrishna Can you pls try now!!
    – Nitish Narang
    Nov 21 at 17:07






  • 1




    @NitishNarang for getDates(1,5) output is ["1/11/2018", "31/11/2018"] but it should be ["1/11/2018", "30/11/2018"]
    – Sai Krishna
    Nov 21 at 17:08








  • 1




    @NitishNarang Thanks for the answer. it works perfectly :D
    – Sai Krishna
    Nov 21 at 17:19













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You can try below approach to figure out your expected result. Solution below takes month as an input so that we can verify for any month






function getDates(m, a, b) {
var date = new Date()
, y = date.getFullYear()
, lastDate = new Date(y, m, 0).getDate()

var offset = new Date(new Date().setDate(1)).getDay()

return [(a - 1) * 7 + 1, b * 7].map(d => {
d -= offset
if(d < 1) d = 1
if(d > lastDate) d = lastDate
return d + '/' + m + '/' + y
})
}

console.log(getDates(11,1,2))
console.log(getDates(11,2,3))
console.log(getDates(11,3,4))
console.log(getDates(11,1,5))








share|improve this answer














You can try below approach to figure out your expected result. Solution below takes month as an input so that we can verify for any month






function getDates(m, a, b) {
var date = new Date()
, y = date.getFullYear()
, lastDate = new Date(y, m, 0).getDate()

var offset = new Date(new Date().setDate(1)).getDay()

return [(a - 1) * 7 + 1, b * 7].map(d => {
d -= offset
if(d < 1) d = 1
if(d > lastDate) d = lastDate
return d + '/' + m + '/' + y
})
}

console.log(getDates(11,1,2))
console.log(getDates(11,2,3))
console.log(getDates(11,3,4))
console.log(getDates(11,1,5))








function getDates(m, a, b) {
var date = new Date()
, y = date.getFullYear()
, lastDate = new Date(y, m, 0).getDate()

var offset = new Date(new Date().setDate(1)).getDay()

return [(a - 1) * 7 + 1, b * 7].map(d => {
d -= offset
if(d < 1) d = 1
if(d > lastDate) d = lastDate
return d + '/' + m + '/' + y
})
}

console.log(getDates(11,1,2))
console.log(getDates(11,2,3))
console.log(getDates(11,3,4))
console.log(getDates(11,1,5))





function getDates(m, a, b) {
var date = new Date()
, y = date.getFullYear()
, lastDate = new Date(y, m, 0).getDate()

var offset = new Date(new Date().setDate(1)).getDay()

return [(a - 1) * 7 + 1, b * 7].map(d => {
d -= offset
if(d < 1) d = 1
if(d > lastDate) d = lastDate
return d + '/' + m + '/' + y
})
}

console.log(getDates(11,1,2))
console.log(getDates(11,2,3))
console.log(getDates(11,3,4))
console.log(getDates(11,1,5))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 17:30

























answered Nov 21 at 16:48









Nitish Narang

2,925812




2,925812












  • toLocaleDateString() probably shouldn't be used. In my locale, getDates(3, 4) returned ["11/21/2018", "24/21/2018"], one of which isn't an actual date. But changing it to toLocaleDateString('en-IN') the answer comes out correct.
    – Khauri McClain
    Nov 21 at 16:55












  • I realised adding 'en-In' would do the trick. But I have updated solution li'l differently. Pls try now.
    – Nitish Narang
    Nov 21 at 16:59












  • @SaiKrishna Can you pls try now!!
    – Nitish Narang
    Nov 21 at 17:07






  • 1




    @NitishNarang for getDates(1,5) output is ["1/11/2018", "31/11/2018"] but it should be ["1/11/2018", "30/11/2018"]
    – Sai Krishna
    Nov 21 at 17:08








  • 1




    @NitishNarang Thanks for the answer. it works perfectly :D
    – Sai Krishna
    Nov 21 at 17:19


















  • toLocaleDateString() probably shouldn't be used. In my locale, getDates(3, 4) returned ["11/21/2018", "24/21/2018"], one of which isn't an actual date. But changing it to toLocaleDateString('en-IN') the answer comes out correct.
    – Khauri McClain
    Nov 21 at 16:55












  • I realised adding 'en-In' would do the trick. But I have updated solution li'l differently. Pls try now.
    – Nitish Narang
    Nov 21 at 16:59












  • @SaiKrishna Can you pls try now!!
    – Nitish Narang
    Nov 21 at 17:07






  • 1




    @NitishNarang for getDates(1,5) output is ["1/11/2018", "31/11/2018"] but it should be ["1/11/2018", "30/11/2018"]
    – Sai Krishna
    Nov 21 at 17:08








  • 1




    @NitishNarang Thanks for the answer. it works perfectly :D
    – Sai Krishna
    Nov 21 at 17:19
















toLocaleDateString() probably shouldn't be used. In my locale, getDates(3, 4) returned ["11/21/2018", "24/21/2018"], one of which isn't an actual date. But changing it to toLocaleDateString('en-IN') the answer comes out correct.
– Khauri McClain
Nov 21 at 16:55






toLocaleDateString() probably shouldn't be used. In my locale, getDates(3, 4) returned ["11/21/2018", "24/21/2018"], one of which isn't an actual date. But changing it to toLocaleDateString('en-IN') the answer comes out correct.
– Khauri McClain
Nov 21 at 16:55














I realised adding 'en-In' would do the trick. But I have updated solution li'l differently. Pls try now.
– Nitish Narang
Nov 21 at 16:59






I realised adding 'en-In' would do the trick. But I have updated solution li'l differently. Pls try now.
– Nitish Narang
Nov 21 at 16:59














@SaiKrishna Can you pls try now!!
– Nitish Narang
Nov 21 at 17:07




@SaiKrishna Can you pls try now!!
– Nitish Narang
Nov 21 at 17:07




1




1




@NitishNarang for getDates(1,5) output is ["1/11/2018", "31/11/2018"] but it should be ["1/11/2018", "30/11/2018"]
– Sai Krishna
Nov 21 at 17:08






@NitishNarang for getDates(1,5) output is ["1/11/2018", "31/11/2018"] but it should be ["1/11/2018", "30/11/2018"]
– Sai Krishna
Nov 21 at 17:08






1




1




@NitishNarang Thanks for the answer. it works perfectly :D
– Sai Krishna
Nov 21 at 17:19




@NitishNarang Thanks for the answer. it works perfectly :D
– Sai Krishna
Nov 21 at 17:19












up vote
0
down vote













function getRange(week1, week2){
var start = (moment().startOf('month') > moment().startOf('month').add(week1 - 1, 'week').startOf('week')) ? moment().startOf('month') : moment().startOf('month').add(week1 - 1, 'week').startOf('week');
var end = ( moment().endOf('month') < moment().startOf('month').add(week2 - 1 , 'week').endOf('week')) ? moment().endOf('month'): moment().startOf('month').add(week2 - 1 , 'week').endOf('week');

console.log(start.format('D/MM/YYYY'))
console.log(end.format('D/MM/YYYY'))

}


If you can use moment.js






share|improve this answer























  • For getRange(1,5) the output is 28/10/2018 01/12/2018 but output should be as 1/11/2018 30/11/2018 My requirement is it should be in current month should not jump to other month
    – Sai Krishna
    Nov 21 at 17:07










  • You could add conditionals to check if the start and end of the week are outside the current month
    – Keith A Young
    Nov 21 at 17:20










  • @SaiKrishna—you should put your requirements in the question, not in comments for an answer.
    – RobG
    Nov 21 at 20:34















up vote
0
down vote













function getRange(week1, week2){
var start = (moment().startOf('month') > moment().startOf('month').add(week1 - 1, 'week').startOf('week')) ? moment().startOf('month') : moment().startOf('month').add(week1 - 1, 'week').startOf('week');
var end = ( moment().endOf('month') < moment().startOf('month').add(week2 - 1 , 'week').endOf('week')) ? moment().endOf('month'): moment().startOf('month').add(week2 - 1 , 'week').endOf('week');

console.log(start.format('D/MM/YYYY'))
console.log(end.format('D/MM/YYYY'))

}


If you can use moment.js






share|improve this answer























  • For getRange(1,5) the output is 28/10/2018 01/12/2018 but output should be as 1/11/2018 30/11/2018 My requirement is it should be in current month should not jump to other month
    – Sai Krishna
    Nov 21 at 17:07










  • You could add conditionals to check if the start and end of the week are outside the current month
    – Keith A Young
    Nov 21 at 17:20










  • @SaiKrishna—you should put your requirements in the question, not in comments for an answer.
    – RobG
    Nov 21 at 20:34













up vote
0
down vote










up vote
0
down vote









function getRange(week1, week2){
var start = (moment().startOf('month') > moment().startOf('month').add(week1 - 1, 'week').startOf('week')) ? moment().startOf('month') : moment().startOf('month').add(week1 - 1, 'week').startOf('week');
var end = ( moment().endOf('month') < moment().startOf('month').add(week2 - 1 , 'week').endOf('week')) ? moment().endOf('month'): moment().startOf('month').add(week2 - 1 , 'week').endOf('week');

console.log(start.format('D/MM/YYYY'))
console.log(end.format('D/MM/YYYY'))

}


If you can use moment.js






share|improve this answer














function getRange(week1, week2){
var start = (moment().startOf('month') > moment().startOf('month').add(week1 - 1, 'week').startOf('week')) ? moment().startOf('month') : moment().startOf('month').add(week1 - 1, 'week').startOf('week');
var end = ( moment().endOf('month') < moment().startOf('month').add(week2 - 1 , 'week').endOf('week')) ? moment().endOf('month'): moment().startOf('month').add(week2 - 1 , 'week').endOf('week');

console.log(start.format('D/MM/YYYY'))
console.log(end.format('D/MM/YYYY'))

}


If you can use moment.js







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 17:18

























answered Nov 21 at 16:56









Keith A Young

11




11












  • For getRange(1,5) the output is 28/10/2018 01/12/2018 but output should be as 1/11/2018 30/11/2018 My requirement is it should be in current month should not jump to other month
    – Sai Krishna
    Nov 21 at 17:07










  • You could add conditionals to check if the start and end of the week are outside the current month
    – Keith A Young
    Nov 21 at 17:20










  • @SaiKrishna—you should put your requirements in the question, not in comments for an answer.
    – RobG
    Nov 21 at 20:34


















  • For getRange(1,5) the output is 28/10/2018 01/12/2018 but output should be as 1/11/2018 30/11/2018 My requirement is it should be in current month should not jump to other month
    – Sai Krishna
    Nov 21 at 17:07










  • You could add conditionals to check if the start and end of the week are outside the current month
    – Keith A Young
    Nov 21 at 17:20










  • @SaiKrishna—you should put your requirements in the question, not in comments for an answer.
    – RobG
    Nov 21 at 20:34
















For getRange(1,5) the output is 28/10/2018 01/12/2018 but output should be as 1/11/2018 30/11/2018 My requirement is it should be in current month should not jump to other month
– Sai Krishna
Nov 21 at 17:07




For getRange(1,5) the output is 28/10/2018 01/12/2018 but output should be as 1/11/2018 30/11/2018 My requirement is it should be in current month should not jump to other month
– Sai Krishna
Nov 21 at 17:07












You could add conditionals to check if the start and end of the week are outside the current month
– Keith A Young
Nov 21 at 17:20




You could add conditionals to check if the start and end of the week are outside the current month
– Keith A Young
Nov 21 at 17:20












@SaiKrishna—you should put your requirements in the question, not in comments for an answer.
– RobG
Nov 21 at 20:34




@SaiKrishna—you should put your requirements in the question, not in comments for an answer.
– RobG
Nov 21 at 20:34


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.





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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416639%2fhow-to-get-first-day-of-particular-weekweek-2-in-november-and-last-day-of-part%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Berounka

Fiat S.p.A.

Type 'String' is not a subtype of type 'int' of 'index'