Javascript 一刻不算一天
我有以下javascript:Javascript 一刻不算一天,javascript,momentjs,Javascript,Momentjs,我有以下javascript: const to = moment(item.to); const toNextDay = to.add(1, 'days'); item.to是具有以下格式的字符串: "2020-06-30T00:00:00" 但是没有添加第二天,那将是“2020-07-01T00:00:00” 这就是功能: private getCurrentRecord(records: ConfigRecord[]) { let result: string = null
const to = moment(item.to);
const toNextDay = to.add(1, 'days');
item.to是具有以下格式的字符串:
"2020-06-30T00:00:00"
但是没有添加第二天,那将是“2020-07-01T00:00:00”
这就是功能:
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;
}
private getCurrentRecord(记录:ConfigRecord[]){
let result:string=null;
对于(让index=records.length-1;index>=0;index--){
常数项=记录[索引];
if(item.from&&item.to){
常数自=力矩(项目自);
常数至=时刻(项目至)。添加(1,'天');
const today=时刻();
console.log(today.format());
如果(从检查代码的其余部分,因为这部分是正确的
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"
一个小小的警告,Moment.add()会使to.add(1,“day”)to和nextDay之后的时间发生变化,即2020-07-01。使用to.clone().add(1,“day”)如果您不想失去原始时间,请尝试以下方法:
const to = moment("2020-06-30T00:00:00");
const toNextDay = moment(to.add(1, 'days').toDate());
由于矩正在修改原始矩对象,请使用toString()或toDate()获取修改的日期
const to=力矩(“2020-06-30T00:00:00”);
const toNextDay=时刻(to.add(1,'days').toDate());
log('In local time=>',toNextDay.toString());
const toUTC=时刻utc(“2020-06-30T00:00:00”);
const toNextDayUTC=矩.utc(toUTC.add(1,'days').toDate());
console.log('In UTC=>',toNextDayUTC.toString());
尝试以下操作。使用矩的第二个参数解析字符串,然后使用add()
添加指定的天数
var input=“2020-06-30T00:00:00”;
设addDay=时刻(输入“YYYY-MM-DD hh:MM:ss”)。添加(1,“天”);
console.log(addDay.format(“YYYY-MM-DDTHH:MM:ss”);
你能添加一个常量吗?我不确定javascript中的这个规则,但我会尝试将其设置为非常量。如果这样做有效,请通知我,我会将此注释“升级”为一个答案。另请参见——我只会将实体设置为“var”而不是常量……请参见上面我的注释您好,我尝试使用“let”,但Angular的tslint这么说值从未被重新分配,这是真的。我也尝试过使用var,但tslint告诉我:“禁止使用‘var’关键字,改用‘let’或‘const’(没有var关键字)”.而且,他不与var@JosephDoggie<代码> const 只是意味着引用本身是不可变的,它没有提到它引用的对象的可变性。考虑使用矩和函数而不是<代码>不能完成,它必须是IM对象的一个时刻对象,以评估是否比现在更大。(这也是一个瞬间对象)7月2日回来了。我想地区问题就在那里。我得到了7月1日。我想你指的是时区问题。地区包括语言和国家…是的。谢谢你的更正。可能是时区问题。7月2日回来了。