Categories
JavaScript Answers

How to get the X and Y coordinates for a div element with JavaScript?

Sometimes, we want to get the X and Y coordinates for a div element with JavaScript.

In this article, we’ll look at how to get the X and Y coordinates for a div element with JavaScript.

How to get the X and Y coordinates for a div element with JavaScript?

To get the X and Y coordinates for a div element with JavaScript, we an use the getBoundingXClientRect method.

For instance, we add a div by writing:

<div>
  hello world
</div>

Then we write:

const element = document.querySelector('div');
const position = element.getBoundingClientRect();
const x = position.left;
const y = position.top;
console.log(x, y)

to select the div with querySelector.

And then we call getBoundingClientRect to get the position of the div.

Then we get the x and y coordinates of the div with position.left and position.top.

Conclusion

To get the X and Y coordinates for a div element with JavaScript, we an use the getBoundingXClientRect method.

Categories
JavaScript Answers

How to get the current date minus 20 seconds in JavaScript?

Sometimes, we want to get the current date minus 20 seconds in JavaScript.

In this article, we’ll look at how to get the current date minus 20 seconds in JavaScript.

How to get the current date minus 20 seconds in JavaScript?

To get the current date minus 20 seconds in JavaScript, we can get the timestamp of the date and then we subtract 20000 milliseconds from the timestamp.

For instance, we write:

const d = new Date(2022, 1, 1)
const newD = new Date(d.getTime() - 20000);
console.log(newD)

to create date d.

Then we call d.getTime to return the timestamp of the date in milliseconds.

Next, we subtract 20000 milliseconds from the timestamp to get the timestamp of d minus 20 seconds.

And then we use the returned timestamp to create a new date with the Date constructor.

Therefore, newD is Mon Jan 31 2022 23:59:40 GMT-0800 (Pacific Standard Time) .

Conclusion

To get the current date minus 20 seconds in JavaScript, we can get the timestamp of the date and then we subtract 20000 milliseconds from the timestamp.

Categories
JavaScript Answers

How to select elements and add class to them in JavaScript?

Sometimes, we want to select elements and add class to them in JavaScript.

In this article, we’ll look at how to select elements and add class to them in JavaScript.

How to select elements and add class to them in JavaScript?

To select elements and add class to them in JavaScript, we can use querySelectorAll and a for-of loop.

For instance, we write:

<ul>
  <li class="even"></li>
  <li class="odd"></li>
  <li class="even"></li>
</ul>

to add a list.

Then we select all the li’s and add a class to them by writing:

const list = document.querySelectorAll("li.even, li.odd");
for (const li of list) {
  li.classList.add('cf');
}

We select all the li’s with querySelectorAll.

And then we loop through them with a for-of loop.

In the loop, we call li.classList.add to add the 'cf' class to each li element.

Conclusion

To select elements and add class to them in JavaScript, we can use querySelectorAll and a for-of loop.

Categories
JavaScript Answers

How to calculate number of working days between two dates in JavaScript using moment.js?

Sometimes, we want to calculate number of working days between two dates in JavaScript using moment.js.

In this article, we’ll look at how to calculate number of working days between two dates in JavaScript using moment.js.

How to calculate number of working days between two dates in JavaScript using moment.js?

To calculate number of working days between two dates in JavaScript using moment.js, we can use a while loop.

For instance, we write:

const calcBusinessDays = (startDate, endDate) => {
  const day = moment(startDate);
  let businessDays = 0;
  while (day.isSameOrBefore(endDate, 'day')) {
    if (day.day() !== 0 && day.day() !== 6) {
      businessDays++;
    }
    day.add(1, 'd');
  }
  return businessDays;
}

const startDate = '2022-01-01'
const endDate = '2022-01-31'
console.log(calcBusinessDays(startDate, endDate))

to define the calcBusinessDays function that takes the startDate and endDate strings.

In it, we set day to the startDate parsed into a moment object with moment.

Then we use a while loop that calls day.isSameOrBefore with endDate to run the loop until day is the same as endDate.

In the loop, we check if the week day isn’t 0 or 6, which are Saturday and Sunday respectively.

If the condition is true, then we add 1 to businessDays.

And then we add 1 day to day with day.add(1, 'd').

Finally, we return businessDays.

As a result, 21 is logged in the console.

Conclusion

To calculate number of working days between two dates in JavaScript using moment.js, we can use a while loop.

Categories
JavaScript Answers

How to add hours to a parsed moment date with JavaScript?

Sometimes, we want to add hours to a parsed moment date with JavaScript.

In this article, we’ll look at how to add hours to a parsed moment date with JavaScript.

How to add hours to a parsed moment date with JavaScript?

To add hours to a parsed moment date with JavaScript, we can use the add method.

For instance, we write:

const myDate = "2022-04-14 07:07:15";
const newDate = moment(myDate).add(5, 'hours').format('YYYY-MM-DD hh:mm:ss');
console.log(newDate)

to add 5 hours to the moment object parsed from myDate.

We call add with 5 and 'hours' to add 5 hours.

And then we call format with a format string to return the datetime stored in the moment object.

Therefore, newDate is "2022-04-14 12:07:15".

Conclusion

To add hours to a parsed moment date with JavaScript, we can use the add method.