admin管理员组文章数量:1410705
I want to add two times like time1 = '00:05'
and time2 = '10:00'
. I want the result like the following after sum: result='10:05'
. I used moment for that, this is what I used:
let x = moment.duration(moment(this.time1, "hh:mm A").add(moment(this.time2, "hh:mm A")));
let result = moment.utc(x.asMilliseconds()).format('HH:mm:ss');
I want to add two times like time1 = '00:05'
and time2 = '10:00'
. I want the result like the following after sum: result='10:05'
. I used moment for that, this is what I used:
let x = moment.duration(moment(this.time1, "hh:mm A").add(moment(this.time2, "hh:mm A")));
let result = moment.utc(x.asMilliseconds()).format('HH:mm:ss');
but I got nothing, how can I do it?
Share edited Oct 25, 2019 at 16:47 Mohammed Saber Mohammed asked Oct 1, 2017 at 16:40 Mohammed Saber MohammedMohammed Saber Mohammed 3134 silver badges17 bronze badges 4-
1
'00:05 PM'
is a time, not a duration; it means five minutes past midnight, not just five minutes. And ask one question at a time. – jonrsharpe Commented Oct 1, 2017 at 16:48 - @jonrsharpe ok what u remend to do it. what should i use – Mohammed Saber Mohammed Commented Oct 1, 2017 at 16:50
- I remend you start by using a coherent representation of time. Without understanding why your inputs and expected outputs are what they are, it’s hard to say what you should do. Adding times makes no sense, which is why datetime libraries include duration/time delta types. – jonrsharpe Commented Oct 1, 2017 at 22:30
- I suggest you start by learning about the difference between a "time" and a "duration". – Code-Apprentice Commented Oct 25, 2019 at 16:49
3 Answers
Reset to default 3You can't add time this way with moment because you are asking it to add two times, not a time plus a duration. If you want to add ten minutes, use the add()
function with a duration.
moment(this.time2, "hh:mm A").add(10, 'minutes')
More here: https://momentjs./docs/#/manipulating/add/
It's not really clear in your question what 00:05 PM
means. That doesn't look like a valid time. Moment will interpret it as 12:05pm, but it looks like you want to interpret it as 5 minutes. (That's the only way you get 10:05 as an answer). You can do this with moment if you don't include the PM
part of the string.
moment.duration('00:05')
Is a duration of five minutes. You can add this to your time with:
moment('10:00 PM', '"hh:mm A"').add(moment.duration('00:05'))
// 22:05:00
Adding two periods works but it is currently not obvious in moment, how to format it like you want. Until they add format()
to durations this will have to do:
var d = moment.duration('03:10:10').add(moment.duration('01:20:30'))
moment.utc(d.as('milliseconds')).format("HH:mm:ss")
// '04:30:40'
See Example For Add & Diff using moment.js .. cheers
本文标签: javascriptHow To Get The Sum of Two Times Using MomentjsStack Overflow
版权声明:本文标题:javascript - How To Get The Sum of Two Times Using Moment.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744862940a2629175.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论