Categories
JavaScript Answers

How to get the UNIX timestamp for the start of today in JavaScript?

Sometimes, we want to get the UNIX timestamp for the start of today in JavaScript.

In this article, we’ll look at how to get the UNIX timestamp for the start of today in JavaScript.

How to get the UNIX timestamp for the start of today in JavaScript?

To get the UNIX timestamp for the start of today in JavaScript, we can use the Date constructor.

For instance, we write:

const now = new Date();
const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const timestamp = startOfDay / 1000;
console.log(timestamp)

to create a new Date instance with the current date and time.

Then we create another Date instance with the year, month and day of the month of today and assigned it to startOfDay.

This will set the time to midnight of the same day.

Then we divide startOfDay by 1000 to return the UNIX timestamp.

Conclusion

To get the UNIX timestamp for the start of today in JavaScript, we can use the Date constructor.

Categories
JavaScript Answers

How to fix an element not updating when the innerHTML property is set with JavaScript?

Sometimes, we want to fix an element not updating when the innerHTML property is set with JavaScript.

In this article, we’ll look at how to fix an element not updating when the innerHTML property is set with JavaScript.

How to fix an element not updating when the innerHTML property is set with JavaScript?

To fix an element not updating when the innerHTML property is set with JavaScript, we should make sure the element is loded before we try to set its innerHTML value.

For instance, we write:

<h1 id="ma">s</h1>

to add an h1 element.

Then we write:

window.onload = () => {
  document.getElementById("ma").innerHTML = "JavaScript";
}

to set the window.onload property to a function that selects the h1 element and set the innerHTML property to 'JavaScript'.

window.onload functions when the page is loaded.

Conclusion

To fix an element not updating when the innerHTML property is set with JavaScript, we should make sure the element is loded before we try to set its innerHTML value.

Categories
JavaScript Answers

How to validate a timestamp in JavaScript?

Sometimes, we want to validate a timestamp in JavaScript.

In this article, we’ll look at how to validate a timestamp in JavaScript.

How to validate a timestamp in JavaScript?

To validate a timestamp in JavaScript, we can call getTime on a Date instance and see if it returns a value greater than 0.

For instance, we write:

console.log(new Date('2012-08-09').getTime() > 0)
console.log(new Date('abc').getTime() > 0)

Then the first log will log true since it’s a valid date.

And the 2nd log will log true since it’s not a valid date.

getTime returns a timestamp in milliseconds.

Conclusion

To validate a timestamp in JavaScript, we can call getTime on a Date instance and see if it returns a value greater than 0.

Categories
JavaScript Answers

How to show or hide an image with JavaScript?

Sometimes, we want to show or hide an image with JavaScript.

In this article, we’ll look at how to show or hide an image with JavaScript.

How to show or hide an image with JavaScript?

To show or hide an image with JavaScript, we can set the style.visibility property of the img element.

For instance, we write:

<button id='showBtn'>show image</button>
<button id='hideBtn'>hide image</button>
<img src="https://picsum.photos/200/300">

to add buttons and img elements.

Then we write:

const showBtn = document.getElementById('showBtn')
const hideBtn = document.getElementById('hideBtn')
const img = document.querySelector('img');

showBtn.onclick = () => {
  img.style.visibility = 'visible';
}

hideBtn.onclick = () => {
  img.style.visibility = 'hidden';
}

We select the elements with getElementById and querySelector.

Then we set the onclick property of the buttons to functions that sets the img.style.visibility property.

'visible' makes the img element visible and 'hidden' makes the img element hidden.

Conclusion

To show or hide an image with JavaScript, we can set the style.visibility property of the img element.

Categories
JavaScript Answers

How to convert a datetime string to timestamp in JavaScript?

Sometimes, we want to convert a datetime string to timestamp in JavaScript.

In this article, we’ll look at how to convert a datetime string to timestamp in JavaScript.

How to convert a datetime string to timestamp in JavaScript?

To convert a datetime string to timestamp in JavaScript, we can call Date.parse.

For instance, we write:

const s = '2022-01-01'
const timeInMillis = Date.parse(s);
console.log(timeInMillis)

to call Date.parse with s.

Then we get that timeInMillis is 1640995200000.

Conclusion

To convert a datetime string to timestamp in JavaScript, we can call Date.parse.