如何在datetimepicker中添加基于小时的最少天数? [英] How can I add a minimum of days based on hour in datetimepicker?

查看:91
本文介绍了如何在datetimepicker中添加基于小时的最少天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码javascript如下:

My code javascript like this :

$(function () {    
  $('#datepicker').datetimepicker({
    minDate: moment().startOf('day')
  }).on('dp.change', function(e){
    if( e.date && e.date.isSame(moment(), 'd') ){
      var min = moment().startOf('minute').add(300, 'm');
      $('#timepicker').data("DateTimePicker").minDate(min);
    } else {
      $('#timepicker').data("DateTimePicker").minDate(false);
    }
  });

  $('#timepicker').datetimepicker({
    minDate: moment().startOf('minute').add(300, 'm'),
  });
});

演示和完整代码如下: https://jsfiddle.net/tyvetr5h/

Demo and full code like this : https://jsfiddle.net/tyvetr5h/

我的问题:

例如现在8月16日晚上9点。由于看来显示的日期是8月17日凌晨2点。但我的代码是8月16日凌晨2点

For example now on 16 August at 9 pm. As it appears that the date displayed is 17 August at 2 am. But my code is August 16 at 2 am

我该如何解决?

我尝试更新这样的一天:

I try to update the day like this :

minDate: moment().startOf('day').add(300, 'm')

但它是一样的

推荐答案

在实例化时,您必须将300分钟添加到DP mindate 。由于这个时刻是当前的时间和日期,这与添加它是相关的。

You have to add the 300 minutes to the DP mindate when instantiating too. Since this "moment" is the current time and date, that is relevant to add it.

我和你的时区不一样,所以我不得不添加更多不到300分钟让它明天落下......但这是你的 更新的小提琴 。我删除了所有 .startOf() ,我认为与您尝试实现的目标无关。

I'm not the same timezone as you, so I had to add more than 300 minutes to make it fall on tomorrow... But here is your updated Fiddle. I removed all .startOf(), which I think is irrelevant in what you try to achieve.

编辑

我发现诀窍。

EDIT
I found the trick.

问题在于你改变了时刻值...所以当是时候比较日期时,最好以Unix格式来做,是从1970年1月1日开始的毫秒......

The thing is that you change the moment value... so when it is time to compare the dates, better do it in "Unix format", which is in milliseconds from january 1st 1970...

这个长整数并不关心日变化。因此,如果时间是300分钟,则比较容易(和防错)。

That long integer doesn't care about the day change. So it is easier (and error proof) to compare if the time is 300 minutes from now.

$(function () {

  console.clear();

  $('#datepicker').datetimepicker({
    minDate: moment().add(300, 'm').startOf("day")
  })
    .val(moment().add(300, 'm').format("DD-MM-YYYY"))   // To set correct "valid" date from start

    .on('dp.change', function(e){
    var now = moment().format("X");
    //console.log(e.date.format("X"));
    //console.log(now);

    // Comparing selected date (moment object) in milliseconds
    if(e.date.format("X")+(300*60*1000) > now ){
      console.log("above 5 hours, unlocking time.");
      $('#timepicker').data("DateTimePicker").minDate(false);
    }else{
      console.log("below 5 hours, locking time (and probably resetting the value to min...)");
      $('#timepicker').data("DateTimePicker").minDate(moment().add(300, 'm'));
    }

  });

  $('#timepicker').datetimepicker({
    minDate: moment().add(300, 'm'),
  });
});

CodePen

这篇关于如何在datetimepicker中添加基于小时的最少天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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