Categories
JavaScript Answers

How to convert timestamp to human readable format with JavaScript?

Sometimes, we want to convert timestamp to human readable format with JavaScript.

In this article, we’ll look at how to convert timestamp to human readable format with JavaScript.

How to convert timestamp to human readable format with JavaScript?

To convert timestamp to human readable format with JavaScript, we can use the date’s setTime method.

For instance, we write

const newDate = new Date();
newDate.setTime(unixtime * 1000);
const dateString = newDate.toUTCString();

to create a Date object.

Then we call setTime with unixtime * 1000 to set the timestamp of newDate to unixtime * 1000.

unixtime is the timestamp of the date in seconds.

then we call toUTCString to return a human readable date string from newDate.

Conclusion

To convert timestamp to human readable format with JavaScript, we can use the date’s setTime method.

Categories
JavaScript Answers

How to return data from axios API with JavaScript?

Sometimes, we want to return data from axios API with JavaScript.

In this article, we’ll look at how to return data from axios API with JavaScript.

How to return data from axios API with JavaScript?

To return data from axios API with JavaScript, we can use the return statement.

For instance, we write

const axiosTest = async () => {
  const response = await axios.get(url);
  return response.data;
};

to define the axiosTest function that returns the response data from the axios.get method.

We call axios.get with url to return a promise with the response from making a get request to the url.

Then we return response.data to return the data.

Next, in an async function, we write

const data = await axiosTest();

to get the response data from axiosTest.

Conclusion

To return data from axios API with JavaScript, we can use the return statement.

Categories
JavaScript Answers

How to loop through childNodes with JavaScript?

Sometimes, we want to loop through childNodes with JavaScript.

In this article, we’ll look at how to loop through childNodes with JavaScript.

How to loop through childNodes with JavaScript?

To loop through childNodes with JavaScript, we can use a for-of loop.

For instance, we write

const children = element.childNodes;

for (const child of children) {
  console.log(child);
}

to get the child nodes of the element with the childNodes property.

Then we loop through the children child nodes with a for-of loop.

We get the node being looped through from child.

Conclusion

To loop through childNodes with JavaScript, we can use a for-of loop.

Categories
JavaScript Answers

How to calculate the date that is 2 days ago with JavaScript?

Sometimes, we want to calculate the date that is 2 days ago with JavaScript.

In this article, we’ll look at how to calculate the date that is 2 days ago with JavaScript.

How to calculate the date that is 2 days ago with JavaScript?

To calculate the date that is 2 days ago with JavaScript, we use the getDate and setDate methods.

For instance, we write

const d = new Date();
d.setDate(d.getDate() - 2);
console.log(d.toString());

to create the date d with the current date and time.

Then we get the date of the current date with getDate and subtract 2 from it to get the date 2 days ago.

And then we use the difference as the argument for setDate.

Finally, we call toString to return a date string version of d.

Conclusion

To calculate the date that is 2 days ago with JavaScript, we use the getDate and setDate methods.

Categories
JavaScript Answers

How to use variable in string match with JavaScript?

Sometimes, we want to use variable in string match with JavaScript.

In this article, we’ll look at how to use variable in string match with JavaScript.

How to use variable in string match with JavaScript?

To use variable in string match with JavaScript, we can use the RegExp constructor to create our regex.

For instance, we write

const word = "Web Development Tutorial";
const vowels = "[aeiou]";
const re = new RegExp(vowels, "gi");
const arr = word.match(re);

to create a regex that match vowels with

const re = new RegExp(vowels, "gi");

The first argument is the regex pattern string and the 2nd argument is the string with the flags.

g means we return all matches and i means we search in a case-insensitive manner.

Then we call word.match with re to return an array of matches.

Conclusion

To use variable in string match with JavaScript, we can use the RegExp constructor to create our regex.