1

I have written few lines of code using javascript in Vue framework. I can not display date on html from var. I used vue-bootstrap for styles. Any suggestion is app?

<template>
    <div class="app">
        <b-form-datepicker id="datepicker" class="weekpicker" placeholder="Select a week" local="en"></b-form-datepicker>
    <div class="w-75 p-3 mb-1 text-light">
        <b-form-select class="weekpicker" onfocus="myFunction()">
            <b-form-select-option  hidden value="">Select a week</b-form-select-option>
            <b-form-select-option  id="mydate"  value="">{{ myFunction() }}</b-form-select-option>
            <b-form-select-option  type="date"  value=""></b-form-select-option>
            <b-form-select-option  type="date"  value=""></b-form-select-option>
        </b-form-select>

    </div>
        <button><span>Submit</span></button>
    </div>
</template>


<script>


export default {
  name: 'TS_home',
  data() {
    return {
    };
  },
    methods: {
      myFunction() {
          let x = new Date();
          let current_date = x.toDateString();
          x.setDate(x.getDate() + 7);
          let seventh_date = x.toDateString()
          document.getElementById("mydate").innerHTML = current_date + " - " + seventh_date;
      }
    }
};
</script>
3
  • 1
    Don't manipulate the DOM with JS. Use the tools that Vue provides. Set a data property (eg mydate) and display it in your template using {{ mydate }} Commented Jun 4, 2020 at 0:36
  • You should also be using a Vue event handler, ie @focus="myFunction" Commented Jun 4, 2020 at 0:38
  • When i use @focus placeholder is not showing up. Commented Jun 4, 2020 at 1:23

2 Answers 2

1

As @Phil said since you are using Vue you should define the data property for the date like so:

data() {
  return {
    myDate: null
  }
}

When it comes to date pickers it is usually @change event or v-model.

Try:

<b-form-datepicker id="datepicker" class="weekpicker" placeholder="Select a week" local="en" @change="myDate=$event"></b-form-datepicker>

Then display the property in your HTML template like:

{{myDate}}

Another possibility is to use vue-bootstrap if that is not what you have installed already. You would do it like this:

npm install vue-bootstrap-datetimepicker --save

then in your component

  // Import required dependencies 
  import 'bootstrap/dist/css/bootstrap.css';

  // Import this component
  import datePicker from 'vue-bootstrap-datetimepicker';

then in your data

data() {
  return {
    myDate: new Date(),
    options: {
      format: 'DD/MM/YYYY',
      useCurrent: false,
    }
  }
}

and in your HTML template

<date-picker v-model="myDate" :config="options"></date-picker>

and to display that date you would again use:

{{myDate}}

Please refer to the documentation for more detailed information.

I hope it helps. Good luck.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use the data property instead of manipulating the dom

data() {
  return {
    myDate: ''
  }
}

Inside the function

myFunction() {
          let x = new Date();
          let current_date = x.toDateString();
          x.setDate(x.getDate() + 7);
          let seventh_date = x.toDateString()
          this.myDate = current_date + " - " + seventh_date;
      }

In the html, use the vue-event handler

<b-form-select class="weekpicker" @focus="myFunction()">
<b-form-select-option  id="mydate"  value="">{{ myDate }}</b-form-select-option>

Hope this will help :)

1 Comment

Thanks for reply, Nope it is not displaying the date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.