Categories
JavaScript Answers

How to set the value of a datetime-local input to a date with JavaScript?

To set the value of a datetime-local input to a date with JavaScript, we can set the input value to a date string.

For instance, we write:

<input id="dt" type="datetime-local" />

to add the datetime-local input.

Then we write:

const now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
document.getElementById('dt').value = now.toISOString().slice(0, 16);

We create the now date with the Date constructor.

Then we call setMinutes to change the time to local time by calling it with now.getMinutes() - now.getTimezoneOffset().

Next we set the value of the input with:

document.getElementById('dt').value = now.toISOString().slice(0, 16);

We set the value to a date string by taking the first 16 characters of the date time string with now.toISOString().slice(0, 16).

Categories
JavaScript Answers

How to display a loading screen while site content loads with JavaScript?

Sometimes, we want to display a loading screen while site content loads with JavaScript.

In this article, we’ll look at how to display a loading screen while site content loads with JavaScript.

How to display a loading screen while site content loads with JavaScript?

To display a loading screen while site content loads with JavaScript, we can use the jQuery on method to watch for the load event to be emitted.

For instance, we write:

$(window).on('load', () => {
  $("#loader").fadeOut("fast");
});

to call select the window with $(window).

Then we call on with 'load' and a function that selects the div and call fadeOut on it to make it disappear with the fade out effect.

We call it with 'fast' to make it fade out fast.

Then we add some styles to the div by writing:

#loader {
  width: 100%;
  height: 100%;
  background-color: white;
  margin: 0;
}

Conclusion

To display a loading screen while site content loads with JavaScript, we can use the jQuery on method to watch for the load event to be emitted.

Categories
JavaScript Answers

How to add a HTML5 Canvas pie chart with JavaScript?

Sometimes, we want to add a HTML5 Canvas pie chart with JavaScript.

In this article, we’ll look at how to add a HTML5 Canvas pie chart with JavaScript.

How to add a HTML5 Canvas pie chart with JavaScript?

To add a HTML5 Canvas pie chart with JavaScript, we can use the canvas moveTo, arc, lineTo, and fill methods.

For instance, we write:

<canvas id="can" width="200" height="200" />

to add a canvas element.

Then we write:

const canvas = document.getElementById("can");
const ctx = canvas.getContext("2d");
let lastend = 0;
const data = [200, 60, 15];
const myTotal = data.reduce((a, b) => a + b, 0);
const myColor = ['red', 'green', 'blue'];

for (const [i, d] of data.entries()) {
  ctx.fillStyle = myColor[i];
  ctx.beginPath();
  ctx.moveTo(canvas.width / 2, canvas.height / 2);
  ctx.arc(canvas.width / 2, canvas.height / 2, canvas.height / 2, lastend, lastend + (Math.PI * 2 * (d / myTotal)), false);
  ctx.lineTo(canvas.width / 2, canvas.height / 2);
  ctx.fill();
  lastend += Math.PI * 2 * (d / myTotal);
}

We select the canvas element with document.getElementById.

Then we get the context with getContext.

Next, we loop through the data entries with a for-of loop to add the pie slices.

To add the slice, we call beginPath to start drawing.

Then we call moveTo to move to the coordinates we want to start drawing.

Next, we call arc to draw the arc with the width, height and angle.

Then we call lineTo to move the line to the end of the slice.

Finally, we call fill to fill the chart with the fillStyle color.

And then we repeat this with all the data entries.

Conclusion

To add a HTML5 Canvas pie chart with JavaScript, we can use the canvas moveTo, arc, lineTo, and fill methods.

Categories
JavaScript Answers

How to pass an array into the JavaScript string fromCharCode method?

Sometimes, we want to pass an array into the JavaScript string fromCharCode method.

In this article, we’ll look at how to pass an array into the JavaScript string fromCharCode method.

How to pass an array into the JavaScript string fromCharCode method?

To pass an array into the JavaScript string fromCharCode method, we call String.fromCharCode method with with an array spread as the arguments of it.

For instance, we write:

const array = [72, 69, 76, 76, 79];
const chars = String.fromCharCode(...array)
console.log(chars)

to call String.fromCharCodewith thearray` entries as the arguments.

We unpacked array with the spread operator.

And then we assign the returned result to chars.

As a result, we get that chars is 'HELLO'.

Conclusion

To pass an array into the JavaScript string fromCharCode method, we call String.fromCharCode method with with an array spread as the arguments of it.

Categories
JavaScript Answers

How to convert result from JavaScript Date.now() to yyyy/MM/dd hh:mm:ss ffff format?

Sometimes, we want to convert result from JavaScript Date.now() to yyyy/MM/dd hh:mm:ss ffff format.

In this article, we’ll look at how to convert result from JavaScript Date.now() to yyyy/MM/dd hh:mm:ss ffff format.

How to convert result from JavaScript Date.now() to yyyy/MM/dd hh:mm:ss ffff format?

To convert result from JavaScript Date.now() to yyyy/MM/dd hh:mm:ss ffff format, we can use moment.js.

For instance, we write:

const d = moment().format('YYYY/MM/D hh:mm:ss SSS')
console.log(d)

We get the current date time with moment().

Then we call format with 'YYYY/MM/D hh:mm:ss SSS' to return a string of the current date time in yyyy/MM/dd hh:mm:ss ffff format.

Therefore d is '2022/01/7 04:58:24 671'.

Conclusion

To convert result from JavaScript Date.now() to yyyy/MM/dd hh:mm:ss ffff format, we can use moment.js.