Categories
JavaScript Answers

How to convert HH:MM:SS times to seconds with moment.js?

Sometimes, we want to convert HH:MM:SS times to seconds with moment.js.

In this article, we’ll look at how to convert HH:MM:SS times to seconds with moment.js.

How to convert HH:MM:SS times to seconds with moment.js?

To convert HH:MM:SS times to seconds with moment.js, we can use the diff and startOf methods.

For instance, we write:

const s = moment('12:10:12: PM', 'HH:mm:ss: A').diff(moment().startOf('day'), 'seconds');
console.log(s)

We create a moment object with moment by parsing the time string.

Then we call diff to find the difference between the start of the day and the given time and return the result in seconds.

Therefore s is 43812.

Conclusion

To convert HH:MM:SS times to seconds with moment.js, we can use the diff and startOf methods.

Categories
JavaScript Answers

How to capture all the anchor element click events with JavaScript?

Sometimes, we want to capture all the anchor element click events with JavaScript.

In this article, we’ll look at how to capture all the anchor element click events with JavaScript.

How to capture all the anchor element click events with JavaScript?

To capture all the anchor element click events with JavaScript, we can use event delegation.

For instance, we write:

<a href='https://example.com/foo'>foo</a>
<a href='https://example.com/bar'>bar</a>

to add 2 anchor elements.

Then we write:

document.addEventListener(`click`, e => {
  if (e.target.tagName.toLowerCase() === 'a') {
    e.preventDefault()
    console.log(`You clicked ${e.target.href}`);
  }
});

to call document.addEventListener to add an event listener to the whole page.

Then we check the tag name of the element is clicked on with e.target.tagName.

If it’s 'a', then we log the href property of it, which has the value of the href attribute of the anchor element.

Conclusion

To capture all the anchor element click events with JavaScript, we can use event delegation.

Categories
JavaScript Answers

How to get month in mm format in JavaScript?

Sometimes, we want to get month in mm format in JavaScript.

In this article, we’ll look at how to get month in mm format in JavaScript.

How to get month in mm format in JavaScript?

To get month in mm format in JavaScript, we can use the date’s getMonth and string’s padStart methods.

For instance, we write:

const mm = (new Date(2022, 1, 1).getMonth() + 1).toString().padStart(2, '0')
console.log(mm)

to create a new date set to Feb 1, 2022.

We get the month number from the date with getMonth

And we add 1 to it to convert it to a human readable month number.

Then we call toString to convert it to a string.

And then we all padStart with 2 and '0' to prepend 0’s to the string until it has length 2.

Therefore, mm is '02'.

Conclusion

To get month in mm format in JavaScript, we can use the date’s getMonth and string’s padStart methods.

Categories
JavaScript Answers

How to check if a div element is empty with JavaScript?

Sometimes, we want to check if a div element is empty with JavaScript.

In this article, we’ll look at how to check if a div element is empty with JavaScript.

How to check if a div element is empty with JavaScript?

To check if a div element is empty with JavaScript, we can check if the innerHTML property of the div is empty.

For instance, we write:

<div></div>

to add an empty div.

Then we write:

const isEmpty = document.querySelector('div').innerHTML === "";
console.log(isEmpty)

to select the div with querySelector.

Then we check if its innerHTML property is an empty string.

If it is, then the div is empty.

Since, isEmpty logs true, the div is empty.

Conclusion

To check if a div element is empty with JavaScript, we can check if the innerHTML property of the div is empty.

Categories
JavaScript Answers

How to generate an image of a div and save the image to disk with JavaScript?

Sometimes, we want to generate an image of a div and save the image to disk with JavaScript.

In this article, we’ll look at how to generate an image of a div and save the image to disk with JavaScript.

How to generate an image of a div and save the image to disk with JavaScript?

To generate an image of a div and save the image to disk with JavaScript, we can use the html2canvas library.

For instance, we write:

<script src='https://html2canvas.hertzen.com/dist/html2canvas.min.js'></script>

<div id="capture" style="padding: 10px; background: #f5da55">
  <h4 style="color: #000; ">Hello world!</h4>
</div>

to add the html2canvas script and a div that we want to capture and download as an image.

Then we write:

(async () => {
  const canvas = await html2canvas(document.querySelector("#capture"))
  const link = document.createElement("a");
  link.download = 'capture.png';
  link.href = canvas.toDataURL("image/png");
  link.click();
  link.remove()
})()

to put the div content into a canvas and return it as a promise with html2canvas.

Then we create a link with createElement.

We set the file name of the downloaded file by setting the download propert.

Next, we set the URL of the file to download to the image with canvas.toDataURL.

And then we call click to start the download.

Finally, we call remove to remove the link.

Conclusion

To generate an image of a div and save the image to disk with JavaScript, we can use the html2canvas library.