最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue js datepicker, getting only formatted date portion - Stack Overflow

programmeradmin5浏览0评论

I have a working instance of Vue Datepicker, which is functional to the point of picking a date and logging it on select within the console.

The problem is that it logs as Fri Oct 18 2019 15:01:00 GMT-0400 but I need to send the formatted date portion of this like 2019-10-18 only.

This is vuejs-datepicker library and I can't seem to get anything to work with this:

customFormatter(date) {
  return moment(date).format('MMMM Do YYYY, h:mm:ss a');
}

What exactly am I doing wrong here?

<datepicker :value="date" @selected="CallDateFunction"></datepicker>

date(){
  return {
    date: '',
    ...

CallDateFunction(date){
  console.log(date);
}

I have a working instance of Vue Datepicker, which is functional to the point of picking a date and logging it on select within the console.

The problem is that it logs as Fri Oct 18 2019 15:01:00 GMT-0400 but I need to send the formatted date portion of this like 2019-10-18 only.

This is vuejs-datepicker library and I can't seem to get anything to work with this:

customFormatter(date) {
  return moment(date).format('MMMM Do YYYY, h:mm:ss a');
}

What exactly am I doing wrong here?

<datepicker :value="date" @selected="CallDateFunction"></datepicker>

date(){
  return {
    date: '',
    ...

CallDateFunction(date){
  console.log(date);
}
Share Improve this question asked Oct 14, 2019 at 19:06 Geoff_SGeoff_S 5,1057 gold badges58 silver badges168 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

vuejs-datepicker's selected callback is called with either a date object or null.

You can use the following example code to get a string representation of the date only:

CallDateFunction(date){
  if (date) {
    const dateString = date.toISOString().substring(0, 10);
    console.log(dateString);
  } else {
    console.log('null date');
  }
}

The VueDatePicker have a Props to disable TimePicker that is true by default. As :enableTimePicker="false"

Solution:

    <Datepicker v-model="date" :enableTimePicker="false"></Datepicker>

Source: https://vue3datepicker./api/props/#enabletimepicker

You can format your date with Date.prototype.toISOString() method and set it to your data:

callDateFunction(rawDate){
  if (rawDate)
    this.formattedDate = rawDate.toISOString().split('T')[0]
}

See also: https://developer.mozilla/de/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

You can use the javascript function

jsref_toisostring

.

Documentation

Its pretty forward:

var d = new Date();
var n = d.toISOString();
发布评论

评论列表(0)

  1. 暂无评论