[removed] IF time >= 9:30 then

前端 未结 8 2047
一整个雨季
一整个雨季 2020-12-30 11:25

I\'m trying to write a statement that says \"if time is this and less than that then\". I can use get hours and get min. However, I\'m having problems combining a time suc

相关标签:
8条回答
  • 2020-12-30 11:36

    You have a few typos and basic javascript errors.
    Might wanna brush up on the basics.
    W3Schools is where I learned all I know. It works fine if you fix them...

    var now = new Date();
      var hour = now.getHours();
      var day = now.getDay();
      var minutes = now.getMinutes();
      if(day == 0 && hour == 9 && minutes < 30 && minutes > 10 || day == 0 && hour == 9)
          document.write('Time is between 9:10 and 9:30');
    

    Think of the if statement as basic logic.
    If the day is Sunday(0)
    AND the hour is 9
    AND the minutes are greater than 10 AND the minutes are less than 10 OR the day is Sunday(0)
    AND the hour is before 9.

    0 讨论(0)
  • 2020-12-30 11:46

    use new Date().getTime() returns milliseconds for much easier comparison. This way there is no need to check hour, min, second, millisecond. Fiddle link

    var d930 = new Date(2010, 12, 21, 9, 30, 0, 0), // today 9:30:00:000
        d931 = new Date(2010, 12, 21, 9, 31, 0, 0), // today 9:31:00:000
        t930 = d930.getTime(),
        t931 = d931.getTime();
    
    
    console.log(t931 > t930);
    

    This way your code can check against a static 9:30 time.

    var time930 = new Date(2010, 12, 21, 9, 30, 0, 0).getTime(),
        sunday = 0,
        now = new Date();
    
    if(now.getDay() == sunday && now.getTime() >= time930){
        /* do stuff */
    }
    
    0 讨论(0)
  • 2020-12-30 11:47

    One way is to do a direct comparison on date objects. Choose an arbitrary year, month and day, and then incorporate your times as follows:

    var older = new Date("1980-01-01 12:15");
    var newer = new Date("1980-01-01 12:30");
    
    if (newer > older){
        alert("Newer time is newer");
    
    } else {
        alert ("The time is not newer");
    
    }
    

    The MDC documentation on the Date object will help with some more details. The bottom line is that if you want to compare times, you don't actually need to call any methods on the objects, and it's possible to directly compare them. The date() object can take a variety of strings to assign a new time to the returned instance, these are from the MDC documentation:

    today = new Date();
    birthday = new Date("December 17, 1995 03:24:00");
    birthday = new Date(1995,11,17);
    birthday = new Date(1995,11,17,3,24,0);
    

    As you can see, it's pretty simple. Don't complicate, and have a look through the documentation :)

    While we're here, here's a test using your example:

    var base = new Date("1980-01-01 9:30");
    var test = new Date("1980-01-01 9:30:01");
    
    if (test >= base){
        alert("test time is newer or equal to base time");
    
    } else {
        alert ("test time is older than 9.30");
    
    }
    
    0 讨论(0)
  • 2020-12-30 11:48
    if the hour is less than 9, true
    or
    if hour is 9 and minutes lt 30, true
    

    so that would look like

    if ((hour < 9) || (hour == 9 && minutes < 30))
    

    Use words to figure out your logic. Symbols are just shortcuts.

    0 讨论(0)
  • 2020-12-30 11:53

    I made this solution simple and easy to read (thus easy to adjust) by using a simple trick.

    The trick is to transform the hour and minutes into a number. Eg 09:30 into 9.30

    var now     = new Date();
    var hour    = now.getHours();
    var minutes = now.getMinutes();
    
    // make the current time of day computable and readable at the same time
    // 9:30 becomes 9.30
    var timeOfDay = hour + (minutes / 100);
    
    console.log('timeOfDay: ' + timeOfDay);
    
    // For setting the time limits simply use the time with a dot instead of a colon
    if( (9.30 <= timeOfDay) && (timeOfDay <= 11.30) ){
        console.log('Time is between 9:30 and 11:30');
    }
    
    0 讨论(0)
  • 2020-12-30 11:54
      var now = new Date();
      var closeTime = new Date();
      closeTime.setHours(9); closeTime.setMinutes(30);
      console.log(now, closeTime, now.getTime() >= closeTime.getTime());
    

    close time is based on today, then we just change the hours and minutes to 9:30.

    0 讨论(0)
提交回复
热议问题