admin管理员组文章数量:1406942
I have this code in Sharepoint:
var d = $.trim(oListItem.get_item('Begindatum'));
alert(d);
var m = d.getMonth() + 1;
alert(m);
The first alert returns: Thu Apr 20 2017 00:00:00 GMT+0200 (W. Europe Daylight Time), which is correct.
The second alert (m) is not fired and the code after that is not executed. What is wrong with my code?
I have this code in Sharepoint:
var d = $.trim(oListItem.get_item('Begindatum'));
alert(d);
var m = d.getMonth() + 1;
alert(m);
The first alert returns: Thu Apr 20 2017 00:00:00 GMT+0200 (W. Europe Daylight Time), which is correct.
The second alert (m) is not fired and the code after that is not executed. What is wrong with my code?
Share Improve this question asked Jun 2, 2017 at 9:25 johannesjohannes 1,4481 gold badge13 silver badges26 bronze badges 6- Look into the browser console if you see any error – Jens Commented Jun 2, 2017 at 9:27
-
1
Your
d
is a string, not a Date object. – georg Commented Jun 2, 2017 at 9:27 -
1
$.trim()
returns a string – Andreas Commented Jun 2, 2017 at 9:27 - 1 I guess d is a string not a date – Jens Commented Jun 2, 2017 at 9:28
- If you trim something, then it is probably a string value, and not a Date object. Ergo, you can not call Date object methods like getMonth on it. Pretty sure the browser console would’ve already told you something like that, if only you had bothered to look. – C3roe Commented Jun 2, 2017 at 9:28
1 Answer
Reset to default 3$.trim()
will return a string, not a date.
You would need to cast back to a date object before you can do getMonth()
.
var d = $.trim(oListItem.get_item('Begindatum'));
alert(d);
var m = new Date(d).getMonth() + 1;
alert(m);
本文标签: javascriptWhat is wrong with getMonth()1Stack Overflow
版权声明:本文标题:javascript - What is wrong with getMonth() + 1; - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745051301a2639670.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论