Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the default date of the day in vue js

I would like to set the date of the day by default in my datepicker.

code:

<template lang="html">
  <input type="date" v-model="date">
</template>
<script lang="js">
export default {
name: 'component',
props: [],
mounted() {

},
data() {
  return {
    // date: new Date().toJSON.split('T')[0],
    date: new Date(),
  }
},
methods: {

},
computed: {

},
               }
</script>

it does not work unfortunately, I was told about moment js but I do not know how we use it

like image 415
static Avatar asked Jun 06 '18 12:06

static


People also ask

How do I find my current date Vue?

In every vue app get the current date and time in vuejs is a common matter. It is very simple to get current date time in vue js cause we can get it using Date(). Date() will provide us full date and time with the timezone. so We can also make a better format like yyyy-mm-dd in vue js.

What design pattern does Vue use?

Technically, Vue. js is focused on the ViewModel layer of the MVVM pattern. It connects the View and the Model via two way data bindings.

How to get current date and time in vuejs?

It is very simple in the Vuejs Application If you are already aware of getting a current date in JavaScript. Javascript provides Date object, holds date, time, and timezone information. It provides various methods to provide the current date information. Vuejs always runs on client browsers, Getting date and time in vue is client-side only.

How to build a calendar with vuetify and Vue?

A lightweight Vue.js plugin that allows building calendars. Vuetify has a built-in date picker. If you are using a Vuetify, you don’t need to install any packages, and creating a date picker object is simple as using the v-date-picker component.

Why do you need a date picker in Vue JS?

If you are building a system that involves booking a specific service or some kind of planning or event creation activities, the date picker is a feature you might want to have. When it comes to Vue.js, you don’t need to reinvent the wheel, as there are already packages that let you implement data picker in Vue.js.

How do I add days to a date in Java?

The setDate () method can also be used to add days to a date: If adding days shifts the month or year, the changes are handled automatically by the Date object. The setHours () method sets the hours of a date object (0-23):


2 Answers

v-model on a Date input works with a string in yyyy-MM-dd format. If you're happy to have strings in your model, not date objects, then just put your default date string in the date variable, like this.

date : new Date().toISOString().slice(0,10)

Here's a running example. A name has been changed to avoid having variable names close to reserved keywords !

vm = new Vue({
  el : '#vueRoot',
  data : { myDate : new Date().toISOString().slice(0,10) }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<div  id='vueRoot'>
    <input type='date' v-model='myDate'>
    <br>
    {{myDate}}
</div>

Of course you may want to have dates as date objects in your model, if you want to format or compare them. In that case, you should avoid v-model and code the two sides of your binding separately, like this.

like image 170
bbsimonbb Avatar answered Sep 19 '22 01:09

bbsimonbb


v-model binding

<datepicker v-model="state.date" name="uniquename"></datepicker>

Emits events

<datepicker 
    @selected="doSomethingInParentComponentFunction" 
    @opened="datepickerOpenedFunction"
    @closed="datepickerClosedFunction"
>
</datepicker>
like image 41
Ketan Avatar answered Sep 21 '22 01:09

Ketan