Categories
JavaScript Answers

How to validate if date is before date of current date with JavaScript?

Sometimes, we want to validate if date is before date of current date with JavaScript.

In this article, we’ll look at how to validate if date is before date of current date with JavaScript.

How to validate if date is before date of current date with JavaScript?

To validate if date is before date of current date with JavaScript, we compare their timestamps.

For instance, we write

const isAfterToday = (date) => {
  return new Date(date).valueOf() > new Date().valueOf();
};

to define the isAfterToday function.

In it, we compare the date date with today’s date by converting to timestamps with the valueOf method.

Then we compare if date is bigger than today’s date with >.

Conclusion

To validate if date is before date of current date with JavaScript, we compare their timestamps.

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.