简体   繁体   中英

Convert final two digits to a percent value over 60. (e.g., 15 / 60 => .25 etc)

I have given numbers like these:

0915
0930
0945
1000
0015

They should be rounded and look like this:

0900 => 9
0915 => 9.25
0930 => 9.5
0945 => 9.75
1000 => 10
0015 => 0.25

I tried a lot of functions like round and ceil but i cant get it working. It is for a calendar, the given numbers are the times where the meetings start like '09:45'.

You could calculate the minutes by division with 60.

 var data = ['0915', '0930', '0945', '1000', '0015'], result = data.map(function (a) { return +a.slice(0, 2) + a.slice(2, 4) / 60; }); console.log(result); 

n.substr(0,2)*1 + n.substr(2,2)/60

will give you the expected result

where n is a string representing time in the format HHmm where HH is the hour, mm minutes as in the data you posted as example. Both value must be two digits numbers forming a 4 digits string.

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