Giter VIP home page Giter VIP logo

Comments (5)

mcescalante avatar mcescalante commented on July 23, 2024 4

I just ran into this same issue and did some digging and found a nice simple fix. As you mentioned,moment itself as a library supports unix timestamps with moment.unix(). I installed moment, imported it, and added a filter into my component to do Unix timestamps for me:

Vue component

filters: {
  formatUnix: function (value) {
    if (value) {
      return moment.unix(value).format('MM-DD-YYYY')
    }
  }
}

Usage in template - no parenthesis required

{{ someVar | formatUnix }}

This will take Unix epoch (timestamp) as input and give you back MM-DD-YYYY. I prefer filters to methods. If you haven't written your own filters before, here are the docs

from vue-moment.

savagemechanic avatar savagemechanic commented on July 23, 2024 3

@Joeper214 install moment for the filter not vue-moment. npm install --save moment

from vue-moment.

sebkolind avatar sebkolind commented on July 23, 2024

If anyone has the same issue, then you can use this function in your methods property of your Vue component. This will format your unix timestamp to a valid date that Moment can use.

formatDate(unix) {
	const date = new Date(unix * 1000); // convert to milliseconds
	const year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();

	// Check if day or month is only 1 digit
	// this because Moment.js works with 0 leading values
	if (day.toString().length !== 2) day = `0${day}`;
	if (month.toString().length !== 2) month = `0${month}`;

	return `${year}-${month}-${day}`;
},

from vue-moment.

Joeper214 avatar Joeper214 commented on July 23, 2024

I install vue-moment in my vue appliction and created the filter but this returns:

[Vue warn]: Error in render function: "ReferenceError: moment is not defined"

found in

---> <App> at src/components/App.vue
       <Root>

from vue-moment.

brockpetrie avatar brockpetrie commented on July 23, 2024

Unix epoch support added in #58— will close this when it's merged in.

from vue-moment.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.