moment.js添加不添加时间(moment.js add doesn't add time)

编程入门 行业动态 更新时间:2024-10-19 13:18:14
moment.js添加不添加时间(moment.js add doesn't add time)

当我做

----编辑----

var someMoment = moment('6:30 PM', ["h:mm A"]);

----结束编辑----

someMoment.add(30, 'minutes')

我似乎没有得到任何结果。

console.log(start); -- Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…} console.log(start.add(inc, 'minutes')); --Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…}

文档说添加变异指定的时刻,所以上面应该工作,但我也尝试过

var end = start.add(inc, 'minutes') console.log(end); --Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…}

我能做的就是这个

console.log(start.add(inc, 'minutes').format("h:mm A")); --7:00 PM

我想要的是花一点时间,加上30分钟,最好有一个提前30分钟的新时刻,或者至少让最初时刻提前30分钟。

我知道我可以把格式拿出来放在一个新的时刻,我想我会,但这似乎有点破碎。

----编辑----

使用时刻2.1

我从我的应用程序中的方法中得到这个,我没有在jsfiddle或任何东西中隔离它,但该方法需要一个字符串和一个增量。 我想我会把它贴在这里

在这里,我尝试一种方式,但我也尝试使用修改后的开始和克隆开始

var timeIsBetweenStartInc = function(_target:string, _start:string, inc:int){ var target = moment(_target, ["h:mm A"]); var start = moment(_start, ["h:mm A"]); console.log(start); var end = moment(start).add(inc, 'minutes'); console.log(end); return target.isBetween(start, end, 'minutes', '[)');(target, start, end); };

----结束编辑----

when I do

---- edit ----

var someMoment = moment('6:30 PM', ["h:mm A"]);

---- end edit ----

someMoment.add(30, 'minutes')

I don't seem to get any result.

console.log(start); -- Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…} console.log(start.add(inc, 'minutes')); --Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…}

The docs say that add mutates the specified moment, so the above should work but I've also tried

var end = start.add(inc, 'minutes') console.log(end); --Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…}

what I can do though is this

console.log(start.add(inc, 'minutes').format("h:mm A")); --7:00 PM

What I want is to take a moment, add 30 minutes to it and, preferably have a new moment that is 30 minutes ahead, or at least have the initial moment be 30 minutes ahead.

I know I can take the format out put and put it in a new moment, and I guess I will but this seems kind of broken.

---- edit ----

using moment 2.1

I am getting this from within a method in my app, I haven't isolated it in a jsfiddle or anything, but the method takes a string and a increment. I guess I'll paste it here

here i'm trying one way but I've also tried using the modified start and cloning the start

var timeIsBetweenStartInc = function(_target:string, _start:string, inc:int){ var target = moment(_target, ["h:mm A"]); var start = moment(_start, ["h:mm A"]); console.log(start); var end = moment(start).add(inc, 'minutes'); console.log(end); return target.isBetween(start, end, 'minutes', '[)');(target, start, end); };

---- end edit ----

最满意答案

Moment.js有一个clone方法,您可以像这样使用:

var now = moment(), future = now.clone();

然后你可以为克隆时刻增加一些时间:

future.add(30, "minutes");

现在,如果你比较两个时刻的UNIX时间戳......

future.unix() - now.unix();

你会注意到它们是1800秒=相隔30分钟。

Moment.js has a clone method that you could use like this:

var now = moment(), future = now.clone();

And then you could add some time to the cloned moment:

future.add(30, "minutes");

Now if you compare the UNIX timestamps of the two moments...

future.unix() - now.unix();

You'll notice they're 1800 seconds = 30 minutes apart.

更多推荐

本文发布于:2023-08-07 01:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:添加时间   js   moment   add   time

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!