简体   繁体   中英

How can I convert time into percent using moment

I need to convert a complete hour by hour into %, but I am not sure if moment is able to do that.

Eg.: I do have

let start = 08:00:00
// until
let end = 09:00:00

This is equal: 100%, but I receive an information from frontend value:

let data = 08:25:40

I need to know how much percent of hour have been passed from start, until the end.

I do have an workaround, but this is a little bit like a trick not a real solution.

Does anybody knows if JS has a library that can do this for me, easily?

Thanks.

Count the time difference from start to end and from start to your data in milisecond, then do simple math. Use diff method to calculate the time difference in milliseconds.

Here is a similar code snippet:

let start = 08:00:
let end = 09:00:00
let data = 08:25:40

const mStart = moment(start)
const mEnd = moment(end)
const mData = moment(data)

const percentile = 100.0 * mData.diff(mStart)/mEnd.diff(mStart)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM