Created
August 5, 2019 14:51
-
-
Save nirajkvinit/68f59a1618254e56bb8647fd2b4419ce to your computer and use it in GitHub Desktop.
PeeTimer.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var moment = require("moment"); | |
/** | |
* | |
* @param {*} effHours Effective Hours input as 'hh:mm' | |
* @param {*} lastCheckin Last Checkin time input as 'hh:mm a' | |
* @param {*} minEffHours Minimum effective hours needed by default it is 7 hours | |
*/ | |
const peeTimeCalculator = (effHours, lastCheckin, minEffHours = "7") => { | |
if (!effHours) throw "Effective hours input is not provided"; | |
if (!lastCheckin) throw "Last check in time input is not provided"; | |
if (!minEffHours) throw "Minimum Effective hours input is not provided"; | |
let startTime = moment(lastCheckin, "hh:mma"); | |
if (effHours.includes(":")) { | |
effHours = effHours.split(":"); | |
startTime | |
.subtract(parseInt(effHours[0]), "hours") | |
.subtract(parseInt(effHours[1]), "minutes"); | |
} else { | |
startTime.subtract(parseInt(effHours), "hours"); | |
} | |
let endTime = startTime.clone(); | |
if (minEffHours.includes(":")) { | |
minEffHours = minEffHours.split(":"); | |
endTime | |
.add(parseInt(minEffHours[0]), "hours") | |
.add(parseInt(minEffHours[1]), "minutes"); | |
} else { | |
endTime.add(parseInt(minEffHours), "hours"); | |
} | |
let remainingTime = endTime.diff(moment(), "minutes"); | |
remainingTime = | |
Math.floor(remainingTime / 60) + | |
" hours " + | |
(remainingTime % 60) + | |
" minutes"; | |
console.log("You can leave by ", endTime.format("hh:mm a")); | |
console.log(remainingTime); | |
}; | |
let myargs = process.argv.slice(2); | |
if (myargs.length > 1) { | |
try { | |
peeTimeCalculator(myargs[0], myargs[1]); | |
} catch (error) { | |
console.error(error); | |
} | |
} else { | |
console.log(` | |
Please input the followings: \n | |
- Effective Hours: 2:57 \n | |
- Last In time: eg. 2:52PM \n | |
- (Optional) Average Hours: eh. 7.5 \n | |
`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment