[英]Moment.js bug while adding 1 week at last week of year
I am trying to add one week whatever user has selected week and year and i need output in 'week year' format. 我试图添加一个星期,无论用户选择了星期和年份,我都需要以“周年”格式输出。
I tried to use momentjs. 我试图使用momentjs。 check below example, instead of giving '1 2016' its giving '1 2015'.
请检查以下示例,而不是给“ 1 2016”赋予“ 1 2015”。
var test = moment('52 2015','w YYYY').add(1,'w').format('w YYYY'); $('body').html(test);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src='http://momentjs.com/downloads/moment.min.js'></script>
52th week of 2015 starts on 2015-12-20; 2015年第52周于2015年12月20日开始; one week later, it is 2015-12-27.
一周后,是2015年12月27日。 No bug in Moment.js - not all years have 52 weeks.
Moment.js中没有错误-并非所有年份都有52周。 (Note that 1st week of 2015 starts in 2014.)
(请注意,2015年的第一周从2014年开始。)
EDIT: This is a bit confusingly stated, and indeed Moment.js is being confusing itself: 编辑:这有点令人困惑,实际上Moment.js本身就是令人困惑:
moment('1 2015','w YYYY').format('w YYYY')
// => 1 2014
In the parsing, YYYY
refers to the year where you count weeks; 在分析中,
YYYY
是指您计算周数的年份; accidentally, the date for the start of that week lies in the previous year. 偶然地,该周开始的日期是前一年。 In formatting,
YYYY
refers to the year the date is in. I guess one could call that a bug; 在格式化中,
YYYY
是指日期所在的年份。 it is certainly unexpected. 这当然是出乎意料的。
EDIT2: How to do it? EDIT2:怎么做? You do it yourself.
你自己做。 There is something strange if month is not January and week is 1st week of the year;
如果月份不是一月,而一周是一年中的第一周,那会有些奇怪。 so increment the year.
所以增加年份。
function weekAndYear(date) { var week = date.week(); var year = date.year(); if (date.month() && week == 1) { year++; } return '' + week + ' ' + year; } document.body.textContent = weekAndYear(moment('52 2015','w YYYY').add(1,'w'));
<script src="https://cdn.jsdelivr.net/momentjs/2.10.6/moment-with-locales.min.js"></script>
Also be aware that week()
(and w
specifier) are locale-dependent, and will give you different results in different places. 还要注意,
week()
(和w
指定符)是与语言环境相关的,并且将在不同的地方给您不同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.