Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Sascha Manns
Added: Apr 17, 2019 2:35 PM
Views: 10
Tags: no tags
  1. function run() {
  2.   var one = "{{DAUEROFFIZIELL}}";
  3.   var two = "00:30:00";
  4.   var result = dateDiff(one, two);
  5.  
  6.   return result;
  7. }
  8.  
  9. // Based on https://www.codeproject.com/Answers/1267292/How-do-I-substract-time-from-another-time
  10. String.prototype.toHHMMSS = function () {
  11.     var sec_num = parseInt(this, 10); // dont forget the second parm
  12.     var hours = Math.floor(sec_num / 3600);
  13.     var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
  14.     var seconds = sec_num - (hours * 3600) - (minutes * 60);
  15.  
  16.     if (hours < 10) { hours = "0" + hours; }
  17.     if (minutes < 10) { minutes = "0" + minutes; }
  18.     if (seconds < 10) { seconds = "0" + seconds; }
  19.     var time = hours + ':' + minutes + ':' + seconds;
  20.     return time;
  21. }
  22.  
  23. function dateDiff(time1, time2) {
  24.     var t1 = new Date();
  25.     var parts = time1.split(":");
  26.     t1.setHours(parts[0], parts[1], parts[2], 0);
  27.     var t2 = new Date();
  28.     parts = time2.split(":");
  29.     t2.setHours(parts[0], parts[1], parts[2], 0);
  30.  
  31.     return (parseInt(Math.abs(t1.getTime() - t2.getTime()) / 1000)).toString().toHHMMSS();
  32. }