Comments (5)
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.
@Joeper214 install moment for the filter not vue-moment. npm install --save moment
from vue-moment.
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.
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.
Unix epoch support added in #58— will close this when it's merged in.
from vue-moment.
Related Issues (20)
- Vulnerability in the moment module before 2.19.3 HOT 7
- Where can I add 'moment.suppressDeprecationWarnings = true'? HOT 1
- Cannot redefine property: $moment HOT 1
- vue moment not working when used in axios HOT 1
- "ReferenceError: moment is not defined" HOT 11
- Getting unrecognized time in 24hr format. HOT 5
- ./node_modules/moment/src/lib/locale/locales.js Critical dependency: the request of a dependency is an expression
- ./node_modules/moment/src/lib/locale/locales.js 78:12-82:13 Critical dependency: the request of a dependency is an expression HOT 1
- Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. HOT 1
- How to get timezone of client?
- Reduce bundle size? HOT 1
- Filters being deprecated in Vue 3 HOT 6
- moment-timezone does not work for tz Pacific/Auckland with vue-moment HOT 2
- Deprecation warning: use moment.updateLocale(localeName, config) to change an existing locale
- Compatibility issues with vue 3 HOT 2
- Example for using within string interpolation in Vue template HOT 2
- Deprecation warning - value provided is not in a recognized RFC2822 or ISO format
- Use 1 month/day ago etc instead of a month/day ago.
- Moment.js <2.29.2 vulnerabilities
- Abandoned repo ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-moment.