1

I'm a code newbie so forgive me if the answer to this question is obvious!

I'm collecting JSON data from an API and I have a value, ExpectedDateTime, which I'd like to use to calculate the number of minutes and seconds from now.

It has the format: 2016-05-09T12:26:26

I've tried this:

 function applyTimeToVallingby(data) {
        $scope.timeToVallingby = 0;
        $scope.timeToVallingby2 = 0;
        d = new Date();
        for(i=0;i<data.ResponseData.Buses.length;i++){
            if(data.ResponseData.Buses[i].JourneyDirection === 2){
                if($scope.timeToVallingby===0){
                    $scope.timeToVallingby=(d-data.ResponseData.Buses[i].ExpectedDateTime);
                }else if($scope.timeToVallingby!=0&&$scope.timeToVallingby2===0){
                    $scope.timeToVallingby2=d-data.ResponseData.Buses[i].ExpectedDateTime;
                }
            }
        }
    }

But it doesn't work. I've tried to find a way to convert the new Date() value to something similar to the format of ExpectedDateTime, so that I can just subtract, but haven't been able to.

Best regards,

  • 1
    check it out: http://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site – Maxim Shoustin May 09 '16 at 11:02

3 Answers3

3

Kind of diff of time :

var date = new Date('2016-05-09T12:26:26');
var now = new Date();
alert(" Seconds from now : " + parseInt( (now.getTime() - date.getTime())/1000 ) );

In your way - d.getTime() - new Date( data.ResponseData.Buses[i].ExpectedDateTime).getTime()

xAqweRx
  • 1,236
  • 1
  • 10
  • 23
0

Maybe you could use Moment.js library:

$scope.daysLeft = function (end_date) {
        var now = moment();
        var then = moment(end_date);
        var diff = then.diff(now, 'days');

        if(diff <= 0)
            return 0;

        return diff;
}
notgiorgi
  • 1,691
  • 12
  • 27
0

You need to first convert ExpectedDateTime to a Date Object

var expectedDateTime = "2016-05-09T12:26:26";

var items = expectedDateTime.split("T");
var dates = items[0].split("-");
var times = items[1].split(":");

var expectedDateObj = new Date( dates[0], dates[1]-1, dates[2], times[0], times[1], times[2] );

Now simple get the number of Milliseconds difference from now and this expectedDateObj object

var now = new Date();
var noOfMS = now.getTime() - expectedDateObj.getTime();
var numberOfSeconds = noOfMS/1000;

var noOfMinAndSec = "Min = " + numberOfSeconds/60 + " Sec = " + numberOfSeconds%60;

DEMO

var expectedDateTime = "2016-05-09T12:26:26";

var items = expectedDateTime.split("T");
var dates = items[0].split("-");
var times = items[1].split(":");

var expectedDateObj = new Date( dates[0], dates[1]-1, dates[2], times[0], times[1], times[2] );



var now = new Date();
var noOfMS = now.getTime() - expectedDateObj.getTime();
var numberOfSeconds = Math.floor(Math.abs(noOfMS/1000));

var noOfMinAndSec = "Min = " + parseInt(numberOfSeconds/60) + " Sec = " + numberOfSeconds%60;

alert( noOfMinAndSec );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94