2017-05-08 2 views
0

Ich möchte Stunden in Javascript berechnen, aber es scheint, es ist ein sehr langer Code. nicht für ganze Stunden, wenn es um halbe Stunden geht, wird es kompliziert. Das ist mein Code bisher.Berechnung von Stunden mit if-Anweisung

<script> 
$(function(){ 
    $('#intime,#outtime').on('input', function() { 
     // declare variables 
     var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime 
     var outtime= new moment($("#outtime").val(), 'HH:mm:ss'); 

     var startTime = new moment("08:00:00", 'HH:mm:ss'); 
     var halfDay = new moment("12:30:00", 'HH:mm:ss'); 
     var wholeDay = new moment("18:00:00", 'HH:mm:ss'); 


     var othours = 0; // declare day as 0 
     /*AFTER 18.00*/ 
     if (moment(intime).hour() == 8 && (moment(outtime).hour() == 18 && moment(outtime).minute() == 30)) { 
      othours = 0.5; 
     } 
     else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 19 && moment(outtime).minute() == 30)) { 
      othours = 1.5; 
     } 
     else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 20 && moment(outtime).minute() == 30)) { 
      othours = 2.5; 
     } 
     else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 21 && moment(outtime).minute() == 30)) { 
      othours = moment(outtime).hour() && moment(outtime).minute() - moment(wholeDay).hour() && moment(wholeDay).minute(); 
     } 
     else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 22 && moment(outtime).minute() == 30)) { 
      othours = 4.5; 
     } 
     else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 23 && moment(outtime).minute() == 30)) { 
      othours = 5.5; 
     } 
     else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) { 
      othours = 6.5; 
     } 
     /*BEFORE 8.00*/ 
     else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) { 
      othours = 6.5; 
     } 
     else if (moment(intime).hour() == 8 && (moment(outtime).hour() == 00 && moment(outtime).minute() == 30)) { 
      othours = 6.5; 
     } 

     /*WHOLE HOURS*/ 
     else if (moment(intime).hour() == 8 && moment(outtime).hour() > 18) { 
      othours = moment(outtime).hour() - moment(wholeDay).hour(); 
     } 
     else if (moment(intime).hour() < 8 && moment(outtime).hour() > 18) { 
      othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour(); 
     } 
     else if (moment(intime).hour() < 8 && moment(outtime).hour() == 18) { 
      othours = moment(startTime).hour() - moment(intime).hour(); 
     } 
     $("#othours").val(othours); 

    }); 
}) 
</script> 

Gibt es eine kürzere Methode oder muss ich für andere halbe Stunden so weitermachen? Wenn intime = 6.30 und outtime = 19.30, sollten die anderen drei Stunden sein.

enter image description here

das ist mein Ausgang. Ich möchte berechnen Stunden OT, wenn ich in Zeit eingeben und out-Zeit

+0

Was möchten Sie in Ihrer Ausgabe ist es sehr komplex Ihren Code. Ich kann nicht verstehen, was Sie wollen ... –

+0

Für Anfänger nicht die gleichen Funktionen immer wieder aufrufen, wenn Sie sie in einfachen Variablen einmal speichern können – charlietfl

+0

Aber was versuchst du zu erreichen? – Picard

Antwort

0

Try this -

<script> 
$(function(){ 
    $('#intime,#outtime').on('input', function() { 
     // declare variables 
     var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime 
     var outtime= new moment($("#outtime").val(), 'HH:mm:ss'); 

     var othours = 0; // declare day as 0 

     othours = moment.utc(moment(outtime,"DD/MM/YYYY HH:mm:ss").diff(moment(intime,"DD/MM/YYYY HH:mm:ss"))).format("HH") 
     if(othours>10) 
      $("#othours").val(othours-10); 
     else 
      $("#othours").val("Whole Day not work"); 

    }); 
}) 
</script> 
+0

ich wollte auch minuten berechnen. Danke trotzdem :) – Lisa234

+0

Sie ändern nur Format ("HH: mm") –

+0

intime = Moment (intime) .add (10, 'Stunden'); Othours = Moment.utc (Moment (outtime, "TT/MM/JJJJ HH: mm: ss"). Diff (Moment (intime, "TT/MM/JJJJ HH: mm: ss"))). "HH: mm"); $ ("# othours"). Val (othours); –

0

das funktioniert mit dem, was ich suchte.

<script> 
$(function(){ 
    $('#intime,#outtime').on('input', function() { 
     // declare variables 
     var intime = new moment($("#intime").val(), 'HH:mm:ss'); // get value of intime 
     var outtime= new moment($("#outtime").val(), 'HH:mm:ss'); 

     var startTime = new moment("08:00:00", 'HH:mm:ss'); 
     var wholeDay = new moment("18:00:00", 'HH:mm:ss'); 

     var othours = 0; // declare othours as 0 

     if (moment(intime).hour() == 8 && (moment(outtime).hour() > 18 && moment(outtime).minute() == 30)) { 
      othours = moment(outtime).hour() - moment(wholeDay).hour() + 0.5; 
     } 
     else if (moment(intime).hour() == 8 && moment(outtime).hour() > 18) { 
      othours = moment(outtime).hour() - moment(wholeDay).hour(); 
     } 
     else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && (moment(outtime).hour() > 18 && moment(outtime).minute() == 30)) { 
      othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour(); 
     } 
     else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && moment(outtime).hour() == 18) { 
      othours = moment(startTime).hour() - moment(intime).hour() - 0.5; 
     } 
     else if (moment(intime).hour() < 8 && moment(outtime).hour() == 18) { 
      othours = moment(startTime).hour() - moment(intime).hour(); 
     } 
     else if ((moment(intime).hour() < 8 && moment(intime).minute() == 30) && moment(outtime).hour() > 18) { 
      othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour() - 0.5; 
     } 
     else if (moment(intime).hour() < 8 && (moment(outtime).hour() > 18) && moment(outtime).minute() == 30) { 
      othours = moment(startTime).hour() - moment(intime).hour() + moment(outtime).hour() - moment(wholeDay).hour() + 0.5; 
     } 

     $("#othours").val(othours); 

    }); 
}) 
</script>