admin管理员组文章数量:1022137
I have the following javascript:
const to = moment(item.to);
const toNextDay = to.add(1, 'days');
item.to is a string that has the following format:
"2020-06-30T00:00:00"
But is not adding the next day, that would be "2020-07-01T00:00:00"
This is the Function:
private getCurrentRecord(records: ConfigRecord[]) {
let result: string = null;
for (let index = records.length - 1; index >= 0; index--) {
const item = records[index];
if (item.from && item.to) {
const from = moment(item.from);
const to = moment(item.to).add(1, 'days');
const today = moment();
console.log(today.format());
if (from <= today && today < to) {
result = item.value;
break;
}
}
}
return result;
}
I have the following javascript:
const to = moment(item.to);
const toNextDay = to.add(1, 'days');
item.to is a string that has the following format:
"2020-06-30T00:00:00"
But is not adding the next day, that would be "2020-07-01T00:00:00"
This is the Function:
private getCurrentRecord(records: ConfigRecord[]) {
let result: string = null;
for (let index = records.length - 1; index >= 0; index--) {
const item = records[index];
if (item.from && item.to) {
const from = moment(item.from);
const to = moment(item.to).add(1, 'days');
const today = moment();
console.log(today.format());
if (from <= today && today < to) {
result = item.value;
break;
}
}
}
return result;
}
Share
Improve this question
edited Jun 4, 2020 at 15:29
user3442470
asked Jun 4, 2020 at 15:18
user3442470user3442470
4392 gold badges7 silver badges21 bronze badges
6
- Can you add to a constant? I'm not sure of this rule in javascript, but I'd try making it non-constant. If this works, please notify me, and I'll 'promote' this ment to an answer. – JosephDoggie Commented Jun 4, 2020 at 15:20
- See also stackoverflow./questions/563406/add-days-to-javascript-date -- I'd just make the entity a 'var' not a constant … see my ment above – JosephDoggie Commented Jun 4, 2020 at 15:23
- 1 Hello, I tried using 'let' but tslint from Angular is saying that the value is never reassigned, which is true. Also I tried using var but tslint is telling me this: "Forbidden 'var' keyword, use 'let' or 'const' instead (no-var-keyword)". Also, is not working with var – user3442470 Commented Jun 4, 2020 at 15:26
-
1
@JosephDoggie
const
just means that the reference itself is immutable, it doesn't say anything about the mutability of the object it refers to. – Thomas Commented Jun 4, 2020 at 15:31 -
1
Consider using moment's
isSameOrBefore
andisBefore
functions rather than<=
and<
operators; that way you can tell it the level of granularity you want (right now it pares down to the millisecond, but you may only care about paring down to the day). – Heretic Monkey Commented Jun 4, 2020 at 15:44
3 Answers
Reset to default 4Try this one:
const to = moment("2020-06-30T00:00:00");
const toNextDay = moment(to.add(1, 'days').toDate());
As moment is modifying the original moment object, either use toString() or toDate() to get the modified date.
const to = moment("2020-06-30T00:00:00");
const toNextDay = moment(to.add(1, 'days').toDate());
console.log('In local time => ', toNextDay.toString());
const toUTC = moment.utc("2020-06-30T00:00:00");
const toNextDayUTC = moment.utc(toUTC.add(1, 'days').toDate());
console.log('In UTC => ', toNextDayUTC.toString());
<script src="https://momentjs./downloads/moment.min.js"></script>
Check the rest of the code because this part is correct
const to = moment("2020-06-30T00:00:00")
//undefined
to.format()
//"2020-06-30T00:00:00+02:00"
const nextDay = to.add(1, "day")
//undefined
nextDay.format()
//"2020-07-01T00:00:00+02:00"
to.format()
//"2020-07-01T00:00:00+02:00"
A little warning, Moment.add() mutates the moment so after to.add(1, "day") to and nextDay are the same date, 2020-07-01. Use to.clone().add(1, "day") if you don't want to lose the original moment
Try the following. Parse
the string using moment's second argument then use add()
to add the specified number of days
var input = "2020-06-30T00:00:00";
let addDay = moment(input, "YYYY-MM-DD hh:mm:ss").add(1, "days");
console.log(addDay.format("YYYY-MM-DDTHH:mm:ss"));
<script src="https://momentjs./downloads/moment.js"></script>
I have the following javascript:
const to = moment(item.to);
const toNextDay = to.add(1, 'days');
item.to is a string that has the following format:
"2020-06-30T00:00:00"
But is not adding the next day, that would be "2020-07-01T00:00:00"
This is the Function:
private getCurrentRecord(records: ConfigRecord[]) {
let result: string = null;
for (let index = records.length - 1; index >= 0; index--) {
const item = records[index];
if (item.from && item.to) {
const from = moment(item.from);
const to = moment(item.to).add(1, 'days');
const today = moment();
console.log(today.format());
if (from <= today && today < to) {
result = item.value;
break;
}
}
}
return result;
}
I have the following javascript:
const to = moment(item.to);
const toNextDay = to.add(1, 'days');
item.to is a string that has the following format:
"2020-06-30T00:00:00"
But is not adding the next day, that would be "2020-07-01T00:00:00"
This is the Function:
private getCurrentRecord(records: ConfigRecord[]) {
let result: string = null;
for (let index = records.length - 1; index >= 0; index--) {
const item = records[index];
if (item.from && item.to) {
const from = moment(item.from);
const to = moment(item.to).add(1, 'days');
const today = moment();
console.log(today.format());
if (from <= today && today < to) {
result = item.value;
break;
}
}
}
return result;
}
Share
Improve this question
edited Jun 4, 2020 at 15:29
user3442470
asked Jun 4, 2020 at 15:18
user3442470user3442470
4392 gold badges7 silver badges21 bronze badges
6
- Can you add to a constant? I'm not sure of this rule in javascript, but I'd try making it non-constant. If this works, please notify me, and I'll 'promote' this ment to an answer. – JosephDoggie Commented Jun 4, 2020 at 15:20
- See also stackoverflow./questions/563406/add-days-to-javascript-date -- I'd just make the entity a 'var' not a constant … see my ment above – JosephDoggie Commented Jun 4, 2020 at 15:23
- 1 Hello, I tried using 'let' but tslint from Angular is saying that the value is never reassigned, which is true. Also I tried using var but tslint is telling me this: "Forbidden 'var' keyword, use 'let' or 'const' instead (no-var-keyword)". Also, is not working with var – user3442470 Commented Jun 4, 2020 at 15:26
-
1
@JosephDoggie
const
just means that the reference itself is immutable, it doesn't say anything about the mutability of the object it refers to. – Thomas Commented Jun 4, 2020 at 15:31 -
1
Consider using moment's
isSameOrBefore
andisBefore
functions rather than<=
and<
operators; that way you can tell it the level of granularity you want (right now it pares down to the millisecond, but you may only care about paring down to the day). – Heretic Monkey Commented Jun 4, 2020 at 15:44
3 Answers
Reset to default 4Try this one:
const to = moment("2020-06-30T00:00:00");
const toNextDay = moment(to.add(1, 'days').toDate());
As moment is modifying the original moment object, either use toString() or toDate() to get the modified date.
const to = moment("2020-06-30T00:00:00");
const toNextDay = moment(to.add(1, 'days').toDate());
console.log('In local time => ', toNextDay.toString());
const toUTC = moment.utc("2020-06-30T00:00:00");
const toNextDayUTC = moment.utc(toUTC.add(1, 'days').toDate());
console.log('In UTC => ', toNextDayUTC.toString());
<script src="https://momentjs./downloads/moment.min.js"></script>
Check the rest of the code because this part is correct
const to = moment("2020-06-30T00:00:00")
//undefined
to.format()
//"2020-06-30T00:00:00+02:00"
const nextDay = to.add(1, "day")
//undefined
nextDay.format()
//"2020-07-01T00:00:00+02:00"
to.format()
//"2020-07-01T00:00:00+02:00"
A little warning, Moment.add() mutates the moment so after to.add(1, "day") to and nextDay are the same date, 2020-07-01. Use to.clone().add(1, "day") if you don't want to lose the original moment
Try the following. Parse
the string using moment's second argument then use add()
to add the specified number of days
var input = "2020-06-30T00:00:00";
let addDay = moment(input, "YYYY-MM-DD hh:mm:ss").add(1, "days");
console.log(addDay.format("YYYY-MM-DDTHH:mm:ss"));
<script src="https://momentjs./downloads/moment.js"></script>
本文标签: javascriptMoment is not adding dayStack Overflow
版权声明:本文标题:javascript - Moment is not adding day - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745558720a2156031.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论