Categories
JavaScript Answers

How to get the difference between two Dates in JavaScript?

Sometimes, we want to get the difference between two Dates in JavaScript.

In this article, we’ll look at how to get the difference between two Dates in JavaScript.

How to get the difference between two Dates in JavaScript?

To get the difference between two Dates in JavaScript, we can subtract 2 dates directly.

For instance, we write

const msMinute = 60 * 1000;
const msDay = 60 * 60 * 24 * 1000;
const a = new Date(2021, 2, 12, 23, 59, 59);
const b = new Date("2022 march 12");

console.log(Math.floor((b - a) / msDay), " full days between");
console.log(Math.floor(((b - a) % msDay) / msMinute), " full minutes between");

to subtract b from a to get the difference of their timestamps in milliseconds.

Then we get the floor of the quotient of their difference divided by msDay to get the days difference.

And then we get the floor of the quotient of (b - a) % msDay) divided by msMinute to get the minute difference between b and a‘s times.

Conclusion

To get the difference between two Dates in JavaScript, we can subtract 2 dates directly.

Categories
JavaScript Answers

How to determine the current line number in JavaScript?

Sometimes, we want to determine the current line number in JavaScript.

In this article, we’ll look at how to determine the current line number in JavaScript.

How to determine the current line number in JavaScript?

To determine the current line number in JavaScript, we can get the lineNumber property from an Error object.

For instance, we write

const thisLine = new Error().lineNumber;

to create a new Error object and get the lineNumber property from it to get the line number of the current line.

Conclusion

To determine the current line number in JavaScript, we can get the lineNumber property from an Error object.

Categories
JavaScript Answers

How to convert truthy or falsy to an explicit boolean with JavaScript?

Sometimes, we want to convert truthy or falsy to an explicit boolean with JavaScript.

In this article, we’ll look at how to convert truthy or falsy to an explicit boolean with JavaScript.

How to convert truthy or falsy to an explicit boolean with JavaScript?

To convert truthy or falsy to an explicit boolean with JavaScript, we use the Boolean function.

For instance, we write

const tata = Boolean(toto);

to convert the toto value to a boolean by calling Boolean to return the boolean value of toto.

If toto is truthy, Boolean returns true.

Otherwise, it returns false.

Conclusion

To convert truthy or falsy to an explicit boolean with JavaScript, we use the Boolean function.

Categories
JavaScript Answers

How to auto-scroll to end of div when data is added with JavaScript?

Sometimes, we want to auto-scroll to end of div when data is added with JavaScript.

In this article, we’ll look at how to auto-scroll to end of div when data is added with JavaScript.

How to auto-scroll to end of div when data is added with JavaScript?

To auto-scroll to end of div when data is added with JavaScript, we can set the scrollTop of the div to the scrollHeight of the div.

For instance, we write

window.setInterval(() => {
  const elem = document.getElementById("data");
  elem.scrollTop = elem.scrollHeight;
}, 5000);

to call setInterval to run a function that scrolls the div with ID data to the bottom.

We get the div with getElementById.

And we set elem.scrollTop to elem.scrollHeight to scroll the div to the bottom.

Conclusion

To auto-scroll to end of div when data is added with JavaScript, we can set the scrollTop of the div to the scrollHeight of the div.

Categories
JavaScript Answers

How to use things other than arrays in nested JSON objects?

Sometimes, we want to use things other than arrays in nested JSON objects.

In this article, we’ll look at how to use things other than arrays in nested JSON objects.

How to use things other than arrays in nested JSON objects?

To use things other than arrays in nested JSON objects, we can use objects and primitive values.

For instance, we write

{
  "data": [
    {
      "stuff": [
        {
          "onetype": [
            { "id": 1, "name": "John Doe" },
            { "id": 2, "name": "Don Joeh" }
          ]
        },
        { "othertype": [{ "id": 2, "company": "ACME" }] }
      ]
    },
    {
      "otherstuff": [
        {
          "thing": [
            [1, 42],
            [2, 2]
          ]
        }
      ]
    }
  ]
}

to create JSON objects that has arrays, objects, and primitive values.

Conclusion

To use things other than arrays in nested JSON objects, we can use objects and primitive values.