Skip to content

Instantly share code, notes, and snippets.

@paulredmond
Last active September 26, 2020 15:03
Show Gist options
  • Save paulredmond/871f8eba7ae5d1eb3bc020dd9391ce17 to your computer and use it in GitHub Desktop.
Save paulredmond/871f8eba7ae5d1eb3bc020dd9391ce17 to your computer and use it in GitHub Desktop.
Example Vue.js mixins for time
import dateMixin from './mixins/date';
// Globally
Vue.mixin(dateMixin);
import moment from 'moment';
export default {
filters: {
toISOString(str) {
return moment(str).toISOString();
},
formatDate(str, format = null, outputFormat = 'YYYY-MM-DD HH:mm:ss') {
if (format == null) {
return moment(str).format(outputFormat);
}
return moment(str, format).format(outputFormat);
},
diffForHumans(str) {
return moment(str).from(moment());
},
},
};
<template>
<div>
<p>{{ myDate | toISOString }}</p>
<p>{{ myDate | formatDate(null, 'YYYY-MM-DD') }}
<p>{{ myDate | diffForHumans }}</p>
</div>
</template>
<script>
// Per-component example
import dateMixin from '../mixins/date';
export default {
mixins: [dateMixin]
}
</script>
@papalardo
Copy link

To create filters, you dont need make a mixin.
Just use this

Vue.filter('capitalize', function (value) {
  if (!value) return ''
  value = value.toString()
  return value.charAt(0).toUpperCase() + value.slice(1)
})

https://br.vuejs.org/v2/guide/filters.html

But this help me in anothers problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment