admin管理员组文章数量:1322697
I have some code that gets the first weekday of the month using moment.js (that is a requirement) which looks like this:
dateStart: function() {
var first = moment().startOf('month');
switch(first.day()) {
case 6:
return first.add(2, 'days');
case 0:
return first.add(1, 'days');
default:
return first;
};
}
Is there a better way of doing this?
I have some code that gets the first weekday of the month using moment.js (that is a requirement) which looks like this:
dateStart: function() {
var first = moment().startOf('month');
switch(first.day()) {
case 6:
return first.add(2, 'days');
case 0:
return first.add(1, 'days');
default:
return first;
};
}
Is there a better way of doing this?
Share Improve this question asked Sep 12, 2016 at 8:20 aydowaydow 3,8113 gold badges25 silver badges41 bronze badges 2- Checkout this thread: stackoverflow./questions/18875649/… – David R Commented Sep 12, 2016 at 8:24
- I think your method is pretty clever. – Steven Wexler Commented Sep 12, 2016 at 8:35
4 Answers
Reset to default 3If the first day is sunday or saturday (first.day() % 6 === 0
) then return next monday (first.day(1)
):
function dateStart() {
var first = moment().startOf('month');
return first.day() % 6 === 0 ? first.add(1, 'day').day(1) : first;
}
As mentioned in ments first.day(1)
can return monday in previous month. This can happen if the first day of the month is saturday. To be sure you get monday from the week in current month just add 1 to the weekend date.
Interesting question. I guess you just have to get the first day of the month and then add days until the day is a working day. Check it out: https://jsfiddle/4rfrg4c0/2/
function get_first_working_day (year, month) {
// get the first day of the specified month and year
var first_working_day = new moment([year, month])
// add days until the day is a working day
while (first_working_day.day() % 6 == 0) {
first_working_day = first_working_day.add(1, 'day')
}
// return the day
return first_working_day
}
// tests
$('.september').append(get_first_working_day(2016, 8).format('dddd, MMMM Do YYYY'))
$('.october').append(get_first_working_day(2016, 9).format('dddd, MMMM Do YYYY'))
Try using this:
const currentDate = moment(Date.now);
const dayOfMonth = currentDate.date();
const dayOfWeek = currentDate.weekday();
//check if its the first 3 days of the month
if (dayOfMonth >= 1 && dayOfMonth <= 3) {
//check if its the first of the month and also a weekday
if (dayOfMonth === 1 && dayOfWeek >= 1 && dayOfWeek <= 5) {
//set your conditions here
}
//check if its the 2nd/3rd of the month and also a weekday, if the the 1st/2nd was //on a weekend
if ((dayOfMonth === 2 || dayOfMonth === 3) && dayOfWeek === 1) {
//set your conditions here
}
}
Sorry but none of your solutions are quite satisfying if you want the first monday of the next month = month +1.. Do this (but you can convert using xxx? zzz:yyy; notation, I kept it with classic if for readibility)
let first = moment().startOf('month').add(1, 'month');
let day = first;
if ( first.day() > 1 ) {
day = first.add(8 - first.day(), 'day');
}
if ( day.day() === 0 ) {
day = day.add(1, 'days');
}
date = day.format('YYYY-MM-DD');
本文标签: javascriptGet the First Weekday of the Month with momentjsStack Overflow
版权声明:本文标题:javascript - Get the First Weekday of the Month with moment.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742108423a2421131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论