Categories
JavaScript Answers

How to get a UTC Date object in Node.js and JavaScript?

Sometimes, we want to get a UTC Date object in Node.js and JavaScript.

In this article, we’ll look at how to get a UTC Date object in Node.js and JavaScript.

How to get a UTC Date object in Node.js and JavaScript?

To get a UTC Date object in Node.js and JavaScript, we can call the date’s toUTCString method.

For instance, we write:

console.log(new Date().toUTCString())

to create a new Date instance and call toUTCString to return the UTC date string version of the current date time.

Conclusion

To get a UTC Date object in Node.js and JavaScript, we can call the date’s toUTCString method.

Categories
JavaScript Answers

How to check if server is online with JavaScript?

Sometimes, we want to check if server is online with JavaScript.

In this article, we’ll look at how to check if server is online with JavaScript.

How to check if server is online with JavaScript?

To check if server is online with JavaScript, we can use the AbortController to cancel the request when the request times out.

For instance, we write:

(async () => {
  const url = 'https://example.com'
  const controller = new AbortController();
  const signal = controller.signal;
  const options = {
    mode: 'no-cors',
    signal
  };
  await fetch(url, options)

  setTimeout(() => {
    controller.abort()
  }, 3000)
})()

to call fetch with a url and options.

options has the signal property assigned to the AbortController instance’s signal property.

Then we call controller.abort to cancel the request to url after 3000 milliseconds by calling it in the setTimeout callback.

If the request is done before 3 seconds, then abort will do nothing.

Conclusion

To check if server is online with JavaScript, we can use the AbortController to cancel the request when the request times out.

Categories
JavaScript Answers

How to get a date range using moment.js and JavaScript?

Sometimes, we want to get a date range using moment.js and JavaScript.

In this article, we’ll look at how to get a date range using moment.js and JavaScript.

How to get a date range using moment.js and JavaScript?

To get a date range using moment.js and JavaScript, we can use the moment add method.

For instance, we write:

const startDate = moment('2022-01-01')
const endDate = moment('2022-01-21')
let d = startDate
const range = []
while (+d.toDate() < +endDate.toDate()) {
  range.push(d.toDate())
  d = d.add(1, 'days')
}
console.log(range)

to set the start and end date of the range to startDate and endDate.

Then we set d to startDate.

Next, we use a while loop that runs until d is later than endDate.

In the loop, we call range.push with d.toDate() to push the moment object d converted to a date.

Then we call add with 1 and 'days' to add one day.

Therefore, range is has the dates from Jan 1, 2022 to Jan 20, 2022.

Conclusion

To get a date range using moment.js and JavaScript, we can use the moment add method.

Categories
JavaScript Answers

How to remove whitespaces with a regular expression and JavaScript?

Sometimes, we want to remove whitespaces with a regular expression and JavaScript.

In this article, we’ll look at how to remove whitespaces with a regular expression and JavaScript.

How to remove whitespaces with a regular expression and JavaScript?

To remove whitespaces with a regular expression and JavaScript, we can use the string replace method.

For instance, we write:

const str = "    bob is a good      boy     "
const s = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
console.log(s)

to call str.replace with the /^\s+|\s+$|\s+(?=\s)/g regex.

The regex matches whitespaces at the start of a string with ^\s+.

It matches whitespaces at the end of a string with \s+$.

And it matches multiple whitespaces within the string with \s+(?=\s).

The g flag returns all matches in the string.

And then we replace them all with an empty string.

Therefore, s is 'bob is a good boy'.

Conclusion

To remove whitespaces with a regular expression and JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to convert strings to bigInt in JavaScript?

Sometimes, we want to convert strings to bigInt in JavaScript.

In this article, we’ll look at how to convert strings to bigInt in JavaScript.

How to convert strings to bigInt in JavaScript?

To convert strings to bigInt in JavaScript, we can use the BigInt function.

For instance, we write:

const string = '8795485488564835575723945398498489398434434';
const bigInt = BigInt(string);
console.log(bigInt)

to call BigInt with string to convert string to a bigint.

Therefore, bigInt is 8795485488564835575723945398498489398434434n .

Conclusion

To convert strings to bigInt in JavaScript, we can use the BigInt function.