1

In my question I want to display values of 4 entities (I am getting those values from server side in milliseconds) in minutes. I can easily do using a custom filter. But I want to display this using a function, not a custom filter

itemTypes is the global variable where I am storing all parameter of all my items. I am getting all my data from a webservice in this global variable.

Now my code

$scope.itemTypes = itemTypes; // itemTypes is a global variable

$scope.selectedItem = {};  // In this variable I am storing all parameters of one particular item type.


if(selectedItemIndex > -1){
$scope.selectedItem = $scope.itemTypes[selectedItemIndex];
}

Now this $scope.selectedItem have 10 parameters whenever a partular iem type is selected.

4 of those parameters are in milliseconds, I need to display those 4 in minutes

I don't want to use a filter, instead I want to use a function called getTimeInMinute

I want to display only in minutes , not in seconds

This is my function

$scope.getTimeInMinute = function(timeInMilli){
var ms = timeInMilli;
ms = 1000*Math.round(ms/1000); // round to nearest second
var d = new Date(ms);
//console.log(d.getUTCMinutes());
var x = d.getUTCMinutes()
return x;
}

I got this function from this Stackoverflow question (link provided) Link 1

In my template I want to display like this

<div>{{getTimeInMinute(selectedItem.parameter1)}}</div>
<div>{{getTimeInMinute(selectedItem.parameter2)}}</div>
<div>{{getTimeInMinute(selectedItem.parameter3)}}</div>
<div>{{getTimeInMinute(selectedItem.parameter4)}}</div>

I am unable to display the data in milliseconds using function. I can easily do using a custom filter. But I want to display this using a function, not a custom filter

R. Richards
  • 24,603
  • 10
  • 64
  • 64
tommy123
  • 587
  • 1
  • 10
  • 28

2 Answers2

0

If you are getting the time in milliseconds and not a timestamp (milliseconds since 01/01/1970), don't make a Date from the milliseconds, instead just divide some more until you get minutes and then (maybe) round the number:

$scope.getTimeInMinute = function(timeInMilli){
  let seconds = timeInMilli/1000;
  let minutes = seconds/60;
  return Math.floor(minutes);
}

Here as an example without angular

function getTimeInMinute(timeInMilli){
  let seconds = timeInMilli/1000;
  let minutes = seconds/60;
  return Math.floor(minutes);
}

console.log(getTimeInMinute(1334000));

Here is an example with the numbers from your comment:

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
  $scope.getTimeInMinute = function(timeInMilli){
    let seconds = timeInMilli/1000;
    let minutes = seconds/60;
    return Math.floor(minutes);
  }
  
  $scope.selectedItem = {};
  
  $scope.selectedItem.parameter1 = 600000;
  $scope.selectedItem.parameter2 = 240000;
  $scope.selectedItem.parameter3 = 480000;
  $scope.selectedItem.parameter4 = 360000;
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <div>{{getTimeInMinute(selectedItem.parameter1)}}</div>
  <div>{{getTimeInMinute(selectedItem.parameter2)}}</div>
  <div>{{getTimeInMinute(selectedItem.parameter3)}}</div>
  <div>{{getTimeInMinute(selectedItem.parameter4)}}</div>
</div>
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • Luca Can you provide me a fiddle? Suppose my parameter1 value in ms is 600000, parameter2 is 240000, parameter3 is 480000 and parameter4 is 360000. I am facing problem while displaying in HTML template. It will great if you provide a fiddle with HTML and JS – tommy123 May 27 '18 at 13:40
  • OK. I have time, no problem – tommy123 May 27 '18 at 13:43
  • Luca thanks a lot. I have chosen your answer as the correct one. – tommy123 May 27 '18 at 13:49
  • Luca can you tell me why this convention is followed for milliseconds - 01/01/1970 – tommy123 May 27 '18 at 13:50
  • This may help you: https://stackoverflow.com/questions/1090869/why-is-1-1-1970-the-epoch-time – Luca Kiebel May 27 '18 at 13:51
  • I have tried this in JSFiddle . I am getting this https://jsfiddle.net/r6drwug3/1/ – tommy123 May 27 '18 at 13:53
  • You need to change the Load Type to something else than `onload` in the js settings (https://i.imgur.com/ugENpLZ.jpg) to make this work, sorry, doesn't seem to save it to the fiddle on edits – Luca Kiebel May 27 '18 at 13:58
  • can you please update my fiddle and provide me the updated link – tommy123 May 27 '18 at 14:00
  • can you put this in a working fiddle or Plunker and provide me a link. This is the last help I need from your side – tommy123 May 27 '18 at 14:02
  • https://jsfiddle.net/lucakiebel/qz0wa8zp/1/ This works for me at least. see the image I posted earlier: this is how you change the Load Type in jsfiddle – Luca Kiebel May 27 '18 at 14:03
  • I explained this multiple times now. The Load Type in jsfiddle has to be changed, or else angular will not run – Luca Kiebel May 27 '18 at 14:05
  • Ok now I got you. Thanks a lot. – tommy123 May 27 '18 at 14:07
0

Use the java script: moment.js libary

var x = 433276000
var tempTime = moment.duration(x);
var y = tempTime.hours() + tempTime.minutes();