Categories
JavaScript Answers

How to convert JavaScript date to string?

Sometimes, we want to convert JavaScript date to string.

In this article, we’ll look at how to convert JavaScript date to string.

How to convert JavaScript date to string?

To convert JavaScript date to string, we use some date methods.

For instance, we write

const temp = new Date();
const dateStr =
  temp.getFullYear().toString().padStart(2, "0") +
  (1 + temp.getMonth()).toString().padStart(2, "0") +
  temp.getDate().toString().padStart(2, "0") +
  temp.getHours().toString().padStart(2, "0") +
  temp.getMinutes().toString().padStart(2, "0") +
  temp.getSeconds().toString().padStart(2, "0");

to get the 4 digit year with getFullYear.

We get the month with 0 for January to 11 for December with getMonth.

We get the date with getDate.

We get the hours with getHours.

We get the minutes with getMinutes.

And we get the seconds with getSeconds.

We convert each number to a string with toString.

Then we call padStart to prepend '0' to each string until it has length 2.

And then we concatenate them all together.

Conclusion

To convert JavaScript date to string, we use some date methods.

Categories
Vue Answers

How to run code in mounted and again for restart functionality with Vue.js?

Sometimes, we want to run code in mounted and again for restart functionality with Vue.js.

In this article, we’ll look at how to run code in mounted and again for restart functionality with Vue.js.

How to run code in mounted and again for restart functionality with Vue.js?

To run code in mounted and again for restart functionality with Vue.js, we move the code in mounted into its own method.

For instance, we write

new Vue({
  methods: {
    init() {
      //...
    },
  },
  mounted() {
    this.init();
  },
});

to define the init method and call it in mounted.

Then we add

<button v-if="playerWon" @click="init">Play Again</button>

into our HTML code to call init when we click on the button.

Conclusion

To run code in mounted and again for restart functionality with Vue.js, we move the code in mounted into its own method.

Categories
JavaScript Answers

How to set focus to iframe contents with JavaScript?

Sometimes, we want to set focus to iframe contents with JavaScript.

In this article, we’ll look at how to set focus to iframe contents with JavaScript.

How to set focus to iframe contents with JavaScript?

To set focus to iframe contents with JavaScript, we call the iframe’s body’s focus method.

For instance, we write

document
  .getElementsByName("iframe_name")[0]
  .contentWindow.document.body.focus();

to get the iframe with getElementsByName.

Then we get its body element with contentWindow.document.body.

And then we call focus to focus on it.

Conclusion

To set focus to iframe contents with JavaScript, we call the iframe’s body’s focus method.

Categories
React Native Answers

How to add transparent overlay in React Native and JavaScript?

Sometimes, we want to add transparent overlay in React Native and JavaScript.

In this article, we’ll look at how to add transparent overlay in React Native and JavaScript.

How to add transparent overlay in React Native and JavaScript?

To add transparent overlay in React Native and JavaScript, we add transparent styles for the View.

For instance, we write

import { View, StyleSheet } from "react-native";

const styles = StyleSheet.create({
  overlaycontainer: {
    ...StyleSheet.absoluteFillObject,
    backgroundColor: "#000",
    opacity: 0.8,
    justifyContent: "center",
    alignItems: "center",
  },
});

//...

<View style={styles.overlaycontainer}>
  <Text style={{ color: "#fff" }}>Locating directions please wait ...</Text>
</View>

to add styles with the opacity value with StyleSheet.create.

Then we use the styles.overlaycontainer styles for the View.

Conclusion

To add transparent overlay in React Native and JavaScript, we add transparent styles for the View.

Categories
JavaScript Answers

How to add labels or legends for all chart types in Chart.js and JavaScript?

Sometimes, we want to add labels or legends for all chart types in Chart.js and JavaScript.

In this article, we’ll look at how to add labels or legends for all chart types in Chart.js and JavaScript.

How to add labels or legends for all chart types in Chart.js and JavaScript?

To add labels or legends for all chart types in Chart.js and JavaScript, we use the chart’s generateLegend method.

For instance, we write

<div>
  <canvas id="chartDiv" height="400" width="600"></canvas>
  <div id="legendDiv"></div>
</div>

to add a canvas for the chart and a div for the legend.

Then we write

const data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [
    {
      label: "Speed 1",
      fillColor: "rgba(220,220,220,0.2)",
      strokeColor: "rgba(220,220,220,1)",
      pointColor: "rgba(220,220,220,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(220,220,220,1)",
      data: [65, 59, 56, 24, 56, 55, 6],
    },
    {
      label: "Speed 2",
      fillColor: "rgba(151,187,205,0.2)",
      strokeColor: "rgba(151,187,205,1)",
      pointColor: "rgba(151,187,205,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(151,187,205,1)",
      data: [28, 1, 54, 76, 24, 12, 6],
    },
  ],
};

const myLineChart = new Chart(
  document.getElementById("chartDiv").getContext("2d")
).Line(data);
document.getElementById("legendDiv").innerHTML = myLineChart.generateLegend();

to create a chart with the canvas and the Chart constructor.

We call Line to create a line chart with the data.

Then we call myLineChart.generateLegend to return an HTML string with the legend’s content and set it as the innerHTML value of the div to show it.

Conclusion

To add labels or legends for all chart types in Chart.js and JavaScript, we use the chart’s generateLegend method.