admin管理员组文章数量:1429841
Is there any way to get all the Monday DATES in the current month using moment.js library.
I know I can get the end of the month with:
moment().endOf('month')
but how to get all monday dates of current / any month.
I dont want some function in javascript default date library. Please refer the code using Moment Js Library.
Is there any way to get all the Monday DATES in the current month using moment.js library.
I know I can get the end of the month with:
moment().endOf('month')
but how to get all monday dates of current / any month.
I dont want some function in javascript default date library. Please refer the code using Moment Js Library.
Share Improve this question edited Nov 10, 2015 at 8:46 Shan Khan asked Nov 9, 2015 at 23:18 Shan KhanShan Khan 10.3k18 gold badges69 silver badges114 bronze badges 2- Possible duplicate of How to get the 4 monday of a month with js? – JotaBe Commented Nov 10, 2015 at 1:14
- @JotaBe Please refer the code using Moment Js Library. I dont want to parse javascript default date library – Shan Khan Commented Nov 10, 2015 at 8:56
3 Answers
Reset to default 26I've just read docs and haven't found any method that returns an array, so you need to do it only with loop
var monday = moment()
.startOf('month')
.day("Monday");
if (monday.date() > 7) monday.add(7,'d');
var month = monday.month();
while(month === monday.month()){
document.body.innerHTML += "<p>"+monday.toString()+"</p>";
monday.add(7,'d');
}
check this out
Update
I made a little drop in for this. Add it to your page:
<script src="https://gist.github.com/penne12/ae44799b7e94ce08753a/raw/moment-daysoftheweek-plus.js"></script>
And then:
moment().allDays(1) //=> [...]
moment().firstDay(1)
etc - feel free to check out the source
Old Post
This function should work:
function getMondays(date) {
var d = date || new Date(),
month = d.getMonth(),
mondays = [];
d.setDate(1);
// Get the first Monday in the month
while (d.getDay() !== 1) {
d.setDate(d.getDate() + 1);
}
// Get all the other Mondays in the month
while (d.getMonth() === month) {
mondays.push(new Date(d.getTime()));
d.setDate(d.getDate() + 7);
}
return mondays;
}
Usage: getMondays()
for this month, or getMondays(moment().month("Feb").toDate())
for a different month.
From jabclab's Stack Overflow Answer, modified to include custom date param.
I know this is old but this is the first thing that comes up in Google.
You can get all the Mondays in the month by using my moment-weekdaysin moment.js plugin:
moment().weekdaysInMonth('Monday');
本文标签: javascriptGet All Monday Dates In Month using MomentjsStack Overflow
版权声明:本文标题:javascript - Get All Monday Dates In Month using Moment,js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737982587a2044175.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论