Categories
JavaScript Answers

How to calculate the day of the year with JavaScript?

Sometimes, we want to calculate the day of the year with JavaScript.

In this article, we’ll look at how to calculate the day of the year with JavaScript.

How to calculate the day of the year with JavaScript?

To calculate the day of the year with JavaScript, we can calculate the difference between the current datetime and the midnight of the first day of the year.

For instance, we write

const now = new Date();
const start = new Date(now.getFullYear(), 0, 0);
const diff =
  now -
  start +
  (start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000;
const oneDay = 1000 * 60 * 60 * 24;
const day = Math.floor(diff / oneDay);

to calculate diff by subtracting now by start.

And then we add back the time zone offset difference to add back the difference caused by daylight saving time.

Then we divide diff by oneDay, which is 1 day in milliseconds.

Finally, we call Math.floor to round the quotient down.

Conclusion

To calculate the day of the year with JavaScript, we can calculate the difference between the current datetime and the midnight of the first day of the year.

Categories
JavaScript Answers

How to split JavaScript string on upper case characters?

Sometimes, we want to split JavaScript string on upper case characters.

In this article, we’ll look at how to split JavaScript string on upper case characters.

How to split JavaScript string on upper case characters?

To split JavaScript string on upper case characters, we can call the string split method with a regex.

For instance, we write

const a = "ThisIsTheStringToSplit".split(/(?=[A-Z])/);

to call split with /(?=[A-Z])/ to split the string on upper case characters.

We use ?= to keep the whole word intact after splitting.

split returns an array with the split strings.

Conclusion

To split JavaScript string on upper case characters, we can call the string split method with a regex.

Categories
JavaScript Answers

How to determine distance from the top of a div to top of window with JavaScript?

Sometimes, we want to determine distance from the top of a div to top of window with JavaScript.

In this article, we’ll look at how to determine distance from the top of a div to top of window with JavaScript.

How to determine distance from the top of a div to top of window with JavaScript?

To determine distance from the top of a div to top of window with JavaScript, we can use the top property.

For instance, we write

const el = document.getElementById("someElement");
const distanceFromTop = el.getBoundingClientRect().top;

to select the element with getElementById.

Then we call getBoundingClientRect to get an object with the distance from the top of the window to the top of the element with the top property.

Conclusion

To determine distance from the top of a div to top of window with JavaScript, we can use the top property.

Categories
JavaScript Answers

How to change element style attribute dynamically using JavaScript?

Sometimes, we want to change element style attribute dynamically using JavaScript.

In this article, we’ll look at how to change element style attribute dynamically using JavaScript.

How to change element style attribute dynamically using JavaScript?

To change element style attribute dynamically using JavaScript, we can set the property in the element’s style property.

For instance, we write

document.getElementById("xyz").style["padding-top"] = "10px";

to select the element with ID xyz with

document.getElementById("xyz")

Then we set its padding-top style to '10px' by setting its style["padding-top"] property.

Conclusion

To change element style attribute dynamically using JavaScript, we can set the property in the element’s style property.

Categories
JavaScript Answers

How to use JavaScript to change the meta tags of the page?

Sometimes, we want to use JavaScript to change the meta tags of the page.

In this article, we’ll look at how to use JavaScript to change the meta tags of the page.

How to use JavaScript to change the meta tags of the page?

To use JavaScript to change the meta tags of the page, we can set the properties that are available in a meta element.

For instance, we write

document.getElementsByTagName("meta")["keywords"].content =
  "My new page keywords!!";
document.getElementsByTagName("meta")["description"].content =
  "My new page description!!";

to select the meta tag with the name attribute set to keywords with

document.getElementsByTagName("meta")["keywords"]

and set its content attribute value.

Likewise, we select the meta tag with the name attribute description with

document.getElementsByTagName("meta")["description"]

and set its content attribute value.

Conclusion

To use JavaScript to change the meta tags of the page, we can set the properties that are available in a meta element.