javascript - Using moment.js, JS, or Jquery, turn on a feature within a time frame -


i'm trying write code reliably show div between times.

i'm using moment.js , claim in docs can can't find proper way query times need.

my code give idea of want doesn't work because when try turn on or off feature, if time goes before midnight, end negative numbers instead of time. (i using utc times , subtracting 10 hours. can use negative numbers in arrays clarity, i'd stick actual times as possible.)

also, way code now, can turn off feature @ time turning off after time better, having on between times best.

would guys know how query moment( ).isafter or .isbetween time rather dates?

so far i've tried many permutations of:

moment().isbetween('7:00:00', '8:00:00', 'hour', 'minute'); , .isafter. 

the docs don't seem have examples , no 1 on web has posed question in past. thanks!

var opensun = [6,7,17,22]; // on hours  // sundays ---------------------------------------------- // // setup sunday - on if (moment().days() === 7) {     (var = 0; < opensun.length; i++) {         if (opensun[i] === moment().utc().hours()-10) {             $('#cast').hide();             $('#cast-on').show();         }      } }  // switch off var offhourssun = [8,17,22]; var offminutessun = [00,30,30];  (var j = 0; j < offhourssun.length; j++) {      if ((moment.utc().hours()-10 === offhourssun[j]) && (moment.utc().minutes() === offminutessun[j])) {       $('#cast').show();       $('#cast-on').hide();     } } 

for task use vanilla javascript solve it.

first recommend use better , more generalized data structure store timeslots. prefer this:

var opensun = [     {         start: {             hour: 7,             minute: 0,         },         end: {             hour: 8,             minute: 0,         },     },     {         start: {             hour: 17,             minute: 0,         },         end: {             hour: 17,             minute: 30,         },     },     {         start: {             hour: 22,             minute: 0,         },         end: {             hour: 22,             minute: 30,         },     } ]; 

then run through defined time slots , check if between boundaries of one:

$(function(){     var start, end, now;     var d = new date();      = d.gethours()*60+d.getminutes();     $('#cast').show();     $('#cast-on').hide();        if (d.getday() === 0) {         (var slot = 0, len = opensun.length; slot < len; slot++) {             start = opensun[slot].start.hour*60+opensun[slot].start.minute;             end = opensun[slot].end.hour*60+opensun[slot].end.minute;             if (now>start && now<end){                 $('#cast').hide();                 $('#cast-on').show();                 break;             }         }     }  }); 

feel free check code @ http://jsfiddle.net/yly8ocd5/

i used data. make example work change fit time slot.

edit: how works moment.js:

$(function(){     var start, end;      $('#cast').show();     $('#cast-on').hide();          if (moment().weekday() === 0) {         (var slot = 0, len = opensun.length; slot < len; slot++) {             start = moment({                 hour: opensun[slot].start.hour,                  minute: opensun[slot].start.minute             });              end = moment({                 hour: opensun[slot].end.hour,                  minute: opensun[slot].end.minute             });              if (moment().isbetween(start, end)){                 $('#cast').hide();                 $('#cast-on').show();                 break;             }         }     }  }); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -