Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

javascript - How to v-model a input of type date

I have an input like so:

<input class="input" type="date" v-model="dob" />

dob is initialized as null, and works properly. For example 03/05/2006 models to 2006-03-05T00:00:00.000+00:0. However when dob is inititialized as 2006-03-05T00:00:00.000+00:0, it doesn't show in the date input. How would I fix this.

question from:https://stackoverflow.com/questions/66049355/how-to-v-model-a-input-of-type-date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT: you could use to set a default value and have it changing afterwards.

data() {
  return {
    initialDate: Date.now(),
  };
},
computed: {
  dob: {
    get() {
      return this.initialDate;
    },
    set(newValue) {
      this.initialDate = newValue;
    },
  },
},

Where is your input coming from ? It probably needs a UNIX timestamp like 1612454883231. Try initializing it with Date.now() or maybe try new Date().toISOString().slice(0,10).
Also, new Date('2006-03-05T00:00:00') if you want to set it manually (and since it's a date of birth...).

If it doesn't work, you need to dig the documentation to see which format it accepts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...