Categories
JavaScript Answers

How to find the type of an input element with JavaScript?

Sometimes, we want to find the type of an input element with JavaScript.

In this article, we’ll look at how to find the type of an input element with JavaScript.

How to find the type of an input element with JavaScript?

To find the type of an input element with JavaScript, we use the type property.

For instance, we write

<input type="text" />

to add an input.

Then we get the type attribute of the input with

const type = document.querySelector("input").type;

We select the input with querySelector.

And we get the type attribute value with the type property.

Conclusion

To find the type of an input element with JavaScript, we use the type property.

Categories
JavaScript Answers

How to get date difference in number of days with Moment.js and JavaScript?

Sometimes, we want to get date difference in number of days with Moment.js and JavaScript.

In this article, we’ll look at how to get date difference in number of days with Moment.js and JavaScript.

How to get date difference in number of days with Moment.js and JavaScript?

To get date difference in number of days with Moment.js and JavaScript, we use the diff method.

For instance, we write

const a = moment([2022, 0, 29]);
const b = moment([2022, 0, 28]);
const diff = a.diff(b, "days");

to call a.diff with b and 'days' to get the difference between a and b in days.

Conclusion

To get date difference in number of days with Moment.js and JavaScript, we use the diff method.

Categories
JavaScript Answers

How to check if date is in the past in JavaScript?

Sometimes, we want to check if date is in the past in JavaScript.

In this article, we’ll look at how to check if date is in the past in JavaScript.

How to check if date is in the past in JavaScript?

To check if date is in the past in JavaScript, we get the difference of the timestamp between the current date time and the given date.

For instance, we write

const diff = new Date().getTime() - givenDate.getTime();
if (diff > 0) {
  console.log("givenDate is in the past");
}

to call getTime to get the timestamp of each datetime in milliseconds.

Then we get their difference and assign it to diff1.

If diff is bigger than 0, then givenDate is in the past.

Conclusion

To check if date is in the past in JavaScript, we get the difference of the timestamp between the current date time and the given date.

Categories
JavaScript Answers

How to catch and handle error response 422 with Axios and JavaScript?

Sometimes, we want to catch and handle error response 422 with Axios and JavaScript.

In this article, we’ll look at how to catch and handle error response 422 with Axios and JavaScript.

How to catch and handle error response 422 with Axios and JavaScript?

To catch and handle error response 422 with Axios and JavaScript, we call catch with a callback to catch errors.

For instance, we write

axios
  .put(url, payload)
  .then((response) => {
    // ...
  })
  .catch((error) => {
    if (error.response) {
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      console.log(error.request);
    } else {
      console.log("Error", error.message);
    }
    console.log(error.config);
  });

to call catch with a callback that has the error parameter.

We get the error response code from the error.response.status property.

Conclusion

To catch and handle error response 422 with Axios and JavaScript, we call catch with a callback to catch errors.

Categories
JavaScript Answers

How to convert a full date to a short date in JavaScript?

Sometimes, we want to convert a full date to a short date in JavaScript.

In this article, we’ll look at how to convert a full date to a short date in JavaScript.

How to convert a full date to a short date in JavaScript?

To convert a full date to a short date in JavaScript, we use the toLocaleDateString method.

For instance, we write

const date = new Date().toLocaleDateString("en-US");

to call toLocaleDateString with the locale string to return a string with the short form of the current date.

Conclusion

To convert a full date to a short date in JavaScript, we use the toLocaleDateString method.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.