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

javascript - Using moment.js with vue.js - Stack Overflow

programmeradmin2浏览0评论

I'm working with Bootastrap-Vue in bination with JavaScript and I want to use the Moment.js in my code.

I get the time but it's not correct...Can some one please help me??

By the way, this is my first question at StackOverflow, so I apologize if I'm ask it wrong.

Thank you.

var moment = require('moment')
export default {
    name: 'something',
    data() {
        return {
            something: [],
            currentPage: 1,
            total_something: 1,
            something_fields: {
                id: {
                    label: 'Id',
                    sortable: true
                },
                purpose: {
                    label: 'Purpose',
                    sortable: false
                },
                state: {
                    label: 'State',
                    sortable: false
                },
                updated: {
                    key: 'updated',
                    label: 'Updated',
                    formatter: (value, key, item) => {
                            return moment(item.updated).calendar();

                    }
                }
            },
        }
    },
    created() {
        this.loadSomething(0, 10)
    },
    watch: {
        currentPage: function (newPage) {
            this.loadSomething(newPage, 10)
        }
    }, methods: {
        loadSomethings(currentPage, limit) {
            if (!(Number.isInteger(currentPage) && Number.isInteger(limit))) {
                currentPage = 0
                limit = 10
            }
            var offset = (currentPage - 1) * limit
            window.API.get('something?offset=' + offset + '&limit=' + limit)
                .then(response => {
                    this.something = response.data.something;
                    this.total_something s = response.data.total;
                    console.log(response.data.something)
                })
                .catch(e => {
                    this.errors.push(e)
                })
        }
    }
}

I'm working with Bootastrap-Vue in bination with JavaScript and I want to use the Moment.js in my code.

I get the time but it's not correct...Can some one please help me??

By the way, this is my first question at StackOverflow, so I apologize if I'm ask it wrong.

Thank you.

var moment = require('moment')
export default {
    name: 'something',
    data() {
        return {
            something: [],
            currentPage: 1,
            total_something: 1,
            something_fields: {
                id: {
                    label: 'Id',
                    sortable: true
                },
                purpose: {
                    label: 'Purpose',
                    sortable: false
                },
                state: {
                    label: 'State',
                    sortable: false
                },
                updated: {
                    key: 'updated',
                    label: 'Updated',
                    formatter: (value, key, item) => {
                            return moment(item.updated).calendar();

                    }
                }
            },
        }
    },
    created() {
        this.loadSomething(0, 10)
    },
    watch: {
        currentPage: function (newPage) {
            this.loadSomething(newPage, 10)
        }
    }, methods: {
        loadSomethings(currentPage, limit) {
            if (!(Number.isInteger(currentPage) && Number.isInteger(limit))) {
                currentPage = 0
                limit = 10
            }
            var offset = (currentPage - 1) * limit
            window.API.get('something?offset=' + offset + '&limit=' + limit)
                .then(response => {
                    this.something = response.data.something;
                    this.total_something s = response.data.total;
                    console.log(response.data.something)
                })
                .catch(e => {
                    this.errors.push(e)
                })
        }
    }
}
Share Improve this question asked Jan 24, 2018 at 7:20 omizomiz 9491 gold badge8 silver badges11 bronze badges 3
  • 2 I get the time but it's not correct is not very helpful. – connexo Commented Jan 24, 2018 at 7:22
  • moment(item.updated).calendar(); -> What is item.updated ? Why are you using calendar ? What is the current output versus the expected output ? What is the result of console.log(moment().format("DD/MM/YYYY HH:mm") ? – Weedoze Commented Jan 24, 2018 at 7:26
  • Dear Weedoze, like I said, this is my first time and I'm also junior programmer. The reason I use calendar is to print like for example(Last Thursday at 3:32 PM) or (Today at 9:00 AM). console.log(moment().format("DD/MM/YYYY HH:mm") has not results in my code. – omiz Commented Jan 24, 2018 at 8:08
Add a ment  | 

2 Answers 2

Reset to default 1
formatter: (value, key, item) => {
      if(item.updated) {
         return moment(item.updated).format(); // format(YOUR DATE FORMAT)
      }

}

https://momentjs./docs/

use this :

updated:{
    key: 'updated',
    label: 'Updated',
    formatter: (value, key, item) => {
        return moment(item.updated).format('DD-MM-YYYY');
    }
}
发布评论

评论列表(0)

  1. 暂无评论