Categories
JavaScript Answers

How to capitalize first letter of each word in JavaScript?

Sometimes, we want to capitalize first letter of each word in JavaScript.

In this article, we’ll look at how to capitalize first letter of each word in JavaScript.

How to capitalize first letter of each word in JavaScript?

To capitalize first letter of each word in JavaScript, we can use the string’s replace method.

For instance, we write:

const str = 'foo bar baz'
const newStr = str.toLowerCase().replace(/^\w|\s\w/g, (letter) => {
  return letter.toUpperCase();
})
console.log(newStr)

We call replace with a regex to match the first letter of each word and a callback that replace those letters with an upper case letter.

Therefore, newStr is 'Foo Bar Baz'.

Conclusion

To capitalize first letter of each word in JavaScript, we can use the string’s replace method.

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 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, 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, we can use the duration, diff, asHours and asMinutes methods.

Categories
JavaScript Answers

How to Calculate Date from Week Number in JavaScript?

Sometimes, we want to calculate date from week number in JavaScript.

In this article, we’ll look at how to calculate date from week number in JavaScript.

Calculate Date from Week Number in JavaScript

To calculate date from week number in JavaScript, we can use the JavaScript date setDate and getDate methods.

For instance, we can write:

const getSundayFromWeekNum = (weekNum, year) => {
  const sunday = new Date(year, 0, (1 + (weekNum - 1) * 7));
  while (sunday.getDay() !== 0) {
    sunday.setDate(sunday.getDate() - 1);
  }
  return sunday;
}

console.log(getSundayFromWeekNum(10, 2021))

We create the getSundayFromWeekNum function that takes the weekNum and year parameters that we use to get the Sunday of the given year and week number.

In the function, we create a JavaScript date with the Date constructor with year, 0, (1 + (weekNum - 1) * 7) to get the date given weekNum and year.

Then we add a while loop to subtract 1 day from the sundday date until we reach a Sunday.

Finally, we return that result.

Then when we console log, we get Sun Feb 28 2021 00:00:00 GMT-0800 (Pacific Standard Time).

Cconclusion

To calculate date from week number in JavaScript, we can use the JavaScript date setDate and getDate methods.

Categories
JavaScript Answers

How to Get Computed Styles of an Element with JavaScript?

Sometimes, we want to get computed styles of an element with JavaScript.

In this article, we’ll look at how to get computed styles of an element with JavaScript.

Get Computed Styles of an Element with JavaScript

To get computed styles of an element with JavaScript, we can use the window.getComputedStyle method.

For instance, if we have:

<div>
  hello
</div>

Then we write:

const element = document.querySelector('div')
const style = window.getComputedStyle(element)
console.log(style)

We get the div with document.querySelector.

Then we call window.getComputedStyle with element as the argument.

Therefore, the console log should show all the computed CSS properties with their values for the div.

Conclusion

To get computed styles of an element with JavaScript, we can use the window.getComputedStyle method.