单击时间选择器时,如何获取当前时间? [英] How can I get the current time when the timepicker is clicked?

查看:101
本文介绍了单击时间选择器时,如何获取当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

  // TIME
  $('#timepicker').datetimepicker({
    minDate: minTime,
  })

    .on('dp.show', function(e){
    console.log(selectedDate);
    if($(this).val()==""){
      $(this).val(minTime.format("HH:mm"));
    }

    // If the date is not selected yet, Nothing to do... Onload min date applies
    if( typeof(selectedDate) == "undefined" ){
      console.log("Date undefined");

      // If a date is selected, check if it's the same date as the min date
    }else if( selectedDate == minDate.format("YYYY-MM-DD")){
      console.log("Date defined - SAME");
      $(this).data("DateTimePicker").minDate(minTime);

      // If the date is different from the min date
    }else{
      console.log("Date defined - Different");
      $(this).data("DateTimePicker").minDate(false);
    }
  })
    .val("");

演示和完整的代码如下: https://jsfiddle.net/oscar11/t0z3td1u/4 /

Demo and full code like this : https://jsfiddle.net/oscar11/t0z3td1u/4/

看来我的代码仍然不完美. minTime从小提琴的第一次执行开始. 单击时间选择器时应采取此方法.例如,我以15.00的价格运行您的jsfidde. 5分钟后,我选择时间选择器,结果固定为20.00.应该在20:05.

It seems my code is still not perfect. minTime is taken from the first time the fiddle executed. It should be taken when the timepicker is clicked. For example I run your jsfidde at 15.00. After 5 minutes, I choose timepicker, the result is fixed at 20.00. Should be at 20:05.

如何做到这一点,以便在单击时间选择器时可以获取当前时间?

How can I do it so that I can get the current time when the timepicker is clicked?

推荐答案

注意:此问题与 这个小提琴 .

Note: This question is related to a previous question from the OP and the accepted solution is in this Fiddle.

按照 Pedram 的正确说明,您只需在时间选择器打开时获取当前时间...并使用更好的输入价值.

As Pedram correctly stated, you just have to get the current time when the time picker opens... And use this better value for the input.

// TIME
$('#timepicker').datetimepicker({
  minDate: minTime,
})

  .on('dp.show', function(e){
  console.log(selectedDate);
  if($(this).val()==""){
    minTime = moment().add(offset, 'm');  // Just get the current time here!
    $(this).val(minTime.format("HH:mm"));
  }

 // ... (Rest is unchanged)

现在,您会说下拉菜单中的最短时间仍未更改...因此用户可以使用向下箭头选择20.00而不是20.05.

Now, you will say that the minimum time is still unchanged in the dropdown... So a user could use the arrow down to select 20.00 instead of 20.05.

可悲的是...一旦实例化dateTimePicker,就无法更改minTime之类的选项.这是自2014年5月起在GitHub上发布的问题.

Sadly... Once a dateTimePicker is instantiated, there is no way to change the options like minTime. That is an issue opened since may 2014 on GitHub.

我想你必须忍受它.

这篇关于单击时间选择器时,如何获取当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆