admin管理员组文章数量:1384131
There is a start date for the process in string form:
'2020-03-02 06:49:05'
And the process pletion date:
'2020-03-02 07:05:02'
Question:
What is the most correct way from the point of view of the approach - to calculate the difference (in minutes) between the start and finish of the process?
(if there are any built-in methods for this in vue.js
ornuxt.js
, it will be very interesting to learn about them as well.)
There is a start date for the process in string form:
'2020-03-02 06:49:05'
And the process pletion date:
'2020-03-02 07:05:02'
Question:
What is the most correct way from the point of view of the approach - to calculate the difference (in minutes) between the start and finish of the process?
(if there are any built-in methods for this in vue.js
ornuxt.js
, it will be very interesting to learn about them as well.)
- 1 how about using some library like momentjs[momentjs./docs/] – adil hussain Commented Mar 2, 2020 at 6:18
- Yes. The best method is to not do the calculation yourself and use a library. And also to think next time that this is such a mon problem that this question might have been asked 1000 times on stackoverflow. So let me first check and then ask this question. – vishal Commented Mar 2, 2020 at 6:43
3 Answers
Reset to default 6I think the best way would be to use Javascript Date object,
d1 = '2020-03-02 06:49:05'
d2 = '2020-03-02 07:05:02'
diff_in_millis = Math.abs(new Date(d1) - new Date(d2))
diff_in_minutes = diff/60000
I suggest using momentjs, you can do something like this:
var duration = moment.duration(endTime.diff(startTime));
var minutes = duration.minutes();
More about duration in momentjs can be found here
Create the date from the string using Date.parse(). It return the date in milliseconds, get the difference and convert that to Minutes.
See snippet below.
const startTime= Date.parse('2020-03-02 06:49:05')
const endTime = Date.parse('2020-03-02 07:05:02')
// Difference in Minutes
const durationInMin= (endTime-startTime)/60000;
console.log({ startTime, endTime, durationInMin })
alert(`Process took: ${durationInMin} minutes`)
Note: For human readable dates, I have found date-fns to be the most helpful. Given its lightweight pared to momentjs. And you could plete the same with the following.
import { differenceInMinutes } from 'date-fns';
const startDate = '2020-03-02 06:49:05';
const endDate = '2020-03-02 07:05:02';
const durationInMin = differenceInMinutes( new Date(endDate), new Date(startDate));
console.log(`Duration: ${durationInMin} minutes`);
At the cost of another dependence to the project of course, but if you are handling lots of human readable dates, it's worth it.
本文标签: javascriptHow to correctly calculate the number of minutes from a certain momentStack Overflow
版权声明:本文标题:javascript - How to correctly calculate the number of minutes from a certain moment? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744535677a2611300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论