HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Populating an array with times with half hour interval between them

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arraywithhourintervalpopulatingbetweentimesthemhalf

Problem

So for example, getTimes("12:00", "15:30"); should return:
["12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30"]

The input parameters are always in :30 or :00 intervals(so 06:00, 06:30, 07:00, ...).
getTimes("06:00", "17:00"); -> should work

getTimes("06:30", "17:00"); -> should work

getTimes("06:25", "17:00"); -> doesn't have to work because the from or until time can never be "06:25"

It also only has to work for the range 00:00 - 23:30 the same day.

What I have right now is this:

function getTimes(from, until){
            //"01/01/2001" is just an arbitrary date
            var until = Date.parse("01/01/2001 " + until);
            var from = Date.parse("01/01/2001 " + from);
            //*2 because because we want every 30 minutes instead of every hour
            var max = (Math.abs(until-from) / (60*60*1000))*2;
            var time = new Date(from);
            var hours = [];
            for(var i = 0; i <= max; i++){
                //doubleZeros just adds a zero in front of the value if it's smaller than 10.
                var hour = this.doubleZeros(time.getHours());
                var minute = this.doubleZeros(time.getMinutes());
                hours.push(hour+":"+minute);
                time.setMinutes(time.getMinutes()+30);
            }
            return hours;
}


How can this be improved?

Solution

Naming (more later)

I'd recommend renaming your function to something more specific than getTimes to avoid collisions with other getTimes functions. A more specific name also provides more information on what the function does.

function createHalfHourIntervals() {}


Looser Coupling

Another potential improvement would be to decouple your time formatting from your time interval creating. You could use a sleek functional programming technique to easily add the formatting after you've made the intervals:

function createHalfHourIntervals(from, until){
  // ...
  var time = new Date(from);
  var intervals = []; // more clear name than hours

  for (var i = 0; i <= max; i++) {
    intervals.push(time);
    time.setMinutes(time.getMinutes() + 30);
  }
}

function formatDateHHcolonMM(date) {  // funny name but specific
  var hour = date.getHours();
  var minute = date.getMinutes();
  return doubleZeros(hour) + ":" + doubleZeros(minute);
}

var intervals = createHalfHourIntervals()
  .map(formatDateHHcolonMM); // map will create a new array of formatted intervals from the array returned by createHalfHourIntervals


This separates your formatting logic from your interval creation logic, making your code more reusable and cohesive. If another programmer were less familiar with your code, but wanted to change the formatting of your intervals in someway, that task is made much simpler in this format. Each section of code doing one task, in general, can make the project easier to understand. However, if you don't plan on reusing much of the code in getTimes or working with other developers, sometimes it can just be simpler to keep all the code contained in one single location.

Good Optional Ideas

Although you may not want it now, it's possible that you may want to create arrays of intervals of different lengths in the future. Again, this depends on what you actually need, but 20min intervals or even interval times offset by 5min [7:05, 7:35] can be quite common. You could prepare better for the future change by reworking your function a bit. Your "01/01/2001 ", "*2" and "+30" are what some consider magic numbers, which tend to make the code less readable--sometimes even requiring their own comments. A good variable name oftentimes is better than a comment, so you may want to give these numbers their own variable names, improving clarity while preparing for a future refactoring:

function createHalfHourIntervals(from, until) {
  var arbitraryDay = "01/01/2001 ";
  // ...

  var intervalLength = 30;
  var intervalsPerHour = 60 / intervalLength;
  var milisecsPerHour = 60 * 60 * 1000;
  // are you sure that Math.abs(until - from) is the behavior you want. It seems like preparation to allow the user to put the 'from' and 'until' parameters in the other order.
}


That's pretty much all of your code! Hope you find this helpful, and keep up the good work!

Code Snippets

function createHalfHourIntervals() {}
function createHalfHourIntervals(from, until){
  // ...
  var time = new Date(from);
  var intervals = []; // more clear name than hours

  for (var i = 0; i <= max; i++) {
    intervals.push(time);
    time.setMinutes(time.getMinutes() + 30);
  }
}

function formatDateHHcolonMM(date) {  // funny name but specific
  var hour = date.getHours();
  var minute = date.getMinutes();
  return doubleZeros(hour) + ":" + doubleZeros(minute);
}

var intervals = createHalfHourIntervals()
  .map(formatDateHHcolonMM); // map will create a new array of formatted intervals from the array returned by createHalfHourIntervals
function createHalfHourIntervals(from, until) {
  var arbitraryDay = "01/01/2001 ";
  // ...

  var intervalLength = 30;
  var intervalsPerHour = 60 / intervalLength;
  var milisecsPerHour = 60 * 60 * 1000;
  // are you sure that Math.abs(until - from) is the behavior you want. It seems like preparation to allow the user to put the 'from' and 'until' parameters in the other order.
}

Context

StackExchange Code Review Q#128260, answer score: 4

Revisions (0)

No revisions yet.