Categories
JavaScript Answers

How to calculate number of working days between two dates in JavaScript using moment.js?

Sometimes, we want to calculate number of working days between two dates in JavaScript using moment.js.

In this article, we’ll look at how to calculate number of working days between two dates in JavaScript using moment.js.

How to calculate number of working days between two dates in JavaScript using moment.js?

To calculate number of working days between two dates in JavaScript using moment.js, we can use a while loop.

For instance, we write:

const calcBusinessDays = (startDate, endDate) => {
  const day = moment(startDate);
  let businessDays = 0;
  while (day.isSameOrBefore(endDate, 'day')) {
    if (day.day() !== 0 && day.day() !== 6) {
      businessDays++;
    }
    day.add(1, 'd');
  }
  return businessDays;
}

const startDate = '2022-01-01'
const endDate = '2022-01-31'
console.log(calcBusinessDays(startDate, endDate))

to define the calcBusinessDays function that takes the startDate and endDate strings.

In it, we set day to the startDate parsed into a moment object with moment.

Then we use a while loop that calls day.isSameOrBefore with endDate to run the loop until day is the same as endDate.

In the loop, we check if the week day isn’t 0 or 6, which are Saturday and Sunday respectively.

If the condition is true, then we add 1 to businessDays.

And then we add 1 day to day with day.add(1, 'd').

Finally, we return businessDays.

As a result, 21 is logged in the console.

Conclusion

To calculate number of working days between two dates in JavaScript using moment.js, we can use a while loop.

Categories
JavaScript Answers

How to add hours to a parsed moment date with JavaScript?

Sometimes, we want to add hours to a parsed moment date with JavaScript.

In this article, we’ll look at how to add hours to a parsed moment date with JavaScript.

How to add hours to a parsed moment date with JavaScript?

To add hours to a parsed moment date with JavaScript, we can use the add method.

For instance, we write:

const myDate = "2022-04-14 07:07:15";
const newDate = moment(myDate).add(5, 'hours').format('YYYY-MM-DD hh:mm:ss');
console.log(newDate)

to add 5 hours to the moment object parsed from myDate.

We call add with 5 and 'hours' to add 5 hours.

And then we call format with a format string to return the datetime stored in the moment object.

Therefore, newDate is "2022-04-14 12:07:15".

Conclusion

To add hours to a parsed moment date with JavaScript, we can use the add method.

Categories
JavaScript Answers

How to find the first matching element given an array and predicate with JavaScript?

Sometimes, we want to find the first matching element given an array and predicate with JavaScript.

In this article, we’ll look at how to find the first matching element given an array and predicate with JavaScript.

How to find the first matching element given an array and predicate with JavaScript?

The first matching element given an array and predicate with JavaScript, we can use the array find method.

For instance, we write:

const a = [2, 4, 10, 5, 7, 20]
const r = a.find(x => x % 2)
console.log(r)

to find the first element in a that is odd with find.

We call a.find with x => x % 2 to return the first element in a that isn’t evenly divisible by 2.

Therefore, r is 5.

Conclusion

The first matching element given an array and predicate with JavaScript, we can use the array find method.

Categories
JavaScript Answers

How to get localized day of week with JavaScript?

Sometimes, we want to get localized day of week with JavaScript.

In this article, we’ll look at how to get localized day of week with JavaScript.

How to get localized day of week with JavaScript?

To get localized day of week with JavaScript, we can use the date’s toLocaleString method.

For instance, we write:

const d = new Date();
const dStr = d.toLocaleString(window.navigator.language, {
  weekday: 'long'
});
console.log(dStr)

to call d.toLocaleString with the language of the device the code is running on and an object with weekday set to 'long' to return a localize string of the current date.

Conclusion

To get localized day of week with JavaScript, we can use the date’s toLocaleString method.

Categories
JavaScript Answers

How to randomly select array item without repeats with JavaScript?

Sometimes, we want to randomly select array item without repeats with JavaScript.

In this article, we’ll look at how to randomly select array item without repeats with JavaScript.

How to randomly select array item without repeats with JavaScript?

To randomly select array item without repeats with JavaScript, we can shuffle the array and then return the number of items we want from the shuffled array.

For instance, we write:

const arr = ["Roger", "Russell", "Clyde", "Egbert", "Clare", "Bobbie", "Simon", "Elizabeth", "Ted", "Caroline"];
const choices = arr.sort(() => Math.random() - 0.5).slice(0, 5)
console.log(choices);

to call arr.sort with a callback that return a number between -0.5 and 0.5 to return the shuffled version of arr.

And then we call slice with 0 and 5 to return the first 5 elements from the shuffled array.

Therefore, choices is something like ["Egbert", "Elizabeth", "Simon", "Clare", "Caroline"].

Conclusion

To randomly select array item without repeats with JavaScript, we can shuffle the array and then return the number of items we want from the shuffled array.