因此,优质时间似乎全部在上午,而非全部在下午,所以只需从 12 中减去他们给出的下午时间,在您的示例中为 12-9,您得到 3 小时,然后将上午的任何时间添加到 0,再次您的示例 0+2,当然是 2 小时。然后乘以 60 得到分钟。一旦您将每个金额乘以所收取的金额,然后将它们加在一起。我会让 am/pm 成为一个切换开关,将每个变量设置为 true/false。
代码可能如下所示:
//start_time and end_time are objects that have 2 properties, am/pm
//which is a bool and time which is a int
function calc_time(start_time,end_time){
var normal_pay;
var premium_pay;
if(start_time.pm == true){
normal_pay = 12 - start_time.time;
normal_pay = normal_pay * 60;
} else{
normal_pay = 0 + start_time.time;
normal_pay = normal_pay * 60 * 2; //this is premium time the whole night was premium pay
}
if(end_time.am == true){
premium_pay = 0 + end_time.time;
premium_pay = premium_pay * 60 * 2;
} else {
premium_pay = 12 - end_time.time;
premium_pay = premium_pay * 60; //this is normal time the whole night was normal pay
}
return premium_pay + normal_pay;
}
这并不能解决你仍然需要考虑的所有问题,比如分钟,比如他们想在凌晨 3:25 结束,但这并不难。