Categories
JavaScript Answers

How to get the number of years since a date, not rounded up with Moment.js and JavaScript?

Sometimes, we want to get the number of years since a date, not rounded up with Moment.js and JavaScript.

In this article, we’ll look at how to get the number of years since a date, not rounded up with Moment.js and JavaScript.

How to get the number of years since a date, not rounded up with Moment.js and JavaScript?

To get the number of years since a date, not rounded up with Moment.js and JavaScript, we call the diff method.

For instance, we write

const years = moment().diff("1981-01-01", "years");

to get the number of years difference between today and January 1, 1981.

We call moment with nothing to get the current datetime as a moment object.

Then we call diff with the date string and 'years' to get the years difference.

Conclusion

To get the number of years since a date, not rounded up with Moment.js, we call the diff method.

Categories
JavaScript Answers

How to query MongoDB ObjectId by date with JavaScript?

Sometimes, we want to query MongoDB ObjectId by date with JavaScript.

In this article, we’ll look at how to query MongoDB ObjectId by date with JavaScript.

How to query MongoDB ObjectId by date with JavaScript?

To query MongoDB ObjectId by date with JavaScript, we call the ObjectId.fromDate method.

For instance, we write

db.users.find({
  _id: { $lt: ObjectId.fromDate(new ISODate("2022-01-05T00:00:00.000Z")) },
});

to call find to find the users entries that has object ID less than the given date.

We call ObjectId.fromDate to convert the ISODate to an object ID.

Conclusion

To query MongoDB ObjectId by date with JavaScript, we call the ObjectId.fromDate method.

Categories
JavaScript Answers

How to check for not null with JavaScript?

Sometimes, we want to check for not null with JavaScript.

In this article, we’ll look at how to check for not null with JavaScript.

How to check for not null with JavaScript?

To check for not null with JavaScript, we can use the !== operator.

For instance, we write

if (val !== null) {
  //...
}

to check if val isn’t equal to null with

val !== null

Conclusion

To check for not null with JavaScript, we can use the !== operator.

Categories
JavaScript Answers

How to remove padding or margins from Google Charts and JavaScript?

Sometimes, we want to remove padding or margins from Google Charts and JavaScript.

In this article, we’ll look at how to remove padding or margins from Google Charts and JavaScript.

How to remove padding or margins from Google Charts and JavaScript?

To remove padding or margins from Google Charts and JavaScript, we can set the width and height options.

For instance, we write

const options = {
  title: "How Much I Ate Last Night",
  width: 350,
  height: 400,
  chartArea: { width: "100%", height: "80%" },
  legend: { position: "bottom" },
};

to set the width and height in pixels.

And then we set the chartArea to fill 100% of the width and height to remove the margins and padding.

Conclusion

To remove padding or margins from Google Charts and JavaScript, we can set the width and height options.

Categories
JavaScript Answers

How to modify a query string without reloading the page with JavaScript?

Sometimes, we want to modify a query string without reloading the page with JavaScript.

In this article, we’ll look at how to modify a query string without reloading the page with JavaScript.

How to modify a query string without reloading the page with JavaScript?

To modify a query string without reloading the page with JavaScript, we can use the history.pushState method.

For instance, we write

const newUrl =
  window.location.protocol +
  "//" +
  window.location.host +
  window.location.pathname +
  "?myNewUrlQuery=1";
window.history.pushState({ path: newUrl }, "", newUrl);

to create the newUrl URL string.

Then we call pushState with { path: newUrl } and newUrl to navigate to newUrl without reloading the page.

We get the current URL data from window.location.

We get the protocol with window.location.protocol.

host has the hostname.

pathname has the path before the query string.

Conclusion

To modify a query string without reloading the page with JavaScript, we can use the history.pushState method.