Categories
JavaScript Answers

How to convert local time to UTC time with moment.js and JavaScript?

Sometimes, we want to convert local time to UTC time with moment.js and JavaScript.

In this article, we’ll look at how to convert local time to UTC time with moment.js and JavaScript.

How to convert local time to UTC time with moment.js and JavaScript?

To convert local time to UTC time with moment.js and JavaScript, we can use the moment utc and format method.

For instance, we write:

const d = new Date(2022, 1, 1)
const s = moment.utc(d).format("YYYY-MM-DD HH:mm:ss Z")
console.log(s)

to create the date d in local time.

Then we create a UTC moment object from d with moment.utc.

Next, we call format to with a format string that returns the date and time with the time zone.

As a result, s is '2022-02-01 08:00:00 +00:00'.

Conclusion

To convert local time to UTC time with moment.js and JavaScript, we can use the moment utc and format method.

Categories
JavaScript Answers

How to create a UTC JavaScript date?

Sometimes, we want to create a UTC JavaScript date.

In this article, we’ll look at how to create a UTC JavaScript date.

How to create a UTC JavaScript date?

To create a UTC JavaScript date, we can use the Date.UTC method.

For instance, we write:

const d = new Date(Date.UTC(2022, 10, 5));
console.log(d.toUTCString());

to call Date.UTC to create a timestamp set to Nov 5, 2022 in UTC.

Then we convert that to a JavaScript date object with the Date constructor.

Therefore, d.toUTCString returns 'Sat, 05 Nov 2022 00:00:00 GMT'.

Conclusion

To create a UTC JavaScript date, we can use the Date.UTC method.

Categories
JavaScript Answers

How to convert a JavaScript object to a query string?

Sometimes, we want to convert a JavaScript object to a query string.

In this article, we’ll look at how to convert a JavaScript object to a query string.

How to convert a JavaScript object to a query string?

To convert a JavaScript object to a query string, we can use the Object.entries method and some array methods.

For instance, we write:

const obj = {
  foo: "22",
  bar: "23434"
};

const queryString = Object.entries(obj)
  .map(([key, value]) => {
    return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
  })
  .join('&')
console.log(queryString)

to call Object.entries with obj to convert obj to an array of key-value pair arrays.

Then we call map with a callback to combine the key and value into a query parameter string.

Next, we call join with '&' to join the query parameters together.

As a result, queryString is 'foo=22&bar=23434'.

Conclusion

To convert a JavaScript object to a query string, we can use the Object.entries method and some array methods.

Categories
JavaScript Answers

How to write the equivalent of Python dictionary’s get method in JavaScript?

Sometimes, we want to write the equivalent of Python dictionary’s get method in JavaScript.

In this article, we’ll look at how to write the equivalent of Python dictionary’s get method in JavaScript.

How to write the equivalent of Python dictionary’s get method in JavaScript?

To write the equivalent of Python dictionary’s get method in JavaScript, we can use hasOwnProperty.

For instance, we write:

const obj = {
  foo: 1
}
const x = obj.hasOwnProperty("foo") ? obj.foo : "default";
const y = obj.hasOwnProperty("bar") ? obj.bar : "default";
console.log(x, y)

to call obj.hasOwnProperty with the keys we’re looking for in obj.

If hasOwnProperty returns true, that means the key is in obj.

And so we return the property’s value.

Otherwise, we return 'default'.

Therefore, x is 1 and y is default.

Conclusion

To write the equivalent of Python dictionary’s get method in JavaScript, we can use hasOwnProperty.

Categories
JavaScript Answers

How to write a JavaScript regex for number with decimals and thousand separator?

Sometimes, we want to write a JavaScript regex for number with decimals and thousand separator.

In this article, we’ll look at how to write a JavaScript regex for number with decimals and thousand separator.

How to write a JavaScript regex for number with decimals and thousand separator?

To write a JavaScript regex for number with decimals and thousand separator, we can use /^\d{1,3}(,\d{3})*(\.\d+)?$/.

^\d{1,3} matches groups of digits up to 3 digits long at the start of the string.

We match the digit groups in the middle of the string with (,\d{3})*.

And we match the digit group at the end of the string with (\.\d+)?$.

Conclusion

To write a JavaScript regex for number with decimals and thousand separator, we can use /^\d{1,3}(,\d{3})*(\.\d+)?$/.