te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Get All Monday Dates In Month using Moment,js - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get All Monday Dates In Month using Moment,js - Stack Overflow

programmeradmin15浏览0评论

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
Add a comment  | 

3 Answers 3

Reset to default 26

I'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');
发布评论

评论列表(0)

  1. 暂无评论