Categories
JavaScript Answers

How to get seconds since epoch in JavaScript?

Sometimes, we want to get seconds since epoch in JavaScript.

In this article, we’ll look at how to get seconds since epoch in JavaScript.

How to get seconds since epoch in JavaScript?

To get seconds since epoch in JavaScript, we get the timestamp of the datetime in milliseconds with getTime.

And then we divide that by 1000 to get the number of seconds since epoch.

For instance, we write

const d = new Date();
const seconds = d.getTime() / 1000;

to create the date d.

And then we call d.getTime to return the timestamp in milliseconds.

Then we divide that by 1000 to get the number of seconds since epoch.

Conclusion

To get seconds since epoch in JavaScript, we get the timestamp of the datetime in milliseconds with getTime.

And then we divide that by 1000 to get the number of seconds since epoch.

Categories
JavaScript Answers

How to subtract minutes from a date in JavaScript?

Sometimes, we want to subtract minutes from a date in JavaScript.

In this article, we’ll look at how to subtract minutes from a date in JavaScript.

How to subtract minutes from a date in JavaScript?

To subtract minutes from a date in JavaScript, we can subtract the timestamp in milliseconds from the current date.

For instance, we write

const MS_PER_MINUTE = 60000;
const myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE);

to subtract the myEndDateTime timestamp in milliseconds by the durationInMinutes * MS_PER_MINUTE milliseconds, which is the durationInMinutes converted to milliseconds.

Then we pass that into the Date constructor to create a new date object.

Conclusion

To subtract minutes from a date in JavaScript, we can subtract the timestamp in milliseconds from the current date.

Categories
JavaScript Answers

How to append to an object with JavaScript?

Sometimes, we want to append to an object with JavaScript.

in this article, we’ll look at how to append to an object with JavaScript.

How to append to an object with JavaScript?

To append to an object with JavaScript, we can use the spread operator.

For instance, we write

let alerts = {
  1: { app: "helloworld", message: "message" },
  2: { app: "helloagain", message: "another message" },
};

alerts = {
  ...alerts,
  alertNo: 2,
};

to spread the existing properties of alerts into a new object with ....

And then we add the alertNo property to it.

Then we assign the new object back to alerts.

Conclusion

To append to an object with JavaScript, we can use the spread operator.

Categories
JavaScript Answers

How to traverse all the nodes of a JSON object tree with JavaScript?

Sometimes, we want to traverse all the nodes of a JSON object tree with JavaScript.

In this article, we’ll look at how to traverse all the nodes of a JSON object tree with JavaScript.

How to traverse all the nodes of a JSON object tree with JavaScript?

To traverse all the nodes of a JSON object tree with JavaScript, we can use the Object.entries method.

For instance, we write

const traverse = (jsonObj) => {
  if (jsonObj !== null && typeof jsonObj == "object") {
    Object.entries(jsonObj).forEach(([key, value]) => {
      traverse(value);
    });
  } else {
    console.log(jsonObj);
  }
};

to create the traverse function.

In it, we loop through the key-value pairs with the Object.entries method.

If jsonObj is an object, then we call traverse with its value in the forEach callback to traverse the level below the current level.

Otherwise, we log the jsonObj value.

Conclusion

To traverse all the nodes of a JSON object tree with JavaScript, we can use the Object.entries method.

Categories
JavaScript Answers

How to mock the imports of an ES6 module with Jest and JavaScript?

Sometimes, we want to mock the imports of an ES6 module with Jest and JavaScript.

In this article, we’ll look at how to mock the imports of an ES6 module with Jest and JavaScript.

How to mock the imports of an ES6 module with Jest and JavaScript?

To mock the imports of an ES6 module with Jest and JavaScript, we can call jest.mock.

For instance, we write

import myfunc from "./mymod";
import shortid from "shortid";

jest.mock("shortid");

describe("mocks shortid", () => {
  it("works", () => {
    shortid.mockImplementation(() => 1);
    expect(myfunc()).toEqual(1);
  });
});

to import the real shortid module with

import shortid from "shortid";

Then we use

jest.mock("shortid");

to mock the shortid module.

And then we call shortid.mockImplementation with a callback to mock the return value of the shortid function.

Conclusion

To mock the imports of an ES6 module with Jest and JavaScript, we can call jest.mock.