Categories
JavaScript Answers

How to convert milliseconds to minutes and seconds with JavaScript?

Sometimes, we want to convert milliseconds to minutes and seconds with JavaScript.

In this article, we’ll look at how to convert milliseconds to minutes and seconds with JavaScript.

How to convert milliseconds to minutes and seconds with JavaScript?

To convert milliseconds to minutes and seconds with JavaScript, we can use some date methods.

For instance, we write

const d = new Date(Date.UTC(0, 0, 0, 0, 0, 0, 298999));

const parts = [d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds()];
const formatted = parts.map((s) => String(s).padStart(2, "0")).join(":");

to create a UTC date with the Date.UTC method and the Date constructor.

Then we put the hours, minutes, and seconds of the d date into the parts array.

getUTCHours gets the hours in UTC.

getUTCMinutes gets the minutes in UTC.

getUTCSeconds gets the seconds in UTC.

Then we prepend a 0 to each number if it has less than 2 digits with padStart after converting them to strings with String.

And we join the strings together with join.

Conclusion

To convert milliseconds to minutes and seconds with JavaScript, we can use some date methods.

Categories
JavaScript Answers

How to do a greater than or equal to with moment.js in JavaScript?

Sometimes, we want to do a greater than or equal to with moment.js in JavaScript.

In this article, we’ll look at how to do a greater than or equal to with moment.js in JavaScript.

How to do a greater than or equal to with moment.js in JavaScript?

To do a greater than or equal to with moment.js in JavaScript, we can use the isSameOrAfter method.

For instance, we write

moment1.isSameOrAfter(moment2);

to check if the datetime in the moment1 is the same or after the datetime in the moment2 moment object.

Conclusion

To do a greater than or equal to with moment.js in JavaScript, we can use the isSameOrAfter method.

Categories
JavaScript Answers

How to get the position of an element with React Native?

Sometimes, we want to get the position of an element with React Native.

In this article, we’ll look at how to get the position of an element with React Native.

How to get the position of an element with React Native?

To get the position of an element with React Native, we can get it from the View‘s onLayout callback.

For instance, we write

<View
  onLayout={(event) => {
    const layout = event.nativeEvent.layout;
    console.log("height:", layout.height);
    console.log("width:", layout.width);
    console.log("x:", layout.x);
    console.log("y:", layout.y);
  }}
></View>

to add a View with the onLayout prop set to a function that gets the position values from the event.nativeEvent.layout property.

We then get the height, width, x, and y values from the layout object.

Conclusion

To get the position of an element with React Native, we can get it from the View‘s onLayout callback.

Categories
JavaScript Answers

How to add and remove multiple CSS classes to an element with JavaScript?

Sometimes, we want to add and remove multiple CSS classes to an element with JavaScript.

In this article, we’ll look at how to add and remove multiple CSS classes to an element with JavaScript.

How to add and remove multiple CSS classes to an element with JavaScript?

To add and remove multiple CSS classes to an element with JavaScript, we can use the classList.add and classList.remove methods.

For instance, we write

buttonTop.classList.add("myButton", "myStyle");

to call classList.add on the buttonTop element to add the myButton and myStyle classes.

And we write

buttonTop.classList.remove("myElement", "myButton", "myStyle");

to call classList.add on the buttonTop element to remove the myElement, myButton and myStyle classes.

Conclusion

To add and remove multiple CSS classes to an element with JavaScript, we can use the classList.add and classList.remove methods.

Categories
JavaScript Answers

How to remove empty strings from array while keeping record without a loop with JavaScript?

Sometimes, we want to remove empty strings from array while keeping record without a loop with JavaScript.

In this article, we’ll look at how to remove empty strings from array while keeping record without a loop with JavaScript.

How to remove empty strings from array while keeping record without a loop with JavaScript?

To remove empty strings from array while keeping record without a loop with JavaScript, we use the array filter method.

For instance, we write

const arr = ["I", "am", "", "still", "here", "", "man"];
const filteredArr = arr.filter(Boolean);

to call arr.filter with Boolean to return an array with the falsy values filtered out.

Since empty strings are falsy, they won’t be included in the returned array.

Conclusion

To remove empty strings from array while keeping record without a loop with JavaScript, we use the array filter method.