简体   繁体   中英

jquery timepicker time start issue

I have and timepicker in my code. code is given below.

   function getCurrentTime(date) {
    var hours = date.getHours(),
        minutes = date.getMinutes(),
        ampm = hours >= 12 ? 'pm' : 'am';

  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;

  return hours + ':' + minutes + ' ' + ampm;
}
 

    var cur_time = new Date();
            cur_time.setHours(cur_time.getHours() + 1);
            $('.pickup_time').timepicker({
                timeFormat: 'hh:mm p',
                interval: 30,
                minTime: getCurrentTime(new Date()),
                // minTime: '1',
                // maxTime: '11:30pm', 
                // startTime: '01:00',
                dynamic: false,
                dropdown: true,
                scrollbar: true           
            });
            $('.pickup_time').timepicker('setTime', cur_time);

Using this code i am getting dropdown like which is given below in image .what i exactly want is i want to start the time from the 2:00 when its 1:43. and if time is 1:25 i want it toi start from 01:30.

在此处输入图片说明

Help needed on this can any one have idea about this ???

In order to round the current time to the next half hour, adjust your getCurrentTime() function as follows:

function getCurrentTime(date) {
  // extract hours and minutes from given date
  var hours = date.getHours(),
    minutes = date.getMinutes();

  // round minutes to half hour or next hour
  if (minutes <= 30) {
    minutes = "30";
  } else {
    minutes = "00";
    hours = (hours + 1) % 24; // loop on 24 hours
  }

  // calc if morning or afternoon
  var ampm = hours >= 12 ? "pm" : "am";

  // adjust hour to 12h format
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'

  return hours + ":" + minutes + " " + ampm;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM