Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

finchy70's avatar

Displaying time from database query in Vue without the seconds.

I pass data from an eloquent query to Vue using props. I want then to display the time data in this collection as 00:00 instead of 00:00:00.

I have tried moment.js with no luck.

How can I do this.

The data is stored in item.start and item.finish in the vue instance.

<template>
    <div>
        <div v-for="item in todays_hours">
            <div class="row">
                <div class="col-2">
                    <div hidden="" ></div>
                </div>
                <div class="col-2" >
                    <div v-text="{{ item.start }}"></div>
                </div>
                <div class="col-2" >
                    <div>{{ item.finish }}</div>
                </div>
                <div class="col-2" >
                    <div v-text="item.job_number"></div>
                </div>
                <div class="col-2" >
                    <div v-text="(item.climbing)?'Yes':'No'"></div>
                </div>
                <div class="col-2" >
                    <button @click="onEdit" class="btn-warning btn-sm mb-1">Edit</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'DisplayHoursComponent',

        props: ['dayCheck', 'hoursWorked'],

        data() {
            return {
                hours_list: this.hoursWorked,
                todays_hours: []
            }
        },

        mounted() {
            for (var i = 0; i < this.hours_list.length; i++) {
                if (this.hours_list[i].day === this.dayCheck) {
                    this.todays_hours.push(this.hours_list[i])
                }
            }

            Event.$on('onAddedEntry', (data) => {
                if(data.day === this.dayCheck){
                    this.todays_hours.push(data);
                }
            })
        },

        methods: {
            onEdit()
            {

            }
        }
    }
</script>

<style scoped>
0 likes
8 replies
MaverickChan's avatar

suggest you use Moment.js, just import moment to your vue component

finchy70's avatar

As mentioned I have already tried moment. Could not get it to work. It was outputing Invalid Date.

MaverickChan's avatar

tried Moment.js ?how ?please show me your code with moment

Cronix's avatar
Cronix
Best Answer
Level 67

Just use a real date when using moment. Since you're only working with the time portion, the actual date is irrelevant.

let time = '12:31:51';
let formatted = moment("2018-03-01 " + time).format('hh:mm');
console.log(formatted); // 12:31
1 like
finchy70's avatar

I passed item.start to timeFormat like this

<div>{{timeFormat(item.finish)}}}</div>

Here is the computed property

computed: {
            timeFormat(value){
                let time = moment("2018-03-01 " + value).format('hh:mm');
                console.log('Time=', time)
            return time;
            }
        },

I have imported moment into the component. The console shows Time = Invalid Time

Also get this error.

app.js:2361 TypeError: _vm.timeFormat is not a function
    at app.js:48654
    at Proxy.renderList (app.js:4325)
    at Proxy.render (app.js:48648)
    at VueComponent.Vue._render (app.js:5164)
    at VueComponent.updateComponent (app.js:3408)
    at Watcher.get (app.js:3762)
    at Watcher.run (app.js:3839)
    at flushSchedulerQueue (app.js:3601)
    at Array.<anonymous> (app.js:2457)
    at flushCallbacks (app.js:2378)

The value of item.start = "09:30:00"

cmdobueno's avatar

You can not do that in computed.

Instead make it a method, be a filter.

This is the issue... because it is literally not a function as computed properties are NOT functions.

1 like

Please or to participate in this conversation.