1

i have from and to time based on railway time and now i converted into normal time AM to PM. but after that i need to add 30 minute interval. i'm little bit confused about iterator method. please help thanks in Advance

My code

let fromtime = '09:00:00'

let totime = '21:00:00'

let getGenTime = (timeString) => {
      let H = +timeString.substr(0, 2);
      let h = (H % 12) || 12;
      let ampm = H < 12 ? " AM" : " PM";
      return timeString = h + timeString.substr(2, 3) + ampm;
}

Now i have from and to time in am and pm format but i need to add 30 minutes interval between from and to time

  fromtime = getGenTime(fromtime) // 09:00 AM

  totime =  getGenTime(totime)   //  09:00 PM

Expected Result in console:

 09:00 AM
 09:30 AM
 10:00 AM
 10:30 AM
 11:00 AM
 ....
 .....
 08:30 PM
 09:00 PM
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Narayanan S
  • 117
  • 5
  • 14
  • 2
    Why don't you just use `Date` objects? – Barmar Nov 30 '17 at 21:57
  • 1
    And the `moment.js` libary should be helpful for formatting the times. – Barmar Nov 30 '17 at 21:57
  • @Barmar actually i get this from and to data from database so i thought over loop possible to add 30 mins . also since i'm new to JavaScript – Narayanan S Nov 30 '17 at 22:01
  • Please don't write obscure code. Future you or whoever is to maintain this code will not be happy with it. If you operate on dates, use [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). See an example of creating a range of dates: https://stackoverflow.com/questions/4345045/javascript-loop-between-date-ranges – rishat Nov 30 '17 at 22:02

1 Answers1

4

It would be better if you used Date object instead of strings, or used moment.js which provide many useful methods for this.

But any way it will need just a loop with some checks to achieve what you want, I made a function that will return an array of all times between your fromtime and totime:

function returnTimesInBetween(start, end) {
  var timesInBetween = [];
  var startH = parseInt(start.split(":")[0]);
  var startM = parseInt(start.split(":")[1]);
  var endH = parseInt(end.split(":")[0]);
  var endM = parseInt(end.split(":")[1]);

  if (startM == 30)
    startH++;
  for (var i = startH; i < endH; i++) {
    timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30");
  }
  timesInBetween.push(endH + ":00");
  if (endM == 30)
    timesInBetween.push(endH + ":30")

  return timesInBetween.map(getGenTime);
}

Demo:

let fromtime = '09:00:00'

let totime = '21:00:00'

let getGenTime = (timeString) => {
  let H = +timeString.substr(0, 2);
  let h = (H % 12) || 12;
  let ampm = H < 12 ? " AM" : " PM";
  return timeString = h + timeString.substr(2, 3) + ampm;
}



function returnTimesInBetween(start, end) {
  var timesInBetween = [];

  var startH = parseInt(start.split(":")[0]);
  var startM = parseInt(start.split(":")[1]);
  var endH = parseInt(end.split(":")[0]);
  var endM = parseInt(end.split(":")[1]);

  if (startM == 30)
    startH++;

  for (var i = startH; i < endH; i++) {
    timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30");
  }

  timesInBetween.push(endH + ":00");
  if (endM == 30)
    timesInBetween.push(endH + ":30")

  return timesInBetween.map(getGenTime);
}

console.log(returnTimesInBetween(fromtime, totime));

Edit:

To get Date objects from your fromtime and totime strings you can do it with Date constructor:

let fromtime = '09:00:00';
var d = new Date(Date.UTC(2017, 10, 10, parseInt(fromtime.split(":")[0]), parseInt(fromtime.split(":")[1])));

Just be careful with Locale and TimeZones, you may find some hour differents in the result.

Demo:

let fromtime = '09:00:00'

let totime = '21:00:00'

let getGenTime = (timeString) => {
  let H = +timeString.substr(0, 2);
  let h = (H % 12) || 12;
  let ampm = H < 12 ? " AM" : " PM";
  return timeString = h + timeString.substr(2, 3) + ampm;
}



function returnTimesInBetween(start, end) {
  var timesInBetween = [];

  var startH = parseInt(start.split(":")[0]);
  var startM = parseInt(start.split(":")[1]);
  var endH = parseInt(end.split(":")[0]);
  var endM = parseInt(end.split(":")[1]);

  if (startM == 30)
    startH++;

  for (var i = startH; i < endH; i++) {
    timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30");
  }

  timesInBetween.push(endH + ":00");
  if (endM == 30)
    timesInBetween.push(endH + ":30")

  timesInBetween.map(getGenTime);
  return timesInBetween.map(time => new Date(Date.UTC(2017, 10, 10, parseInt(time.split(":")[0]), parseInt(time.split(":")[1]))));
}

console.log(returnTimesInBetween(fromtime, totime));
cнŝdk
  • 31,391
  • 7
  • 56
  • 78