Categories
JavaScript Answers

How to convert Unix epoch time to date in Google Sheets and JavaScript?

Sometimes, we want to convert Unix epoch time to date in Google Sheets and JavaScript.

In this article, we’ll look at how to convert Unix epoch time to date in Google Sheets and JavaScript.

How to convert Unix epoch time to date in Google Sheets and JavaScript?

To convert Unix epoch time to date in Google Sheets and JavaScript, we can add a JavaScript function that does the conversion.

We open the script editor with Tools > Script Editor… >

Then we add

function millisToDate(timeInMillis) {
  const yourDateFromMillis = new Date(timeInMillis);
  return yourDateFromMillis;
}

to define the millisToDate function.

In it, we create a Date object from the timeInMillis parameter.

timeInMillis is the Unix timestamp in milliseconds.

Then we return the Date object.

Conclusion

To convert Unix epoch time to date in Google Sheets and JavaScript, we can add a JavaScript function that does the conversion.

Categories
JavaScript Answers

How to get JavaScript window.location.href without the hash?

Sometimes, we want to get JavaScript window.location.href without the hash.

In this article, we’ll look at how to get JavaScript window.location.href without the hash.

How to get JavaScript window.location.href without the hash?

To get JavaScript window.location.href without the hash, we can use the string split method.

For instance, we write

const [uri] = window.location.href.split("#");

to call window.location.href.split withh '#' and destructure the first part to get the URL without the hash part and assign it to uri.

Conclusion

To get JavaScript window.location.href without the hash, we can use the string split method.

Categories
React Answers

How to use ES6 import alias syntax for React components?

Sometimes, we want to use ES6 import alias syntax for React components.

In this article, we’ll look at how to use ES6 import alias syntax for React components.

How to use ES6 import alias syntax for React components?

To use ES6 import alias syntax for React components, we can use the as keyword.

For instance, we write

import { default as StyledLibrary } from "component-library";

to import a default export from the component-library module.

We add a default export with

export default StyledLibrary;

Conclusion

To use ES6 import alias syntax for React components, we can use the as keyword.

Categories
JavaScript Answers

How to get the user agent with JavaScript?

Sometimes, we want to get the user agent with JavaScript.

In this article, we’ll look at how to get the user agent with JavaScript.

How to get the user agent with JavaScript?

To get the user agent with JavaScript, we use the navigator.userAgent property.

For instance, we write

<input type="text" id="UserAgent">

to add an input.

Then we write

document.getElementById('UserAgent').value = navigator.userAgent;

to put the user agent value into the input with navigator.userAgent.

Conclusion

To get the user agent with JavaScript, we use the navigator.userAgent property.

Categories
JavaScript Answers

How to search an array for a substring match in JavaScript?

Sometimes, we want to search an array for a substring match in JavaScript.

In this article, we’ll look at how to search an array for a substring match in JavaScript.

How to search an array for a substring match in JavaScript?

To search an array for a substring match in JavaScript, we can use the array filter method.

For instance, we write

const items = ["item 1", "thing", "id-3-text", "class"];
const matches = items.filter((s) => s.includes("thi"));

to call filter with a callback that calls the string includes method to return an array that has strings in items that has the 'thi' substring.

Conclusion

To search an array for a substring match in JavaScript, we can use the array filter method.