Categories
JavaScript Answers

How to set just the date, ignoring time with JavaScript Date?

Sometimes, we want to set just the date, ignoring time with JavaScript Date.

In this article, we’ll look at how to set just the date, ignoring time with JavaScript Date.

How to set just the date, ignoring time with JavaScript Date?

To set just the date, ignoring time with JavaScript Date, we pass in a date string into the Date constructor.

For instance, we write

const newDate = new Date(oldDate.toDateString());

to get the oldDate‘s date as a string with toDateString.

Then we pass that into the Date constructor to create a new date object with the date.

Conclusion

To set just the date, ignoring time with JavaScript Date, we pass in a date string into the Date constructor.

Categories
TypeScript Answers

How to fix module has no exported member error with TypeScript?

Sometimes, we want to fix module has no exported member error with TypeScript.

In this article, we’ll look at how to fix module has no exported member error with TypeScript.

How to fix module has no exported member error with TypeScript?

To fix module has no exported member error with TypeScript, we make sure we’re importing things that are exported.

For instance, we write

import { SigninComponent } from "./auth/components/signin.component";

to import SigninComponent from ./auth/components/signin.component.ts.

If it’s a default export then we write

import SigninComponent from "./auth/components/signin.component";

We should also make sure the path we’re importing is right.

Conclusion

To fix module has no exported member error with TypeScript, we make sure we’re importing things that are exported.

Categories
JavaScript Answers

How to check whether a button is clicked by using JavaScript?

Sometimes, we want to check whether a button is clicked by using JavaScript.

In this article, we’ll look at how to check whether a button is clicked by using JavaScript.

How to check whether a button is clicked by using JavaScript?

To check whether a button is clicked by using JavaScript, we add a click listener to the button.

For instance, we write

document.getElementById("button").onclick = () => {
  console.log("button was clicked");
};

to select the button with getElementById.

Then we set its onclick property to a function that’s called when the button is clicked.

Conclusion

To check whether a button is clicked by using JavaScript, we add a click listener to the button.

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.