Javascript 如何获取上个月(12月)的最后一个工作日的时刻

Javascript 如何获取上个月(12月)的最后一个工作日的时刻,javascript,momentjs,Javascript,Momentjs,我遵循这个指南 在第一个月(1月)的第一个工作日工作正常 但是,当我试图使用相反的方法时。我得到的是这个月的最后一个工作日,也就是十二月。我尝试更改外接程序的减号和sub,但不起作用 var dateFrom = moment().subtract(1, 'months').endOf('month').format("dddd") alert(dateFrom); 今年用这个 var year = moment().subtract(1, 'months').endOf('month').

我遵循这个指南

在第一个月(1月)的第一个工作日工作正常

但是,当我试图使用相反的方法时。我得到的是这个月的最后一个工作日,也就是十二月。我尝试更改外接程序的减号和sub,但不起作用

 var dateFrom = moment().subtract(1, 'months').endOf('month').format("dddd")
alert(dateFrom);
今年用这个

var year = moment().subtract(1, 'months').endOf('month').get('year');
alert(year);
将其与格式“dddd”一起使用

因此,对于工作日,请使用“时刻商务”

工作

今年用这个

var year = moment().subtract(1, 'months').endOf('month').get('year');
alert(year);
将其与格式“dddd”一起使用

因此,对于工作日,请使用“时刻商务”

工作

您可以使用它进行与工作日相关的处理。如果你做更多的这样的处理,而不仅仅是解决这个问题,那就容易多了

var moment = require('moment-business-days');

// Set the date for december. You can use this for any month.
// Get array of business days for the month
var businessDays=moment('01-12-2017', 'DD-MM-YYYY').monthBusinessDays();

// Get last business day from the array
var lastBusinessDay = businessDays[businessDays.length-1]._d;

console.log(lastBusinessDay);
您可以查看输出或克隆并编辑它。 以下是可用于处理工作日相关处理的

。如果你做更多的这样的处理,而不仅仅是解决这个问题,那就容易多了

var moment = require('moment-business-days');

// Set the date for december. You can use this for any month.
// Get array of business days for the month
var businessDays=moment('01-12-2017', 'DD-MM-YYYY').monthBusinessDays();

// Get last business day from the array
var lastBusinessDay = businessDays[businessDays.length-1]._d;

console.log(lastBusinessDay);
您可以查看输出或克隆并编辑它。 这是我的建议

/*每月最后一周测试一次*/
变量eom=null;/*门店月末*/
var log='';
var i=0;
/*从1月到12月的所有12个月的循环*/
而(i<12){
eom=时刻().utc().month(i).endOf('month');
log=eom.format('LLLL')+'~';
eom.add((eom.day()%6!==0)?0:(eom.day()==0)?-2:-1,“day”);
log+=eom.format('LLLL');
console.log(log);
i++;
}
/*每月最后一周测试一次*/
变量eom=null;/*门店月末*/
var log='';
var i=0;
/*从1月到12月的所有12个月的循环*/
而(i<12){
eom=时刻().utc().month(i).endOf('month');
log=eom.format('LLLL')+'~';
eom.add((eom.day()%6!==0)?0:(eom.day()==0)?-2:-1,“day”);
log+=eom.format('LLLL');
console.log(log);
i++;
}


你能展示你的js?源代码吗?或者你指的是JSFIDLE吗???是的,如果你能注意到,在很多地方,商业周从周六到周三,周四和周五都是周末你能展示你的js?源代码吗?或者你是指JSFIDLE吗???是的,如果你能注意到在很多地方,商业周刊从周六到周三,周四和周五都是周末,请注意我认为“工作日”,OP的意思是“只有周一至周五是周六,当我编辑格式时是2016年,如果我们得到最新的一年,那就太好了:)也编辑了我今年的答案,也更新了小提琴我认为是“工作日”,OP的意思是“仅周一至周五”是的,我在周六编辑了格式,现在是2016年,如果我们能得到最新的一年,那就太好了:)也编辑了我今年的答案,更新了fiddle,还有你有JSFIDLE,所以我可以测试:)我在nodejs中使用了它,我不知道怎样才能在工作日上增加时间fiddle@wiwit我也添加了linkadded fiddle,让我检查一下:)你有JSFIDLE吗,这样我就可以测试:)我在nodejs中使用了它,我不知道如何才能添加即时工作日fiddle@wiwit我还添加了链接added fiddle让我检查一下:)