Categories
JavaScript Answers

How to convert JavaScript date time to MySQL datetime?

Sometimes, we want to convert JavaScript date time to MySQL datetime.

In this article, we’ll look at how to convert JavaScript date time to MySQL datetime.

How to convert JavaScript date time to MySQL datetime?

To convert JavaScript date time to MySQL datetime, we can call the date’s toISOString method.

For instance, we write

const date = new Date().toISOString().slice(0, 19).replace("T", " ");

to call toISOString to return a date string of the date in ISO8601 format.

Then we call slice with 0 and 19 to get the datetime part of the string.

And we replace 'T' with a space with replace.

Conclusion

To convert JavaScript date time to MySQL datetime, we can call the date’s toISOString method.

Categories
JavaScript Answers

How to get GPS location from the web browser with JavaScript?

Sometimes, we want to get GPS location from the web browser with JavaScript.

In this article, we’ll look at how to get GPS location from the web browser with JavaScript.

How to get GPS location from the web browser with JavaScript?

To get GPS location from the web browser with JavaScript, we can use the navigator.geolocation.getCurrentPosition method.

For instance, we write

navigator.geolocation.getCurrentPosition((location) => {
  console.log(location.coords.latitude);
  console.log(location.coords.longitude);
  console.log(location.coords.accuracy);
});

to call getCurrentLocation with a callback that has the location object parameter.

We get the latitude, longitude, and accuracy from the location.coords property.

Conclusion

To get GPS location from the web browser with JavaScript, we can use the navigator.geolocation.getCurrentPosition method.

Categories
JavaScript Answers

How to find the height of text on an HTML canvas with JavaScript?

Sometimes, we want to find the height of text on an HTML canvas with JavaScript.

In this article, we’ll look at how to find the height of text on an HTML canvas with JavaScript.

How to find the height of text on an HTML canvas with JavaScript?

To find the height of text on an HTML canvas with JavaScript, we can use the canvas context’s measureText method.

For instance, we write

const metrics = ctx.measureText(text);
const fontHeight =
  metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent;
const actualHeight =
  metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;

to call measureText on the ctx canvas context.

We call it with the text string to get the text‘s dimensions.

And then we get the fontHeight and actualHeight with

metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent;

and

metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;

Conclusion

To find the height of text on an HTML canvas with JavaScript, we can use the canvas context’s measureText method.

Categories
JavaScript Answers

How to access JSON property with “-” dash with JavaScript?

Sometimes, we want to access JSON property with "-" dash with JavaScript.

In this article, we’ll look at how to access JSON property with "-" dash with JavaScript.

How to access JSON property with "-" dash with JavaScript?

To access JSON property with "-" dash with JavaScript, we can use a string.

For instance, we write

const profileId = jsonObj["profile-id"];

to access the profile-id property from jsonObj with jsonObj["profile-id"].

Conclusion

To access JSON property with "-" dash with JavaScript, we can use a string.

Categories
JavaScript Answers

How to limit concurrency when using JavaScript’s Promise.all()?

Sometimes, we want to limit concurrency when using JavaScript’s Promise.all().

In this article, we’ll look at how to limit concurrency when using JavaScript’s Promise.all().

How to limit concurrency when using JavaScript’s Promise.all()?

To limit concurrency when using JavaScript’s Promise.all(), we can call splice on the promise array to return a new promise functions array with fewer items than the original.

For instance, we write

while (funcs.length) {
  await Promise.all(funcs.splice(0, 100).map((f) => f()));
}

to call funcs.splice to remove the firs 100 items from the funcs promise functions array and return them in a new array.

Then we call map to call f to return the promises.

And we use Promise.all to run the promises in parallel.

We use the while loop to run all the promises in funcs until they’re all removed from it.

Conclusion

To limit concurrency when using JavaScript’s Promise.all(), we can call splice on the promise array to return a new promise functions array with fewer items than the original.