dirkaka
dirkaka

Reputation: 118

Converting HH:MM String into Time Object

In my Ionic AngularJS Project, I have a string called startingTime set as 08:30

I will have a ng-repeat loop which will then make a series of items with an increasing time. For example , the first item in the list will have 08:30, second item in the list 09:00 etc ( the increment will depend on another variable so its not 30mins every loop )

The problem is I cant seem to turn the string into a Time Object and i get null displayed most of the time if i try the following

$scope.newStartTime = moment(startTime).format('HH-MM');
                 OR
$scope.newStartTime = var date = new Date(startTime);

So my 2 main questions are.

  1. How do i convert a string "08:30" into a time object ?
  2. After its a time object, how can i make a loop so the time increases by minutes after every loop ?

Thank you.

Upvotes: 1

Views: 2073

Answers (1)

NotBad4U
NotBad4U

Reputation: 1542

This method work for me, that transform your startingTime into a MomentObject :

 var startingTime = "8:30";
 var date = moment(startingTime, "HHmm");`
 console.log(date);

plunker demo

For create your timer you can make something like that :

$scope.timer = moment(startingTime,"HHmm");
$scope.setInterval = setInterval(function () { $scope.myTimer(); }, 1000);

$scope.myTimer = function () {
   $scope.timer = $scope.timer.add('s', 1);
};

Upvotes: 2

Related Questions