mongoose更新数据 如何比较JavaScript的时间?




mongoose更新数据 (4)

我有两个时间的格式“HH:MM”我想比较他们我有下面的代码来获取我的格式现在的时间:

current_time = new Date();
hour = current_time.getHours();
minute = current_time.getMinutes();
if(hour<10){hour='0'+hour} if(minute<10){minute='0'+minute}
my_time = hour+':'+minute;

而这个代码是在减去GMT差异后得到的时间:

d = new Date()
var n = d.getTimezoneOffset();
var n1 = Math.abs(n);
var difference = (n1/60); 
my_time = my_time - (0+difference);

现在my_time的值应该和match_time的值进行比较:

match_time = 10:00;//for example
if(my_time > match_time)
{
  alert('yes');
}
else
{
  alert('No');
}

如何比较这些值作为时间,当他们是一个字符串?


 if(Date.parse('01/01/2011 10:20:45') == Date.parse('01/01/2011 5:10:10')) {
  alert('same');
  }else{

  alert('different');

  }

The 1st January is an arbitrary date, doesn't mean anything.

使用Date对象。 Date.setHours()允许你指定小时,分钟,秒

var currentD = new Date();
var startHappyHourD = new Date();
startHappyHourD.setHours(17,30,0); // 5.30 pm
var endHappyHourD = new Date();
endHappyHourD.setHours(18,30,0); // 6.30 pm

console.log("happy hour?")
if(currentD >= startHappyHourD && currentD < endHappyHourD ){
    console.log("yes!");
}else{
    console.log("no, sorry! between 5.30pm and 6.30pm");
}

如果我有足够的代表投票@Gabo Esquivel解决方案。 几天来,我一直在寻找和测试的解决方案,这是唯一一个为我工作的。

如果当前时间是0830,我需要一个条件语句测试,如果是的话,做一些事情。 我的if语句不起作用,所以我需要其他例子来处理。

//Business Hours: Saturday 8:30am-12pm; highlight Saturday table row.
function showSaturdayHours() {
    var today = new Date();
    var weekday = today.getDay();
    var saturdayOpen = new Date();
    saturdayOpen.setHours(8, 30, 0);
    var saturdayClose = new Date();
    saturdayClose.setHours(12, 0, 0);

if (weekday == 6) {
    $('#saturday-row').addClass('row-blue'); //highlight table row if current day is Saturday.
    if (today >= saturdayOpen && today < saturdayClose) {
        document.getElementById('saturday-open').innerHTML = 'Open';
    } else {
        document.getElementById('saturday-open').innerHTML = 'Closed';
    }
  }
}

营业时间表: JSFiddle


Date.parse('25/09/2013 13:31') > Date.parse('25/09/2013 9:15')

编辑:

请注意,你正在解析一个你不感兴趣的任意日期,只需要两边都是一样的。





casting