0

Currently working on a project where I have to build time picker with start, end time and interval of meeting. User first pick start time for example 7:15am, then next step is to pick meeting interval that range from 5 min up to 60 min, and last step is end time that should start based on picked start time and meeting interval. So if user pick 7:30am for start time, and pick meeting interval 50 min, my end time should start at 8:20am, 9:10am, 10:00am,... all the way up to 5pm but not greater than. First problem with my current is Start Time picker, in drop down next to 12 hour value I should have PM. My code gives me AM. Second is End Time works fine if I pick meeting interval from 5 min up to 45 min, but if I pick meeting length 50 min or 55 min I'm not getting correct end time values in drop down.

HTML:

<tr>
  <th>Start Time:</th>
  <td>
    <select name="stime" id="stime" />
        <option value="">--Select start time--</option>
    </select>
  </td>
  <br />

  <th>Meeting Length:</th>
  <td>
    <select name="meet_leng" id="meet_leng" onClick="setEndTime()">
       <option value="">--Select length--</option>
    </select>
  </td>
  <br />

  <th>End Time:</th>
  <td>
    <select name="etime" id="etime"/>
      <option value="">--Select end time--</option>
    </select>
  </td>
</tr>

JavaScript:

$(function() {
    for(var i=5; i <= 60; i+=5){
        $('#meet_leng').append('<option value="'+i+'">'+i+' min'+'</option>');
    }

    for(var i=700; i<= 1700; i+=15){
        var mins = i % 100;
        var hours = parseInt(i/100);

      if (mins > 45) {
             mins = 0;
             hours += 1;
             i = hours * 100;
        }

        var standardTime = ' AM';

        if(hours > 12){
             standardTime = ' PM';
             hours %= 13;
             hours++;
        }else{
             hours %= 13;
        }

    $('#stime').append('<option value="'+i+'">'+('0' + (hours)).slice(-2)+':'+('0' +mins).slice(-2)+standardTime+'</option>');
            }
});


function setEndTime(){
  var meetingLength = $('#meet_leng').val();
    var selectedTime = $('#stime').val();
    var sum = meetingLength + selectedTime;

    for(var i=sum; i <= 1700; i+=meetingLength){
        var mins = i % 100;
        var hours = parseInt(i/100);

    if (mins > 59) {
        var new_mins = mins % 60;
      hours += 1;
      i = (hours * 100) + new_mins;
    }

    $('#etime').append('<option value="'+i+'">'+i+'</option>');
    }
 }

Here is my working example: https://jsfiddle.net/dmilos89/rhuh6qum/22/. If anyone can help with this problem please let me know.

JSON C11
  • 11,272
  • 7
  • 78
  • 65
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

1 Answers1

2

basically you could look at using the Date api instead. In the bottom example, we have a startTime which can be a string,

  1. we split it into integers
  2. create a new Date object and set the time to the startTime
  3. add your change in minutes
  4. then pull out the new hour/minutes and format as you please (i think there are ways to get this via the Date api, but i figured heavy handed was fine for the example)

https://jsfiddle.net/2fpg3rte/1/

var time = new Date();
var startTime = "12:01 PM";
var timeChange = 60; //60 minutes
var startHour = startTime.split(':')[0];
var startMin = startTime.split(':')[1].replace(/AM|PM/gi, '');

time.setHours(parseInt(startHour));
time.setMinutes(parseInt(startMin));

$("#start").html(getFormattedTime(time));

//adjusted time
time.setMinutes(time.getMinutes() + timeChange);
$("#end").html(getFormattedTime(time));

function getFormattedTime(time) {
 var postfix = "AM";
 var hour = time.getHours();
 var min = time.getMinutes();

 //format hours
 if (hour > 12) {
   hour = (hour - 12 === 0) ? 12 : hour - 12;
   postfix = hour === 0 ? "AM" : "PM";
 }

 //format minutes
 min = (''+min).length > 1 ? min : '0' + min;
 return hour + ':' + min + ' ' + postfix;
}
Brodie
  • 8,399
  • 8
  • 35
  • 55
  • I fixed my working example. Still like I said my AM/PM does not show correctly. Also if I add meeting length greater than 45min my end time drop down gives me wrong values. – espresso_coffee Feb 10 '16 at 14:51
  • sorry about that... so I did a little sample here of how you might do it with date https://jsfiddle.net/2fpg3rte/ not sure if it's exactly what you neeed, but it seems to work. you just create a date, set the initial time to your start time then add the delta in minutes (ex: 1hr 5min = 65min) and you have the adjusted time. – Brodie Feb 10 '16 at 18:20
  • Thanks a lot for this example. I think I will build my drop down with time values and then use that inside of the function. One more question, how I can get AM/PM next to each time value? Thanks in advance. – espresso_coffee Feb 10 '16 at 18:44
  • I think that start time is always AM no matter what. I tried to change but still shows AM. Do you know why? I tried to put for example 11:30 AM and add 50min to that, I got 12:20 AM instead of PM. – espresso_coffee Feb 10 '16 at 19:03
  • I made a few adjustments -- basically we need to normalize the startTime to be military time, then run it through the process, then we adjust for cases where the time is noon or higher and then for midnight: https://jsfiddle.net/2fpg3rte/2/ – Brodie Feb 10 '16 at 21:07
  • How I should set for loop to start from startTime and go up to 5pm with increment based on what user select. Should be something like this for(var i=startTime; i <= '5:00 PM'; i+=increment){ here output $('#endtime').append('')} or something else? – espresso_coffee Feb 10 '16 at 21:44
  • ya, you could just check the hour and as long as it's lte 17 (17:00 == 5:00pm) then add the minutes to the time ex: `time.setMinutes(interval);` then if the hour is 17 just force it to 5:00 because that's your upper bound anyways. If you're appending all the dom nodes you might considering looking at the DocumentFragment api and using that to create the list in a document fragment and then you just have to touch the DOM once to insert the entire list. But you can still build your html the same (just something to consider). – Brodie Feb 11 '16 at 15:09
  • Thanks a lot for your help. I will go and try that. – espresso_coffee Feb 11 '16 at 15:11
  • 1
    of course no problem :) if you have more questions feel free to pm me. – Brodie Feb 11 '16 at 15:41