Vue2-datepicker: Custom format function

Created on 10 Apr 2018  路  14Comments  路  Source: mengxiong10/vue2-datepicker

Sometimes you need more control when formatting the Dates.

<date-picker v-model="time" :format="customFormatter"></date-picker>
methods: {
  customFormatter(date) {
    return moment(date).format('MMMM Do YYYY, h:mm:ss a');
  }
}

This functionality will be supported somehow? are PRs welcome?

Most helpful comment

since v3.7.0. Pass to formatter an object.

<date-picker v-model="value" :formatter="myFormat" />

  data () {
    return {
      myFormat: {
       // Date to String
        stringify: (date) => {
          return date ? moment(date).format('LL') : null
        },
        // String to Date
        parse: (value) => {
          return value ? moment(value, 'LL').toDate() : null
        }
      }
    }
  }


Old version:
V1.9.5 Added.
Since this prop 'format' affects the input function , I added the prop 'custom-formatter'.

<date-picker v-model="time" :custom-formatter="customFormatter"></date-picker>
methods: {
  customFormatter(date) {
    return moment(date).format('MMMM Do YYYY, h:mm:ss a');
  }
}

All 14 comments

Did you find another solution for formatting the output date?

No, I didn't. I checked the source code, this needs a PR.

Personally I'm using other solution like vuejs-datepicker, in the meantime

Agreed. I ended up using @change and calling a method to do the formatting. There may be a cleaner way that doesn't require passing the variable name.

<date-picker v-model="endDate" @change="dateChange('endDate')"></date-picker>
methods: {
    dateChange(variable) {
        this[variable] = moment(this[variable]).format('MM-DD-YYYY');
    }
}

PRs welcome

since v3.7.0. Pass to formatter an object.

<date-picker v-model="value" :formatter="myFormat" />

  data () {
    return {
      myFormat: {
       // Date to String
        stringify: (date) => {
          return date ? moment(date).format('LL') : null
        },
        // String to Date
        parse: (value) => {
          return value ? moment(value, 'LL').toDate() : null
        }
      }
    }
  }


Old version:
V1.9.5 Added.
Since this prop 'format' affects the input function , I added the prop 'custom-formatter'.

<date-picker v-model="time" :custom-formatter="customFormatter"></date-picker>
methods: {
  customFormatter(date) {
    return moment(date).format('MMMM Do YYYY, h:mm:ss a');
  }
}

Is this assigning the customFormatter() return value to the model?

return a String to dispaly

hello there,
I'm new to vue, need to bind a date object to this so that it sends to server as ISO date string-YYYY-MM-DD, but display format in local culture. could you please suggest the best way to achieve this? i tried to use the custom-formatter, doesn't seem to work.
thanks,

@lan8086 The default bind value is a Date Object. You can set format (YYYY/MM/DD) to format the display. You can easy to set local format in v3.0.0 soon.

@mengxiong10 Is there currently a way to provide a custom format function since the custom-formatter prop was removed in v2?

since v2.9.0. Pass to format an object.

<date-picker v-model="value" :format="myFormat" />

  data () {
    return {
      myFormat: {
       // Date to String
        stringify: (date) => {
          return date ? moment(date).format('LL') : null
        },
        // String to Date
        parse: (value) => {
          return value ? moment(value, 'LL').toDate() : null
        }
      }
    }
  }

@mengxiong10 thanks!

V1.9.5 Added.
Since this prop 'format' affects the input function , I added the prop 'custom-formatter'.

<date-picker v-model="time" :custom-formatter="customFormatter"></date-picker>
methods: {
  customFormatter(date) {
    return moment(date).format('MMMM Do YYYY, h:mm:ss a');
  }
}

I
try using version 3.8.2 this props not working

@hikayatz the latest version is formatter

<date-picker v-model="value" :formatter="myFormat" />

  data () {
    return {
      myFormat: {
       // Date to String
        stringify: (date) => {
          return date ? moment(date).format(''MMMM Do YYYY, h:mm:ss a'') : null
        },
        // String to Date
        parse: (value) => {
          return value ? moment(value, ''MMMM Do YYYY, h:mm:ss a'').toDate() : null
        }
      }
    }
  }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Raiondesu picture Raiondesu  路  4Comments

4spen picture 4spen  路  4Comments

MaxDiMart picture MaxDiMart  路  5Comments

BuiHuyCuong picture BuiHuyCuong  路  5Comments

lequy1010 picture lequy1010  路  5Comments