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
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.

Categories
JavaScript Answers

How to add items to an object through the .push() method with JavaScript?

Sometimes, we want to add items to an object through the .push() method with JavaScript.

In this article, we’ll look at how to add items to an object through the .push() method with JavaScript.

How to add items to an object through the .push() method with JavaScript?

To add items to an object through the .push() method with JavaScript, we should make sure push is called on array.

For instance, we write

const stuff = [];
stuff.push(element);

to call stuff.push with element to append element as the last element of the stuff` array.

Conclusion

To add items to an object through the .push() method with JavaScript, we should make sure push is called on array.

Categories
JavaScript Answers

How to change label color with Chart.js and JavaScript?

Sometimes, we want to change label color with Chart.js and JavaScript.

In this article, we’ll look at how to change label color with Chart.js and JavaScript.

How to change label color with Chart.js and JavaScript?

To change label color with Chart.js and JavaScript, we set the fontColor.

For instance, we write

const ctx = document.getElementById("barChart");
const myChart = new Chart(ctx, {
  type: "bar",
  data: {
    labels: ["label 1", "label 2"],
    datasets: [
      {
        label: "My Label",
        backgroundColor: "rgba(159,170,174,0.8)",
        borderWidth: 1,
        hoverBackgroundColor: "rgba(232,105,90,0.8)",
        hoverBorderColor: "orange",
        scaleStepWidth: 1,
        data: [4, 5],
      },
    ],
  },
  options: {
    legend: {
      labels: {
        fontColor: "blue",
        fontSize: 18,
      },
    },
    scales: {
      yAxes: [
        {
          ticks: {
            fontColor: "green",
            fontSize: 18,
            stepSize: 1,
            beginAtZero: true,
          },
        },
      ],
      xAxes: [
        {
          ticks: {
            fontColor: "purple",
            fontSize: 14,
            stepSize: 1,
            beginAtZero: true,
          },
        },
      ],
    },
  },
});

to set the fontColor of the labels by setting the fontColor in the options object property.

legend.labels has the options for the legend labels.

ticks has the styles for the ticks.

Conclusion

To change label color with Chart.js and JavaScript, we set the fontColor.