Categories
JavaScript Answers

How to add 3 days in milliseconds to current Date with JavaScript?

Sometimes, we want to add 3 days in milliseconds to current Date with JavaScript.

In this article, we’ll look at how to add 3 days in milliseconds to current Date with JavaScript.

How to add 3 days in milliseconds to current Date with JavaScript?

To add 3 days in milliseconds to current Date with JavaScript, we pass in the timestamp for 3 days after today into the Date constructor.

For instance, we write

const dateObj = new Date(Date.now() + 86400000 * 3);

to call Date.now to get the timestamp of the current date time.

Then we add 3 days, which is 86400000 * 3 ms to it.

And we use the sum as the argument of the Date constructor to return a Date object with the date 3 days from now.

Conclusion

To add 3 days in milliseconds to current Date with JavaScript, we pass in the timestamp for 3 days after today into the Date constructor.

Categories
JavaScript Answers

How to fix Bootstrap 4 dropdown menu not working with JavaScript?

Sometimes, we want to fix Bootstrap 4 dropdown menu not working with JavaScript.

In this article, we’ll look at how to fix Bootstrap 4 dropdown menu not working with JavaScript.

How to fix Bootstrap 4 dropdown menu not working with JavaScript?

To fix Bootstrap 4 dropdown menu not working with JavaScript, we should make sure all the required libraries are added.

For instance, we write

<script
  src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
  integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
  crossorigin="anonymous"
></script>
<script
  src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
  integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
  crossorigin="anonymous"
></script>
<script
  src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
  integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
  crossorigin="anonymous"
></script>

to add jQuery and popper.js before adding the Bootstrap JavaScript library.

Conclusion

To fix Bootstrap 4 dropdown menu not working with JavaScript, we should make sure all the required libraries are added.

Categories
JavaScript Answers

How to write regular expression for not allowing spaces in the input field with JavaScript?

Sometimes, we want to write regular expression for not allowing spaces in the input field with JavaScript.

In this article, we’ll look at how to write regular expression for not allowing spaces in the input field with JavaScript.

How to write regular expression for not allowing spaces in the input field with JavaScript?

To write regular expression for not allowing spaces in the input field with JavaScript, we use the \S pattern.

For instance, we write

const regExp = /^\S+$/;

to use the \S pattern to match non-space characters.

+ lets us match a group of non-space characters.

^ is the marker for the start of a word.

And $ is the marker for the end of a word.

Conclusion

To write regular expression for not allowing spaces in the input field with JavaScript, we use the \S pattern.

Categories
JavaScript Answers

How to filter array of objects whose any properties contains a value with JavaScript?

Sometimes, we want to filter array of objects whose any properties contains a value with JavaScript.

In this article, we’ll look at how to filter array of objects whose any properties contains a value with JavaScript.

How to filter array of objects whose any properties contains a value with JavaScript?

To filter array of objects whose any properties contains a value with JavaScript, we use the array filter method.

For instance, we write

const filterByValue = (array, value) => {
  return array.filter(
    (data) =>
      JSON.stringify(data).toLowerCase().indexOf(value.toLowerCase()) !== -1
  );
};

to call array.filter with a callback that checks if the stringified version of the data object in array has the value we’re looking for indexOf.

If indexOf returns something other than -1, then the array returned by filter includes the data object.

Conclusion

To filter array of objects whose any properties contains a value with JavaScript, we use the array filter method.

Categories
JavaScript Answers

How to get month name of last month using moment and JavaScript?

Sometimes, we want to get month name of last month using moment and JavaScript.

In this article, we’ll look at how to get month name of last month using moment and JavaScript.

How to get month name of last month using moment and JavaScript?

To get month name of last month using moment and JavaScript, we use the format method.

For instance, we write

const monthMinusOneName = moment()
  .subtract(1, "month")
  .startOf("month")
  .format("MMMM");

to call subtract to subtract 1 month from today’s date.

We call startOf to return the start date of the month.

And we call format with 'MMMM' to return the month name.

Conclusion

To get month name of last month using moment and JavaScript, we use the format method.