将ionicTimePicker舍入到最接近的30分钟间隔

时间:2016-05-18 14:19:23

标签: javascript ionic-framework

ionicTimePicker中的inputTime组件舍入到最接近的30分钟间隔的最佳方法是什么?

我强烈认为必须改变这一点:

inputTime: ((new Date()).getHours() * 60 * 60 + (new Date()).getMinutes() * 60),

例如:

If current time is 15:17, then return 15:30
If current time is 15:45, then return 16:00

到目前为止我的代码:

$scope.openTimePicker = function () {
  var timePickerObject = {
    step: 30, //Optional
    callback: function (val) {
      if (typeof (val) === 'undefined') {
        console.log('Time not selected');
      } else {
        var selectedTime = new Date(val * 1000);
        console.log('Selected epoch is : ', val, 'and the time is ', selectedTime.getUTCHours(), 'H :', selectedTime.getUTCMinutes(), 'M');
      }
    },
    inputTime: ((new Date()).getHours() * 60 * 60 + (new Date()).getMinutes() * 60),
    format: 24
  };
  ionicTimePicker.openTimePicker(timePickerObject);
};

1 个答案:

答案 0 :(得分:3)

您可以使用这样的舍入函数:     

function getRoundedTime() {
    var d = new Date(); 
    var ratio = d.getMinutes() / 60;
    // Past 30 min mark, return epoch at +1 hours and 0 minutes
    if(ratio > 0.5){
        return (d.getHours() + 1) * 3600;
    }
    // Before 30 minute mark, return epoch at 0 minutes
    if(ratio < 0.5) {
         return d.getHours() * 3600;
    }
    // Right on the 30 minute mark, return epoch at 30 minutes
    return (d.getHours() * 3600) + 1800;
}

我尝试匹配((new Date()).getHours() * 60 * 60 + (new Date()).getMinutes() * 60)

给出的“纪元”格式

在这里你可以看到一些测试输出:

function getRoundedTime(inDate) {
        var d = new Date(); 
        if(inDate) {
          d = inDate;
        }
        var ratio = d.getMinutes() / 60;
        // Past 30 min mark, return epoch at +1 hours and 0 minutes
        if(ratio > 0.5){
            return (d.getHours() + 1) * 3600;
        }
        // Before 30 minute mark, return epoch at 0 minutes
        if(ratio < 0.5) {
             return d.getHours() * 3600;
        }
        // Right on the 30 minute mark, return epoch at 30 minutes
        return (d.getHours() * 3600) + 1800;
}

var a = new Date();
var b = new Date();
var c = new Date();
a.setMinutes(29);
b.setMinutes(30);
c.setMinutes(31);

var aNode = document.createTextNode("Time at " + a.getMinutes() + " minutes yields: " + getRoundedTime(a) + ", ");
var bNode = document.createTextNode("Time at " + b.getMinutes() + " minutes yields: " + getRoundedTime(b) + ", ");
var cNode = document.createTextNode("Time at " + c.getMinutes() + " minutes yields: " + getRoundedTime(c));

var target = document.getElementById("t");
target.appendChild(aNode);
target.appendChild(bNode);
target.appendChild(cNode);
<div id="t">
  
</div>

如果您总想要四舍五入到最近的半小时,请删除“30分钟前标记”条件,这样您最终会得到:

function getRoundedTime() {
    var d = new Date(); 
    var ratio = d.getMinutes() / 60;
    // Past 30 min mark, return epoch at +1 hours and 0 minutes
    if(ratio > 0.5){
        return (d.getHours() + 1) * 3600;
    }
    // Return epoch at 30 minutes past current hour
    return (d.getHours() * 3600) + 1800;
}