【问题标题】:How to compare time in javascript?如何在javascript中比较时间?
【发布时间】:2013-10-01 01:07:33
【问题描述】:

我有两个格式为“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');
}

当它们是字符串时,我如何将这些值与时间进行比较???

【问题讨论】:

    标签: javascript string date time casting


    【解决方案1】:

    使用日期对象。 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");
    }
    

    【讨论】:

    • 我知道问题是问如何比较字符串,但我认为这是解决整体问题的更好方法。
    【解决方案2】:

    假设你对时区不感兴趣,只需要比较两个24小时格式的格式化时间字符串,你可以只比较字符串。

    "09:12" > "12:22" //false
    "09:12" < "12:22" //true
    "08:12" > "09:22" //false
    "08:12" < "09:22" //true
    

    字符串必须标准化为 hh:mm,否则你会得到意想不到的结果:

    "9:12" > "12:22" //true
    

    【讨论】:

      【解决方案3】:
      Date.parse('25/09/2013 13:31') > Date.parse('25/09/2013 9:15')
      

      编辑:

      请注意,您正在解析一个您不感兴趣的任意日期,它只需要双方相同。

      【讨论】:

      • Date.parse 依赖于实现,在这种情况下,将在某些浏览器(例如 Safari)中返回 NaN。 OP 可以直接将字符串作为字符串进行比较。
      【解决方案4】:

      如果我有足够的代表投票支持@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

      【讨论】:

        【解决方案5】:
         if(Date.parse('01/01/2011 10:20:45') == Date.parse('01/01/2011 5:10:10')) {
          alert('same');
          }else{
        
          alert('different');
        
          }
        

        1 月 1 日是任意日期,没有任何意义。

        【讨论】:

          【解决方案6】:

          类似于@Arjun Sol,您可以直接从字符串本身获取时间,创建一个新的 Date 对象并进行比较,而不是使用 Date.parse。

          const time1 = '12:42';
          const time2 = '18:30';
          
          const getTime = time => new Date(2019, 9, 2, time.substring(0, 2), time.substring(3, 5), 0, 0);
          
          const result = getTime(time1) < getTime(time2);
          console.log('This should be true:', result);
          

          【讨论】:

            【解决方案7】:

            我们可以做一些破解。

            var time1 = "09:30";
            var time2 = "10:30";
            var time1Date= new Date("01/01/2000 "+time1);
            var time2Date= new Date("01/01/2000 "+time2);
            
            if(time1Date >= time2Date ){
                console.log("time1");
            }else{
                console.log("time2");
            }
            

            【讨论】:

              猜你喜欢
              • 2017-02-08
              • 1970-01-01
              • 1970-01-01
              • 2014-03-20
              • 1970-01-01
              • 1970-01-01
              • 2018-03-10
              • 2016-06-12
              相关资源
              最近更新 更多