Categories
JavaScript Answers

How to convert 12-hour hh:mm AM/PM time to 24-hour hh:mm time with JavaScript?

Sometimes, we want to convert 12-hour hh:mm AM/PM time to 24-hour hh:mm time with JavaScript

In this article, we’ll look at how to convert 12-hour hh:mm AM/PM time to 24-hour hh:mm time with JavaScript.

How to convert 12-hour hh:mm AM/PM time to 24-hour hh:mm time with JavaScript?

To convert 12-hour hh:mm AM/PM time to 24-hour hh:mm time with JavaScript, we can manipulate the time strings.

For instance, we write

const convertTime12to24 = (time12h) => {
  const [time, modifier] = time12h.split(" ");
  let [hours, minutes] = time.split(":");

  if (hours === "12") {
    hours = "00";
  }

  if (modifier === "PM") {
    hours = parseInt(hours, 10) + 12;
  }

  return `${hours}:${minutes}`;
};

console.log(convertTime12to24("01:02 PM"));

to define the convertTime12to24 function.

It takes the time12h string which has the time in 12 hour format.

In it, we split the time12h string into the time and modifier with split.

Then we split the time into hours and minutes with split.

Next, we check if hours is '12'.

If it is, then we set it to '00'.

And if modifier is 'PM', we add 12 to the hour.

Finally we return the 24 hour format time string.

Conclusion

To convert 12-hour hh:mm AM/PM time to 24-hour hh:mm time with JavaScript, we can manipulate the time strings.

Categories
JavaScript Answers

How to format an integer to a specific length in JavaScript?

Sometimes, we want to format an integer to a specific length in JavaScript.

In this article, we’ll look at how to format an integer to a specific length in JavaScript.

How to format an integer to a specific length in JavaScript?

To format an integer to a specific length in JavaScript, we use the string padStart method.

For instance, we write

const formatted = String(num).padStart(4, "0");

to convert num into a string with String.

Then we call padStart with 4 and '0' to prepend 0’s to the string until it has length 4.

Conclusion

To format an integer to a specific length in JavaScript, we use the string padStart method.

Categories
Vue Answers

How to customize item-text in Vuetify v-select with Vue.js?

Sometimes, we want to customize item-text in Vuetify v-select with Vue.js.

In this article, we’ll look at how to customize item-text in Vuetify v-select with Vue.js.

How to customize item-text in Vuetify v-select with Vue.js?

To customize item-text in Vuetify v-select with Vue.js, we fill in the selection and item slot.

For instance, we write

<template>
  <v-select
    label="Names"
    v-model="name"
    :items="names"
    item-value="id"
    item-text="name"
    return-object
  >
    <template v-slot:selection="{ item }">
      {{ getText(item) }}
    </template>
    <template v-slot:item="{ item }">
      {{ getText(item) }}
    </template>
  </v-select>
</template>

<script>
export default {
  data: () => ({
    names: [
      { id: 1, name: "Paul", age: 23 },
      { id: 2, name: "Marcelo", age: 15 },
      { id: 3, name: "Any", age: 30 },
    ],
    name: null,
  }),
  methods: {
    getText: (item) => `${item.name} - ${item.age}`,
  },
};
</script>

to fill in the selection slot to customize the text for the selection.

And we fill in the slot:item slot to customize the text for the drop down options.

item has the entry in names array for the selection slot.

And item has the entry in names array being rendered in the drop down.

Conclusion

To customize item-text in Vuetify v-select with Vue.js, we fill in the selection and item slot.

Categories
React Native Answers

How to overlay ActivityIndicator in React Native?

Sometimes, we want to overlay ActivityIndicator in React Native.

In this article, we’ll look at how to overlay ActivityIndicator in React Native.

How to overlay ActivityIndicator in React Native?

To overlay ActivityIndicator in React Native, we can put it in a modal.

For instance, we write

<Modal
  transparent={true}
  animationType={"none"}
  visible={loading}
  onRequestClose={() => {
    console.log("close modal");
  }}
>
  <View style={styles.modalBackground}>
    <View style={styles.activityIndicatorWrapper}>
      <ActivityIndicator animating={loading} />
    </View>
  </View>
</Modal>

to put the ActivityIndicator in a Modal.

We have transparent set to true to make the modal transparent so we can see the contents underneath it.

And we set the animating to the loading state to make it animate only when loading is true.

Also, we set visible to loading to show the modal only when loading is true.

Conclusion

To overlay ActivityIndicator in React Native, we can put it in a modal.

Categories
JavaScript Answers

How to pass props to the Redirect component with React Router?

Sometimes, we want to pass props to the Redirect component with React Router.

In this article, we’ll look at how to pass props to the Redirect component with React Router.

How to pass props to the Redirect component with React Router?

To pass props to the Redirect component with React Router, we put it in the object we set as the value of the to prop.

For instance, we write

<Redirect
  to={{
    pathname: "/order",
    state: { id: "123" },
  }}
/>;

to set the to prop to an object with the state property set to an object with the props.

Then we can get the value of id in the destination route component with

this.props.location.state.id

Conclusion

To pass props to the Redirect component with React Router, we put it in the object we set as the value of the to prop.