-1
$.getJSON("api/times", function(times) {
    var time1 = times.departure; // 14:36:30 (string)
    var minutesToAdd = parseInt(times.minutesToAdd); // 6
    var total = "?"; // How can I add minutesToAdd to time1?
});

I am trying to parse time1 so that I can add the minutesToAdd value to the time. The time1 value comes directly from a MySQL database. It is obtained through an API that returns this data in JSON. How can I do this?

jskidd3
  • 4,609
  • 15
  • 63
  • 127
  • @JohanVandemRym That does not fully answer my question. Date.parse() parses a date, I only have a time value. – jskidd3 Apr 01 '14 at 10:48

2 Answers2

1

I you don't want to use Date, use split to get the three parts of your time string. Then you just add your minutes and make sure you don't exceed 59 minutes (if you think it is useful, check if hours doesn't exceed 23) :

$.getJSON("api/times", function(times) {
    var time1 = times.departure; // 14:36:30 (string)
    var minutesToAdd = parseInt(times.minutesToAdd);
    var time = time1.split(':');
    var time[1] += minutesToAdd;
    if (time[1] >= 60) {
        time[1] = 0;
        time[0]++; // maybe you should test if >= 24
    }
    var total = time.join(':'); // equivalent to "time[0]+':'+time[1]+':'+time[2]"
});`
Apolo
  • 3,844
  • 1
  • 21
  • 51
  • Here they do something similar to calculate difference with two time strings: http://stackoverflow.com/questions/17157337/how-do-i-calculate-the-time-difference-between-strings-in-javascript – JohanVdR Apr 01 '14 at 11:00
1

I think you can try this

on line 1 : You have to add a demo date with the time in javascript so that you can create the date object. Later you can do date.getTime() to get the time in milisecond.

var newDateObj = new Date("01/01/13 14:36:30"); // '01/01/13' is the demo value
alert(newDateObj)
var date = new Date(newDateObj.getTime() + 5*60000);
alert(date) // date.getTime() will give you the time
A Paul
  • 8,113
  • 3
  • 31
  • 61