Categories
JavaScript Answers

How to convert normal date to Unix timestamp with JavaScript?

Sometimes, we want to convert normal date to Unix timestamp with JavaScript.

In this article, we’ll look at how to convert normal date to Unix timestamp with JavaScript.

How to convert normal date to Unix timestamp with JavaScript?

To convert normal date to Unix timestamp with JavaScript, we can use the getTime method.

For instance, we write

const d = "2022-01-01T00:00:00.000Z";
console.log(new Date(d).getTime());

to create the Date object from datetime string d.

And then we call the getTime method to get the Unix timestamp in milliseconds.

Conclusion

To convert normal date to Unix timestamp with JavaScript, we can use the getTime method.

Categories
JavaScript Answers

How to resize Google Maps marker icon image with JavaScript?

Sometimes, we want to resize Google Maps marker icon image with JavaScript.

In this article, we’ll look at how to resize Google Maps marker icon image with JavaScript.

How to resize Google Maps marker icon image with JavaScript?

To resize Google Maps marker icon image with JavaScript, we create a marker with the google.maps.Marker constructor.

For instance, we write

const icon = {
  url: "../res/sit_marron.png", 
  scaledSize: new google.maps.Size(50, 50), 
  origin: new google.maps.Point(0, 0), 
  anchor: new google.maps.Point(0, 0), 
};

const marker = new google.maps.Marker({
  position: new google.maps.LatLng(lat, lng),
  map,
  icon,
});

to create the icon object to set the url, scaledSizr, origin and anchor point of the icon.

And then we use the Marker constructor with an object that sets the position of the marker, the map to display it on, and the icon to use.

Conclusion

To resize Google Maps marker icon image with JavaScript, we create a marker with the google.maps.Marker constructor.

Categories
JavaScript Answers

How to generate range of numbers from 0 to n in JavaScript ES2015 only?

Sometimes, we want to generate range of numbers from 0 to n in JavaScript ES2015 only.

In this article, we’ll look at how to generate range of numbers from 0 to n in JavaScript ES2015 only.

How to generate range of numbers from 0 to n in JavaScript ES2015 only?

To generate range of numbers from 0 to n in JavaScript ES2015 only, we can use the array keys method.

For instance, we write

const nums = [...Array(n).keys()];

to use Array(n) to get an array with n empty slots.

Then we call keys on it to get the indexes of the array in an array.

Next, we use the spread operator to spread the indexes into a new array.

Conclusion

To generate range of numbers from 0 to n in JavaScript ES2015 only, we can use the array keys method.

Categories
JavaScript Answers

How to use regex in JavaScript to return just numbers?

Sometimes, we want to use regex in JavaScript to return just numbers.

In this article, we’ll look at how to use regex in JavaScript to return just numbers.

How to use regex in JavaScript to return just numbers?

To use regex in JavaScript to return just numbers, we can use the string match method with a regex pattern that match the numbers.

For instance, we write

const numberPattern = /\d+/g;
const numbers = "something102asdfkj1948948".match(numberPattern);

to call match with the numberPattern regex to match all numbers in the string 'something102asdfkj1948948'.

And then we get the results returned as an array.

Conclusion

To use regex in JavaScript to return just numbers, we can use the string match method with a regex pattern that match the numbers.

Categories
JavaScript Answers

How to concatenate regex literals in JavaScript?

Sometimes, we want to concatenate regex literals in JavaScript.

In this article, we’ll look at how to concatenate regex literals in JavaScript.

How to concatenate regex literals in JavaScript?

To concatenate regex literals in JavaScript, we can get the regex pattern with the source property and then we get the flags with various properties.

For instance, we write

const r1 = /abc/g;
const r2 = /def/;
const r3 = new RegExp(
  r1.source + r2.source,
  (r1.global ? "g" : "") +
    (r1.ignoreCase ? "i" : "") +
    (r1.multiline ? "m" : "")
);

to use the source property to get the /abc/ and /def patterns and concatenate them.

And then we use the global, ignoreCase, and multline to get those flags from r1.

Then we concatenate the patterns and flags together and put them in the RegExp constructor.

Conclusion

To concatenate regex literals in JavaScript, we can get the regex pattern with the source property and then we get the flags with various properties.