Javascript jQuery-检查当前日期和时间是否在所选日期和时间的24小时内

Javascript jQuery-检查当前日期和时间是否在所选日期和时间的24小时内,javascript,jquery,date,Javascript,Jquery,Date,我目前拥有以下jQuery:- $('#datepicker').on('change', function(e) { var selected_date = $('#datepicker').datepicker('getDate'); var today = new Date(); today.setHours(0); today.setMinutes(0); today.setSeconds(0); var today_formatted = Date.par

我目前拥有以下jQuery:-

$('#datepicker').on('change', function(e) {

  var selected_date = $('#datepicker').datepicker('getDate');
  var today = new Date();

  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);

  var today_formatted = Date.parse(today);
  var selected_date_formatted = Date.parse(selected_date);

  var selected_time = $('#time').val();

  if (selected_date_formatted == today_formatted) {
    console.log('yes');
  } else {
    console.log('no');
  }
});
它基本上检查当前日期是否与所选日期相同,并且这部分工作正常


我需要做的是检查当前日期和时间是否在所选日期和时间的24小时内,但不知道如何实现这一点,因此非常感谢您的帮助!)

基本上只需将日期对象转换为时间戳,并使用
毫秒检查它们是否在24小时内

$('#datepicker').on('change', function (e) {
    var selected_date = $('#datepicker').datepicker('getDate');
    var today = new Date();

      //Remove this to get current time, leave this to get time start of day
      today.setHours(0);
      today.setMinutes(0);
      today.setSeconds(0);

    var currentDateTimestamp = today.getTime();
    var selectedDateTimestamp = selected_date.getTime();

    //Check if the timestamp is within 24 hours, 24 hours = 60 seconds * 60 minutes * 24 hours * 1000 milliseconds
    if (Math.abs(currentDateTimestamp - selectedDateTimestamp) <= 60 * 60 * 24 * 1000) {
      //Within 24 hours
    }
});
$(“#日期选择器”)。在('change',函数(e)上{
var selected_date=$('#datepicker')。datepicker('getDate');
var today=新日期();
//删除此项以获取当前时间,保留此项以获取一天的开始时间
今天。设定时间(0);
今天。设定分钟(0);
今天。设置秒(0);
var currentDateTimestamp=today.getTime();
var selectedDateTimestamp=selected_date.getTime();
//检查时间戳是否在24小时内,24小时=60秒*60分钟*24小时*1000毫秒

如果(Math.abs(currentDateTimestamp-selectedDateTimestamp)我认为这也适用于您-

var daydiff = (currentDate.getMilliseconds() - selectedDate.getMilliseconds()) / 86400000;

if (daydiff < 1) {
  //within 24h
} else {
  //not within 24h
}
var daydiff=(currentDate.getmillizes()-selectedDate.getmillizes())/86400000;
if(daydiff<1){
//24小时内
}否则{
//不在24小时内
}

谢谢,现在只是一个小问题,我选择了2016年3月2日23:00的tommorow,它说它仍然在24小时内?@Bobby WI刚刚再试了一次@Bobby W,这里是明天23:30的值,它仍然在24小时内;current=
1456863049577
selecte=
1456876800000
请注意,您可以设置时间到00:00:00.000使用
今天。setHours(0,0,0,0)
,不需要单独调用每个set方法。