Abdelmoula Nami
Abdelmoula Nami

Reputation: 3

How can I use moment.js to exclue Mondays?

I have this code to show guests time based on their Timezone and it show also my time, the if time is between 11:00 and 17:00 so we are on line else we are not.

How can I exclude Monday , so when it's Monday it shows an offline message

function updateTime() {
             var format = 'HH:mm:ssA'
             var divGuest = $('#spt_local_time');
    var divLocal = $('#spt_our_time');  
    var tmz =moment.tz("Africa/Casablanca");
    //put UTC time into divUTC  
   divGuest.text(moment().format('HH:mm:ssA')); 
    //get text from divUTC and conver to local timezone  
    var time  = moment.tz("Africa/Casablanca").format('HH:mm:ssA'); 
    time = moment(time),format;
    divLocal.text(moment.tz("Africa/Casablanca").format('HH:mm:ssA'));

     shiftStart = moment.tz('11:00:00', format, "Africa/Casablanca");
     shiftEnd = moment.tz('17:00:00', format, "Africa/Casablanca");
    var a = moment().day('Monday');
const test = moment();


if (test.isBetween(shiftStart, shiftEnd)) {
   
   $('.sj-support-time .spt-wrap').hasClass('spt-status-on')
                       $('.sj-support-time .spt-wrap').removeClass('spt-status-off').addClass('spt-status-on');

 


} else {

 $('.sj-support-time .spt-wrap').hasClass('spt-status-off')
                       $('.sj-support-time .spt-wrap').removeClass('spt-status-on').addClass('spt-status-off');


}

}


setInterval(updateTime, 1000);
    updateTime();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-with-locales.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.js"></script>

I hope this post will be accepted

Upvotes: 0

Views: 62

Answers (2)

AlTheLazyMonkey
AlTheLazyMonkey

Reputation: 1242

You can use the methods .day() of moment for get the number of day of week from 0 to 6(Sunday-to-Saturday)

const MONDAY = 1;

if (moment().day() === MONDAY) {
    alert('offline'); // Show offline message
} else {
    // do something of different
}

Upvotes: 1

Kunal Burangi
Kunal Burangi

Reputation: 882

moment().format('dddd');

use this to extract the Day and just wrap the whole code into an if condition.

if(moment().format('dddd')==="Monday")
 {
    // your code
 } else{ 
    // offline message
}

Upvotes: 0

Related Questions