Categories
JavaScript Answers

How to Get the Hour Difference Between Two Times with Moment.js and JavaScript?

Sometimes, we want to get the hour difference between two times with moment.js and JavaScript.

In this article, we’ll look at how to get the hour difference between two times with moment.js and JavaScript.

Get the Hour Difference Between Two Times with Moment.js and JavaScript

To get the hour difference between two times with moment.js and JavaScript, we can use the duration, diff, asHours and asMinutes methods.

For instance, we can write:

const startTime = moment("12:26:59 am", "HH:mm:ss a");
const endTime = moment("06:12:07 pm", "HH:mm:ss a");
const duration = moment.duration(endTime.diff(startTime));
const hours = parseInt(duration.asHours());
const minutes = parseInt(duration.asMinutes()) % 60;

console.log(hours, minutes);

We parse 2 times into moment objects with the moment function.

We pass in the format of the time as the 2nd argument.

Then we call moment.duration with the difference between endTime and startTime that we get with the diff method.

Next, we get the hours part of the duration with the asHours method.

And we get the minutes part of the duration with the asMinutes method and get the remainder of that divided by 60.

Therefore, we get 17 45 from the console log.

Conclusion

To get the hour difference between two times with moment.js and JavaScript, we can use the duration, diff, asHours and asMinutes methods.

Categories
JavaScript Answers

How to Get Paragraph Text Inside an Element with JavaScript?

Sometimes, we want to get paragraph text inside an element with JavaScript.

In this article, we’ll look at how to get paragraph text inside an element with JavaScript.

Get Paragraph Text Inside an Element with JavaScript

To get paragraph text inside an element with JavaScript, we can use the innerHTML property of the element.

For instance, if we have:

<p>
  hello world
</p>

Then we write:

const text = document.querySelector('p').innerHTML;
console.log(text)

to get the p element with document.querySelector.

Then we use the innerHTML property to return the text.

Therefore, from the console log, we see that text is:

  hello world

Conclusion

To get paragraph text inside an element with JavaScript, we can use the innerHTML property of the element.

Categories
JavaScript Answers

How to Get the Text Node After an Element with JavaScript?

Sometimes, we want to get the text node after an element with JavaScript.

In this article, we’ll look at how to get the text node after an element with JavaScript.

Get the Text Node After an Element with JavaScript

To get the text node after an element with JavaScript, we can use the nextSibling property to get the the text node after an element.

For instance, if we have:

<input type="checkbox" name='something' value='v1' /> hello world <br />

Then we write:

const text = document
  .querySelector('input[name="something"]')
  .nextSibling.nodeValue;
console.log(text)

We call document.querySelector with the selector string of the checkbox input to select it.

Then we get the value of the text node next to the checkbox with the nextSibling.nodeValue

Therefore, text is hello world according to the console log.

Conclusion

To get the text node after an element with JavaScript, we can use the nextSibling property to get the the text node after an element.

Categories
JavaScript Answers

How to Call the moment.calendar Method Without the Time with JavaScript?

Sometimes, we want to call moment.calendar method without the time with JavaScript.

In this article, we’ll look at how to call moment.calendar method without the time with JavaScript.

Call the moment.calendar Method Without the Time with JavaScript

To call moment.calendar method without the time with JavaScript, we can pass in null as the first argument.

For instance, we can write:

const cal = moment('2021-01-01')
  .calendar(null, {
    lastDay: '[Yesterday]',
    sameDay: '[Today]',
    nextDay: '[Tomorrow]',
    lastWeek: '[last] dddd',
    nextWeek: 'dddd',
    sameElse: 'L'
  })
console.log(cal)

We call moment with a date string.

Then we call calendar on the moment object with null and an object that has some date formatting options.

sameDay lets us specify the format for same day.

lastDay lets us specify the format for yesterday.

nextDay lets us specify the format for tomorrow.

lastWeek lets us specify the format for last week.

nextWeek lets us specify the text for next week.

sameElse specifies the format for all other dates.

Therefore, cal is '01/01/2021'.

Conclusion

To call moment.calendar method without the time with JavaScript, we can pass in null as the first argument.

Categories
JavaScript Answers

How to Draw a Circle in HTML5 Canvas Using JavaScript?

Sometimes, we want to draw a circle in HTML5 canvas using JavaScript.

In this article, we’ll look at how to draw a circle in HTML5 canvas using JavaScript.

Draw a Circle in HTML5 Canvas Using JavaScript

To draw a circle in HTML5 canvas using JavaScript, we can use the arc method.

For instance, we can add the canvas element by writing:

<canvas style='width: 300px; height: 300px'></canvas>

Then we write:

const canvas = document.querySelector('canvas');
const context = canvas.getContext('2d');
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = 70;
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.fill();
context.lineWidth = 5;
context.strokeStyle = '#003300';
context.stroke();

We select the canvas with document.querySelector.

Then we get the canvas context with getContext.

Next, we set the center’s coordinates with centerX and centerY.

We then we set the radius of the circle.

Next, we call beginPath to start drawing.

Then we call arc with centerX, centerY, radius, 0, 2 * Math.PI and `false.

We need 2 * Math.PI to draw an arc forms the circle.

Next, we set the fillStyle to set the fill of the circle.

Then we call fill to draw the fill.

Next, we set lineWidth to set the outline’s width.

Then we set strokeStyle to set the outline color.

And finally, we call stroke to draw the outline.

Now we see a circle with green fill color and black outline drawn.

Conclusion

To draw a circle in HTML5 canvas using JavaScript, we can use the arc method.