In my Django API served with django-rest-framework, I am sending a movie object along with its related information to a Vue app. One piece of information included is the movie duration.
Movie Object:
{
"movie_id": 13,
"duration": "17:52:14",
...
...
},
Component Template:
<div id="movieDetailSynopsis">
...
...
<p>Duration: {{movie.duration}}</p>
</div>
The duration is in the format
HH:MM:SS
eg: 02:22:08
However, I would like it to be displayed as
2h 22m
Is there a way to achieve this formatting in either Django, Vue.js, or JavaScript?
Update:
I attempted to use a global filter
Main.js:
new Vue({
router,
components: {App},
template: '<App/>',
store,
filters: {
durationFormat(value) {
const duration = moment.duration(value);
return duration.hours() + 'h ' + duration.minutes() + 's';
}
}
}).$mount('#app');
Inside the component template:
<div id="movieDetailSynopsis">
...
...
<p>Duration: {{movie.duration | durationFormat}}</p>
</div>
But I encountered an error:
[Vue warn]: Failed to resolve filter: durationFormat (found in anonymous component - use the "name" option for better debugging messages.)