Categories
JavaScript Answers

How to get the server’s port with JavaScript and Express?

Sometimes, we want to get the server’s port with JavaScript and Express.

In this article, we’ll look at how to get the server’s port with JavaScript and Express.

How to get the server’s port with JavaScript and Express?

To get the server’s port with JavaScript and Express, we can use the listener.address method.

For instance, we write

const app = require("express")();
//...
const listener = app.listen(8888, () => {
  console.log("Listening on port ", listener.address().port);
});

to call app.listen with the port to listen to.

And then we call listener.address and use the returned object’s port property to get the port being listened to.

Conclusion

To get the server’s port with JavaScript and Express, we can use the listener.address method.

Categories
JavaScript Answers

How to subtract 2 hours from user’s local time with JavaScript?

Sometimes, we want to subtract 2 hours from user’s local time with JavaScript.

In this article, we’ll look at how to subtract 2 hours from user’s local time with JavaScript.

How to subtract 2 hours from user’s local time with JavaScript?

To subtract 2 hours from user’s local time with JavaScript, we can use the setHours method.

For instance, we write

const d = new Date();
d.setHours(d.getHours() - 2);

to create the Date object d which has the user’s current local date time.

Then we call getHours to get the hour of day from date d.

And we subtract that by 2 and use the difference as the argument of setGHours to subtract 2 hours from user’s local time.

Conclusion

To subtract 2 hours from user’s local time with JavaScript, we can use the setHours method.

Categories
JavaScript Answers

How to convert a date to ISO 8601 format with time zone offset in JavaScript?

Sometimes, we want to convert a date to ISO 8601 format with time zone offset in JavaScript.

In this article, we’ll look at how to convert a date to ISO 8601 format with time zone offset in JavaScript.

How to convert a date to ISO 8601 format with time zone offset in JavaScript?

To convert a date to ISO 8601 format with time zone offset in JavaScript, we can use the toLocaleString method.

For instance, we write

const d = new Date().toLocaleString("sv", { timeZoneName: "short" });

to call toLocaleString with the locale string and { timeZoneName: "short" } to return a date time string formatted into ISO format for the given locale.

We set timeZoneName to 'short' to add the short time zone string to the date time string.

Conclusion

To convert a date to ISO 8601 format with time zone offset in JavaScript, we can use the toLocaleString method.

Categories
JavaScript Answers

How to append array to FormData and send via Ajax with JavaScript?

To append array to FormData and send via Ajax with JavaScript, we can use the spread operator.

For instance, we write

const formData = new FormData();
const arr = ["this", "is", "an", "array"];

for (const a of arr) {
  formData.append("arr[]", a);
}

console.log(...formData);

to loop through the arr array and call formData.append with the key and value of each FormData entry.

Then we use the spread operator to spread the formData key-value entries as arguments of console.log as arrays.

Conclusion

To append array to FormData and send via Ajax with JavaScript, we can use the spread operator.

Categories
JavaScript Answers

How to get all selected values from a multi-select element with JavaScript?

Sometimes, we want to get all selected values from a multi-select element with JavaScript.

In this article, we’ll look at how to get all selected values from a multi-select element with JavaScript.

How to get all selected values from a multi-select element with JavaScript?

To get all selected values from a multi-select element with JavaScript, we use the selectedOptions property.

For instance, we write

<select id="select-meal-type" multiple="multiple">
  <option value="1">Breakfast</option>
  <option value="2" selected>Lunch</option>
  <option value="3">Dinner</option>
  <option value="4" selected>Snacks</option>
  <option value="5">Dessert</option>
</select>

to add a multi-select box.

Then we write

const options = document.getElementById("select-meal-type").selectedOptions;
const values = Array.from(options).map(({ value }) => value);
console.log(values);

to get the select element by ID.

We get the selected options with selectedOptions.

Then we convert the selectedOptions into an array with Array.from.

And we call map with a callback to map the selected option objects to get the value from them.

The value has the value attribute value.

Conclusion

To get all selected values from a multi-select element with JavaScript, we use the selectedOptions property.