Reputation: 2659
I am using moment.js to work with dates.
Orders placed before 10 AM are going to be handled the same day, after 10 am the order will be handled the next day. But when someone orders 11 AM on a Friday I don't want the next handling day to be Saturday, but Monday next week.
How can I do that using moment.js ?
Currently this is my code (there is no check for 10 AM yet, I'll add that later on):
// Set locale to dutch
moment.locale('nl');
// Set current day and set full date to use in string later on
$dag = moment().format('dddd');
$volledatum = $dag +' '+ moment().format('D MMMM');
// Check if day is saturday or sunday
if($dag == 'zaterdag' || $dag == 'zondag'){
// It's the weekend so $volledatum should be the first day after the weekend
$('#datumcheck').append('* Mits wij drukklare en vrijgegeven bestanden vandaag <br><b>('+ $volledatum +') <u>voor 10 uur</u></b> hebben ontvangen en de betaling is voltooid.');
}else{
// It's not the weekend so use the current date
$('#datumcheck').append('* Mits wij drukklare en vrijgegeven bestanden vandaag <br><b>('+ $volledatum +') <u>voor 10 uur</u></b> hebben ontvangen en de betaling is voltooid.');
}
Upvotes: 0
Views: 985
Reputation: 12993
You can use moment().hour()
to check hours and moment.day()
that return the day of the week with Sunday as 0 and Saturday as 6:
// Set locale to dutch
moment.locale('nl');
// Set current day and set full date to use in string later on
const dag = moment('2019-11-23T11:01:00');
const volledatum = dag.format('dddd D MMMM');
// Check if day is saturday or sunday
let isWeekend = (dag.day() === 6 || dag.day() === 0) && dag.hour() >= 11;
console.log('isWeekend', isWeekend);
if (isWeekend) {
// It's the weekend so $volledatum should be the first day after the weekend
$('#datumcheck').append('* Mits wij drukklare en vrijgegeven bestanden vandaag <br><b>(' + volledatum + ') <u>voor 10 uur</u></b> hebben ontvangen en de betaling is voltooid.');
} else {
// It's not the weekend so use the current date
$('#datumcheck').append('* Mits wij drukklare en vrijgegeven bestanden vandaag <br><b>(' + volledatum + ') <u>voor 10 uur</u></b> hebben ontvangen en de betaling is voltooid.');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/nl.js"></script>
<div id="datumcheck"></div>
Upvotes: 1
Reputation: 874
Use moment.weekday()
to get the current day of the week, and moment.hours()
to get the current time hours.
weekday will return a value between 0 to 6, when 0 is Sunday and 6 is Saturday, read more here
Upvotes: 0
Reputation: 1983
You could use moment-business-days.
Example:
var momentBusinessDays = require("moment-business-days")
momentBusinessDays('20-09-2018', 'DD-MM-YYYY').businessAdd(1)
If you don't want to use another external lib, you could use something like:
function addBusinessDays(originalDate, numDaysToAdd) {
const Sunday = 0;
const Saturday = 6;
let daysRemaining = numDaysToAdd;
const newDate = originalDate.clone();
while (daysRemaining > 0) {
newDate.add(1, 'days');
if (newDate.day() !== Sunday && newDate.day() !== Saturday) {
daysRemaining--;
}
}
return newDate;
}
Upvotes: 2