片刻不会添加分钟到从javascript日期创建的对象 [英] Moment not adding minutes to object created from javascript Date

查看:55
本文介绍了片刻不会添加分钟到从javascript日期创建的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法接受带有时间的javascript日期作为输入,并确定当前日期和时间是否在-30分钟内。但是,当我在运行时调试它时,moment.add似乎没有按预期使用分钟。

I have a method that accepts a javascript date with time as input, and determines if the current date and time is within -30 mins. However, when I debug this at runtime, moment.add doesn't seem to be working with minutes as expected.

function isWithinRange(myDate: Date){
    // convert to Moment obj
    let myMoment = moment(myDate);
    let todayMoment = moment(new Date());

    let myMomentOk = myMoment.isValid();
    let todayOk = todayMoment.isValid();

    // create range values
    let preTime = myMoment.subtract('m', 30);
    let postTime = myMoment.add('m', 30);

    //check values are as expected
    let localeTime = myDate.toLocaleString();]
    let preLocale = preTime.toLocaleString();
    let postLocale = postTime.toLocaleString();

    let result = todayMoment.isBetween(preTime, postTime);
    return result;

}

但当我检查时运行时的localeTime,preLocale和postLocale时间,所有三个值都是相同的,Tue Jun 26 2018 09:58:00 GMT-0400。加减分钟语句没有影响。

But when I inspect the localeTime, preLocale and postLocale times at run time, all three values are the same, "Tue Jun 26 2018 09:58:00 GMT-0400". The add and subtract minutes statements had no impact.

我在这里错过了什么或做错了什么?

What am I missing or doing wrong here?

推荐答案

请注意, add() 减去 改变原始时刻

add()


改变原文通过增加时间的时刻。

Mutates the original moment by adding time.

减去


改变减去时间的原始时刻。

Mutates the original moment by subtracting time.

所以你必须使用 clone()

so you have to use clone()

此外,在最近的版本中,第一个参数是时间量加/减,第二个参数是表示要添加的时间键的字符串

Moreover, in the recent version of moment, the first argument is the amount of time to add/subtract and the second argument is the string that represent the key of what time you want to add

这篇关于片刻不会添加分钟到从javascript日期创建的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆