|
31 | 31 | * 3) Else return false * |
32 | 32 | * * |
33 | 33 | ***************************************************************************************/ |
34 | | -function CountingMinutesI(str) { |
35 | | - var time1Obj = {}, time2Obj = {}, timeDiff; |
36 | | - |
37 | | - time1Obj = setTimeObject(str, 0); |
38 | | - time2Obj = setTimeObject(str, 1); |
39 | | - |
40 | | - if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot > time2Obj.tot) { |
41 | | - timeDiff = (((12 - time1Obj.hours + 12) * 60) - (time1Obj.mins)) + ((time2Obj.hours * 60) + time2Obj.mins); |
42 | | - } |
43 | | - else if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot < time2Obj.tot) { |
44 | | - timeDiff = ((time2Obj.hours * 60) + time2Obj.mins) - ((time1Obj.hours * 60) + time1Obj.mins); |
45 | | - } |
46 | | - else if (time1Obj.ampm !== time2Obj.ampm && time1Obj.ampm === "am") { |
47 | | - timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins); |
48 | | - } |
49 | | - else { |
50 | | - timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins); |
51 | | - } |
52 | 34 |
|
53 | | - return timeDiff; |
| 35 | + |
| 36 | + |
| 37 | +const createTimeObj = time => time && ({ |
| 38 | + hour: Number(time.split(':')[0]) || 0, |
| 39 | + minutes: Number(time.split(':')[1].slice(0, -2)) || 0, |
| 40 | + suffix: time.toUpperCase().search(/PM/) !== -1 ? 'PM' : 'AM', |
| 41 | +}) |
| 42 | + |
| 43 | +const timeTo24Sys = t => { |
| 44 | + if (!t) return |
| 45 | + |
| 46 | + if (t.hour === 12 && t.suffix === 'AM') return 0 |
| 47 | + if (t.hour !== 12 && t.suffix === 'PM') return t.hour + 12 |
| 48 | + |
| 49 | + return t.hour |
| 50 | +} |
| 51 | + |
| 52 | +const constructTime = time => time && new Date(_year = 0, _month = 0, _day = 0, timeTo24Sys(time), time.minutes) |
| 53 | + |
| 54 | +const calcTimeDiff = (t1, t2) => { |
| 55 | + if (!t1 || !t2) return |
| 56 | + |
| 57 | + const diff = (constructTime(t2) - constructTime(t1)) / 60 / 1000 |
| 58 | + |
| 59 | + return Math.round(diff >= 0 ? diff : diff + (24 * 60)) |
54 | 60 | } |
55 | | - |
56 | | -function setTimeObject(str, num) { |
57 | | - var arr = str.split("-"); |
58 | | - var tObject = {}; |
59 | | - |
60 | | - tObject.hours = Number(arr[num].slice(0,arr[num].length-2).split(":")[0]); |
61 | | - tObject.mins = Number(arr[num].slice(0,arr[num].length-2).split(":")[1]); |
62 | | - tObject.ampm = arr[num].slice(-2); |
63 | | - tObject.tot = tObject.hours * 100 + tObject.mins; |
64 | | - |
65 | | - return tObject; |
| 61 | + |
| 62 | +const calcTime = (str) => { |
| 63 | + if (!str) return |
| 64 | + const time = str.split('-') |
| 65 | + |
| 66 | + const t1 = createTimeObj(time[0]) |
| 67 | + const t2 = createTimeObj(time[1]) |
| 68 | + |
| 69 | + return calcTimeDiff(t1, t2) |
66 | 70 | } |
| 71 | + |
| 72 | +console.log(calcTime('03:00PM-04:00PM') === 60) |
| 73 | +console.log(calcTime('03:00AM-11:00AM') === 480) |
| 74 | +console.log(calcTime('11:30AM-12:00PM') === 30) |
| 75 | +console.log(calcTime('12:30PM-01:00PM') === 30) |
| 76 | +console.log(calcTime('12:00AM-11:00AM') === 660) |
| 77 | +console.log(calcTime('06:00PM-02:00PM') === 1200) |
| 78 | +console.log(calcTime('11:00PM-12:00AM') === 60) |
| 79 | +console.log(calcTime('11:30PM-12:00AM') === 30) |
| 80 | +console.log(calcTime('11:30AM-12:30AM') === 780) |
| 81 | +console.log(calcTime('01:30AM-01:00AM') === 1410) |
| 82 | +console.log(calcTime('11:00AM-12:00PM') === 60) |
0 commit comments