javascript - Setting Hour/Minute ranges with new Date function -


the following function worked before added m variable if conditions. however, noticed error, when time went 11am 12pm, if else did not run noontime variable. looked @ code , thought had minutes not being specified in conditions. code this: var noontime = (h >= 12 && h <= 17) ? true : false;. changed this: var noontime = (h >= 12, m > 0 && h <= 17, m == 0) ? true : false; try , check minutes in condition.

does see doing wrong trying check minutes in conditions?

var customername = 'bob';  function timenow() {              var d = new date(),          h = ('0' + d.gethours()).slice(-2),          m = ('0' + d.getminutes()).slice(-2);            // checking time range          var morningtime = (h >= 5 && h <= 12, m == 0) ? true : false;          var noontime    = (h >= 12, m > 0 && h <= 17, m == 0) ? true : false;          var nighttime   = (h >= 17 && h <= 5)  ? true : false;          var greeting = "";            if (morningtime) {              greeting = "good morning";          } else if (noontime) {              greeting = "good afternoon";          } else if (nighttime) {              greeting = "good evening";          }            return greeting;      }     // var hellotime = timenow();      document.getelementbyid('dashboard-hello').innerhtml = timenow() + ', ' + customername + '!';      //$('#dashboard-hello').html(hellotime + ', ' + customername);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div id="dashboard-hello"></div>

you'll want combine hour , minute comparisons. using parentheses these groupings make them easier read. use ranges want.

function getgreeting(d) {    h = d.gethours(), // there's no reason format these      m = d.getminutes(); // strings, since you're comparing numbers      // checking time range    var morningtime = ((h >= 5 && h < 12) || (h === 12 && m == 0));    var noontime = ((h === 12 && m > 0) || (h > 12 && h < 17) || (h === 17 && m === 0));    var nighttime = (h >= 17 || h <= 5)    var greeting = "";      if (morningtime) {      greeting = "good morning";    } else if (noontime) {      greeting = "good afternoon";    } else if (nighttime) {      greeting = "good evening";    }      return greeting;  }    console.log("now: " + getgreeting(new date()));  console.log("5 am: " + getgreeting(new date(1, 1, 1, 5, 0, 0)));  console.log("12:00pm: " + getgreeting(new date(1, 1, 1, 12, 0, 0)));  console.log("12:01pm: " + getgreeting(new date(1, 1, 1, 12, 1, 0)));  console.log("5:00pm: " + getgreeting(new date(1, 1, 1, 17, 0, 0)));  console.log("5:01pm: " + getgreeting(new date(1, 1, 1, 17, 1, 0)));  console.log("12:00am: " + getgreeting(new date(1, 1, 1, 0, 0, 0)));

i've made date argument function testing purposes. return it, remove d , put first line of code in.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -