Categories
JavaScript Answers

How to find the day difference between two dates (excluding weekend days) with JavaScript?

Sometimes, we want to find the day difference between two dates (excluding weekend days) with JavaScript.

In this article, we’ll look at how to find the day difference between two dates (excluding weekend days) with JavaScript.

How to find the day difference between two dates (excluding weekend days) with JavaScript?

To find the day difference between two dates (excluding weekend days) with JavaScript, we can use a while loop.

For instance, we write:

const getBusinessDatesCount = (startDate, endDate) => {
  let count = 0;
  let curDate = +startDate;
  while (curDate <= +endDate) {
    const dayOfWeek = new Date(curDate).getDay();
    const isWeekend = (dayOfWeek === 6) || (dayOfWeek === 0);
    if (!isWeekend) {
      count++;
    }
    curDate = curDate + 24 * 60 * 60 * 1000
  }
  return count;
}

const d1 = new Date(2022, 1, 1)
const d2 = new Date(2022, 1, 26)
const diff = getBusinessDatesCount(d1, d2)
console.log(diff)

to define the getBusinessDatesCount function which takes the startDate and endDate date objects.

In it, we use a while loop that loops as long as curDate is less than or equal to endDate.

We create dayOfWeek by converting the curDate timestamp to a Date instance and call its getDay method.

Then we check if dayOfWeek is 0 or 6, which is Sunday or Saturday.

And if isWeekend is false, we increment count by 1.

And then we increment curDate by 1 day by adding 24 * 60 * 60 * 1000 milliseconds.

Finally, we return the count and call getBusinessDatesCount with d1 and d2.

Therefore, diff is 19.

Conclusion

To find the day difference between two dates (excluding weekend days) with JavaScript, we can use a while loop.

Categories
JavaScript Answers

How to select td of the table with JavaScript?

Sometimes, we want to select td of the table with JavaScript.

In this article, we’ll look at how to select td of the table with JavaScript.

How to select td of the table with JavaScript?

To select td of the table with JavaScript, we can use some DOM methods.

For instance, we write:

<table>
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A1</td>
      <td>A2</td>
      <td>A3</td>
    </tr>
    <tr>
      <td>B1</td>
      <td>B2</td>
      <td>B3</td>
    </tr>
    <tr>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
    </tr>
  </tbody>
</table>

to add a table.

Then we write:

const table = document.querySelector('table')
const tds = table.querySelectorAll('td')
console.log(tds)

to select the table with querySelector.

Then we call table.querySelectorAll with 'td' to select all the td’s.

As a result, tds is a node list with the td elements.

Conclusion

To select td of the table with JavaScript, we can use some DOM methods.

Categories
JavaScript Answers

How to delete the last character of a string with JavaScript?

Sometimes, we want to delete the last character of a string with JavaScript.

In this article, we’ll look at how to delete the last character of a string with JavaScript.

How to delete the last character of a string with JavaScript?

To delete the last character of a string with JavaScript, we can use the string’s replace method.

For instance, we write:

const s = 'foobar'
const newS = s.replace(/(\s+)?.$/, '')
console.log(newS)

to call s.replace with a regex that matches the last character from the string s and replace that with an empty string.

\s matches whitespaces, and . matches any character.

The $ means the end of the string.

As a result, newS is 'fooba'.

Conclusion

To delete the last character of a string with JavaScript, we can use the string’s replace method.

Categories
JavaScript Answers

How to parse string to an int in JavaScript?

Sometimes, we want to parse string to an int in JavaScript.

In this article, we’ll look at how to parse string to an int in JavaScript.

How to parse string to an int in JavaScript?

To parse string to an int in JavaScript, we can use a regex to extract the digits from the string.

Then we can use parseInt to convert the string to a number.

For instance, we write:

const s = 'abc123';
const str = s.match(/\d+$/);
const number = parseInt(str, 10);
console.log(number)

to call s.match with a regex to return the digits part of s.

Then we call parseInt with str and 10 to return a number converted from str.

We pass in 10 to return a decimal number.

Therefore, number is 123.

Conclusion

To parse string to an int in JavaScript, we can use a regex to extract the digits from the string.

Then we can use parseInt to convert the string to a number.

Categories
JavaScript Answers

How to check that a date is greater than 30 days from today’s date with JavaScript?

Sometimes, we want to check that a date is greater than 30 days from today’s date with JavaScript.

In this article, we’ll look at how to check that a date is greater than 30 days from today’s date with JavaScript.

How to check that a date is greater than 30 days from today’s date with JavaScript?

To check that a date is greater than 30 days from today’s date with JavaScript, we can compare the timestamps of both dates.

For instance, we write:

const today = new Date()
const d = new Date(2022, 10, 10, 10)
const greaterThan30Days = +d > +today + 30 * 24 * 60 * 60 * 1000
console.log(greaterThan30Days)

to create 2 dates, today and d.

And then we check if d is more than 30 days after today by converting them both to timestamps in milliseconds with +

Then we add 30 days in milliseconds with 30 * 24 * 60 * 60 * 1000 to today.

And then we use > to do the comparison.

Therefore, greaterThan30Days is true.

Conclusion

To check that a date is greater than 30 days from today’s date with JavaScript, we can compare the timestamps of both dates.