Categories
JavaScript Answers

How to compare two string dates in JavaScript?

Sometimes, we want to compare two string dates in JavaScript.

In this article, we’ll look at how to compare two string dates in JavaScript.

How to compare two string dates in JavaScript?

To compare two string dates in JavaScript, we can compare them after we convert the date strings to timestamps.

For instance, we write

const isLater = (str1, str2) => {
  return new Date(str1) > new Date(str2);
};

to define the isLater function.

In it, we convert str1 and str2 to Date objects.

Then we compare them directly with >.

The Date objects are converted to timestamps automatically before they’re compared.

Conclusion

To compare two string dates in JavaScript, we can compare them after we convert the date strings to timestamps.

Categories
JavaScript Answers

How to add 1 month from now to current date in moment.js and JavaScript?

Sometimes, we want to add 1 month from now to current date in moment.js and JavaScript.

In this article, we’ll look at how to add 1 month from now to current date in moment.js and JavaScript.

How to add 1 month from now to current date in moment.js and JavaScript?

To add 1 month from now to current date in moment.js and JavaScript, we can use the add method.

For instance, we write

const startDate = "20-03-2022";
const newDate = moment(startDate, "DD-MM-YYYY").add(5, "days");

to call moment to parse startDate into a moment object.

Then we call add with 5 and 'days' to return a new moment object with date 5 days after startDate.

Conclusion

To add 1 month from now to current date in moment.js and JavaScript, we can use the add method.

Categories
JavaScript Answers

How to use JavaScript array map to filter results with if conditional?

Sometimes, we want to use JavaScript array map to filter results with if conditional.

In this article, we’ll look at how to use JavaScript array map to filter results with if conditional.

How to use JavaScript array map to filter results with if conditional?

To use JavaScript array map to filter results with if conditional, we can use the array filter before map.

For instance, we write

const appIds = applicationsHere
  .filter((obj) => {
    return obj.selected;
  })
  .map((obj) => {
    return obj.id;
  });

to call filter to return an array with all the items with selected set to true.

Then we call map with a callback to return the id property of each object and put them in a new array and return the array.

Conclusion

To use JavaScript array map to filter results with if conditional, we can use the array filter before map.

Categories
JavaScript Answers

How to count the number of elements with same class with JavaScript?

Sometimes, we want to count the number of elements with same class with JavaScript.

In this article, we’ll look at how to count the number of elements with same class with JavaScript.

How to count the number of elements with same class with JavaScript?

To count the number of elements with same class with JavaScript, we can use the querySelectorAll method.

For instance, we write

const count = document.querySelectorAll("#main-div .specific-class").length;

to call querySelectorAll with the selector for the elements with the specific-class class to get all the elements with that class in a node list.

Then we use the length property to get the count of the number of elements selected.

Conclusion

To count the number of elements with same class with JavaScript, we can use the querySelectorAll method.

Categories
JavaScript Answers

How to fetch an array of URLs with Promise.all with JavaScript?

Sometimes, we want to fetch an array of URLs with Promise.all with JavaScript.

In this article, we’ll look at how to fetch an array of URLs with Promise.all with JavaScript.

How to fetch an array of URLs with Promise.all with JavaScript?

To fetch an array of URLs with Promise.all with JavaScript, we call Promise.all with an array of promises.

For instance, we write

const texts = await Promise.all(
  urls.map(async (url) => {
    const resp = await fetch(url);
    return resp.text();
  })
);

to call urls.map with an async function that calls fetch and returns the response as the resolve value of the promise returned.

Then we call Promise.all with the array of promises returned by map to make the requests.

Next we get the responses from the texts array which we get from using await on the promise returned by Promise.all.

Conclusion

To fetch an array of URLs with Promise.all with JavaScript, we call Promise.all with an array of promises.