javascript 在moment.js中从周数生成日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18985475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate date from week number in moment.js
提问by Bartosz
As mentioned in the title, I have a week number and a year value. Is there a way of finding Monday of that week using moment.js? Was trying, but not succeeded
正如标题中提到的,我有一个周数和一个年份值。有没有办法使用moment.js 找到那个星期的星期一?正在尝试,但没有成功
回答by Bartosz
Sorry found solution myself:
抱歉,我自己找到了解决方案:
var test = moment().day("Monday").week(week number here);
回答by blak3r
Building off the answers provided here...
建立在这里提供的答案......
exports.getDateFromWeek = function(week, year) {
return moment().day("Monday").year(year).week(week).toDate();
};
Personally, I wanted the first Sunday... and in which case it's:
就个人而言,我想要第一个星期天……在这种情况下,它是:
exports.getDateFromWeek = function(week, year) {
return moment().day("Sunday").year(year).week(week).toDate();
};
回答by Rahul Tripathi
Try like this:-
像这样尝试:-
var weekdate= function(year, week, dayNumber)
{
var j1 = new Date( year,0,10,12,0,0),
j2 = new Date( year,0,4,12,0,0),
mon1 = j2.getTime() - j1.getDay() * 86400000;
return new Date(mon1 + ((week- 1) * 7 + dayNumber) * 86400000);
};
console.log(weekdate(2010, 1, 4));
2010
starts with Thursady
2010
以。。开始 Thursady