moment 当前周一到周日_Moment.js - 周一以isoWeekday()开始一周

I'm creating a calendar where I print out weeks in a tabular format. One requirement is that I be able to start the weeks either on Monday or Sunday, as per some user option. I'm having a hard time using moment's isoWeekday method.

// Start of some date range. Can be any day of the week.

var startOfPeriod = moment("2013-06-23T00:00:00"),

// We begin on the start of the first week.

// Mon Tues Wed Thur Fri Sat Sun

// 20 21 22 23 24 25 26

begin = moment(startOfPeriod).isoWeekday(1); // will pull from user setting

console.log(begin.isoWeekday()); // 1 - all good

// Let's get the beginning of this first week, respecting the isoWeekday

begin.startOf('week');

console.log(begin.isoWeekday()); // 7 - what happened ???

// Get column headers

for (var i=0; i<7; i++) {

console.log(begin.format('ddd')); // I want Monday first!

begin.add('d', 1);

}

EDIT I misunderstood what isoWeekday was actually doing. I thought it set the "which day of the week is the first day of the week" variable (that doesn't exist). What it actually does is simply changes the day of the week, just like moment.weekday(), but uses a 1-7 range instead of the 0-6.

解决方案

try using begin.startOf('isoWeek'); instead of begin.startOf('week');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值