Categories
JavaScript Answers

How to get am pm from the date time string using moment.js and JavaScript?

Sometimes, we want to get am pm from the date time string using moment.js and JavaScript.

In this article, we’ll look at how to get am pm from the date time string using moment.js and JavaScript.

How to get am pm from the date time string using moment.js and JavaScript?

To get am pm from the date time string using moment.js and JavaScript, we use the 'A' format code.

For instance, we write

console.log(
  moment("Mon 03-Jul-2022, 11:00 AM", "ddd DD-MMM-YYYY, hh:mm A").format(
    "hh:mm A"
  )
);

to call format on a moment object with "hh:mm A" to return a time string with the hours and minutes along with am or pm at the end with 'A'.

Conclusion

To get am pm from the date time string using moment.js and JavaScript, we use the 'A' format code.

Categories
JavaScript Answers

How to detect 64-bit or 32-bit Windows from user agent with JavaScript?

Sometimes, we want to detect 64-bit or 32-bit Windows from user agent with JavaScript.

In this article, we’ll look at how to detect 64-bit or 32-bit Windows from user agent with JavaScript.

How to detect 64-bit or 32-bit Windows from user agent with JavaScript?

To detect 64-bit or 32-bit Windows from user agent with JavaScript, we can use the navigator.userAgent string property.

For instance, we write

if (
  navigator.userAgent.includes("WOW64") ||
  navigator.userAgent.includes("Win64")
) {
  console.log("This is a 64 bit OS");
} else {
  console.log("Not a 64 bit OS");
}

to check if navigator.userAgent includes 'WOW64' or the 'Win64' strings.

If it includes either one, then the user agent is the 64 bit Windows user agent.

Conclusion

To detect 64-bit or 32-bit Windows from user agent with JavaScript, we can use the navigator.userAgent string property.

Categories
JavaScript Answers

How to create multi-dimensional associative arrays in JavaScript?

Sometimes, we want to create multi-dimensional associative arrays in JavaScript.

In this article, we’ll look at how to create multi-dimensional associative arrays in JavaScript.

How to create multi-dimensional associative arrays in JavaScript?

To create multi-dimensional associative arrays in JavaScript, we can create a nested object.

For instance, we write

const myObj = {
  fred: { apples: 2, oranges: 4, bananas: 7, melons: 0 },
  mary: { apples: 0, oranges: 10, bananas: 0, melons: 0 },
  sarah: { apples: 0, oranges: 0, bananas: 0, melons: 5 },
};

console.log(myObj["fred"]["apples"]);

to create the myObj object.

In it, we add the fred, mary and sarah properties.

And we set them to objects with the apples, oranges, bananas and melons properties.

Then we get a nested property with myObj["fred"]["apples"].

Conclusion

To create multi-dimensional associative arrays in JavaScript, we can create a nested object.

Categories
JavaScript Answers

How to reload the page with hash value with JavaScript?

Sometimes, we want to reload the page with hash value with JavaScript.

In this article, we’ll look at how to reload the page with hash value with JavaScript.

How to reload the page with hash value with JavaScript?

To reload the page with hash value with JavaScript, we can set the window.location.hash property to the hash string.

Then we reload the page.

For instance, we write

window.location.hash = "#mypara";
location.reload();

to set window.location.hash to the hash string to append it to the end of the URL.

Then we call location.reload to reload the page.

Conclusion

To reload the page with hash value with JavaScript, we can set the window.location.hash property to the hash string.

Categories
JavaScript Answers

How to capture the close event of popup window in JavaScript?

Sometimes, we want to capture the close event of popup window in JavaScript.

In this article, we’ll look at how to capture the close event of popup window in JavaScript.

How to capture the close event of popup window in JavaScript?

To capture the close event of popup window in JavaScript, we can watch for the window to close with a timer.

For instance, we write

const win = window.open("https://www.example.com");

const timer = setInterval(() => {
  if (win.closed) {
    clearInterval(timer);
    console.log("closed");
  }
}, 1000);

to call window.open to open a popup with the URL.

Then we call setInterval with a callback that checks if the popup is closed with win.closed every second.

If it’s true, then we call clearInterval with timer to stop the setInterval timer.

Conclusion

To capture the close event of popup window in JavaScript, we can watch for the window to close with a timer.