Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UNIX Timestamp #33

Closed
sebkolind opened this issue May 30, 2017 · 5 comments
Closed

UNIX Timestamp #33

sebkolind opened this issue May 30, 2017 · 5 comments

Comments

@sebkolind
Copy link

sebkolind commented May 30, 2017

I can't find a way to make it work with UNIX timestamps. Is it possible or did you cut off this feature?

Moment.js states you can do moment.unix(value).format("MM/DD/YYYY") but how can I do this with your filter?

@sebkolind
Copy link
Author

sebkolind commented May 30, 2017

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}`;
},

@mcescalante
Copy link

mcescalante commented Jun 9, 2017

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

@Joeper214
Copy link

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>

@savagemechanic
Copy link

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

@brockpetrie
Copy link
Owner

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

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

No branches or pull requests

5 participants