Giter VIP home page Giter VIP logo

vue-moment's Introduction

vue-moment

npm version Build Status License: MIT

Handy Moment.js filters for your Vue.js project.

Installation

Install via NPM...

$ npm install vue-moment

...and require the plugin like so:

Vue.use(require('vue-moment'));

Usage

Simply set moment as the filtering function and you're good to go. At least one argument is expected, which the filter assumes to be a format string if the argument doesn't match any of the other filtering methods.

<span>{{ someDate | moment("dddd, MMMM Do YYYY") }}</span>
<!-- or create a new date from 'now' -->
<span>{{ new Date() | moment("dddd, MMMM Do YYYY") }}</span>

Passing Your Date

Moment.js expects your input to be either: a valid ISO 8601 formatted string (see http://momentjs.com/docs/#/parsing/string/), a valid Date object, a Unix timestamp (in seconds, passed as a Number; if you'd like to pass milliseconds, send as a date object using new Date(milliseconds)), or a date string with an accompanying format pattern (i.e. when you know the format of the date input). For the latter, vue-moment allows you to pass your date and format pattern(s) as an array, like such:

<span>{{ [ someDate, "MM.DD.YY" ] | moment("dddd, MMMM Do YYYY") }}</span>
<!-- or when you want to parse against more than one pattern -->
<span>{{ [ someDate, ["MM.DD.YY", "MM-DD-YY", "MM-DD-YYYY"] ] | moment("dddd, MMMM Do YYYY") }}</span>

As of 3.0.0, passing an empty or invalid input will no longer initiate moment with a new Date object fallback.

Filtering Methods

format (default)

This is the default filtering option. Formats the date against a string of tokens. See http://momentjs.com/docs/#/displaying/format/ for a list of tokens and examples.

Default

<span>{{ someDate | moment("YYYY") }}</span>
<!-- e.g. "2010" -->
<span>{{ someDate | moment("ddd, hA") }}</span>
<!-- e.g. "Sun, 3PM" -->
<span>{{ someDate | moment("dddd, MMMM Do YYYY, h:mm:ss a") }}</span>
<!-- e.g. "Sunday, February 14th 2010, 3:25:50 pm" -->

For more information about moment#format, check out http://momentjs.com/docs/#/displaying/format/.

from

Display a moment in relative time, either from now or from a specified date.

Default (calculates from current time)

<span>{{ someDate | moment("from", "now") }}</span>
<!-- or shorthanded -->
<span>{{ someDate | moment("from") }}</span>

With a reference time given

<span>{{ someDate | moment("from", "Jan. 11th, 1985") }}</span>

With suffix hidden (e.g. '4 days ago' -> '4 days')

<span>{{ someDate | moment("from", "now", true) }}</span>
<!-- or -->
<span>{{ someDate | moment("from", true) }}</span>
<!-- or with a reference time -->
<span>{{ someDate | moment("from", "Jan. 11th, 2000", true) }}</span>

For more information about moment#fromNow and moment#from, check out http://momentjs.com/docs/#/displaying/fromnow/ and http://momentjs.com/docs/#/displaying/from/.

calendar

Formats a date with different strings depending on how close to a certain date (today by default) the date is.

Default (calculates from current time)

<span>{{ someDate | moment("calendar") }}</span>
<!-- e.g. "Last Monday 2:30 AM" -->

With a reference time given

<span>{{ someDate | moment("calendar", "July 10 2011") }}</span>
<!-- e.g. "7/10/2011" -->

For more information about moment#calendar, check out http://momentjs.com/docs/#/displaying/calendar-time/.

add

Mutates the original moment by adding time.

<span>{{ someDate | moment("add", "7 days") }}</span>
<!-- or with multiple keys -->
<span>{{ someDate | moment("add", "1 year, 3 months, 30 weeks, 10 days") }}</span>

For more information about moment#add, check out http://momentjs.com/docs/#/manipulating/add/.

subtract

Works the same as add, but mutates the original moment by subtracting time.

<span>{{ someDate | moment("subtract", "3 hours") }}</span>

For more information about moment#subtract, check out http://momentjs.com/docs/#/manipulating/subtract/.

timezone

Convert the date to a certain timezone

<span>{{ date | moment('timezone', 'America/Los_Angeles', 'LLLL ss')}}</span>

To use this filter you will need to pass moment-timezone through to the plugin

// main.js
import Vue from 'vue'
import VueMoment from 'vue-moment'
import moment from 'moment-timezone'

Vue.use(VueMoment, {
    moment,
})

For more information about moment#timezone, check out https://momentjs.com/timezone/docs/#/using-timezones/converting-to-zone/.

Chaining

There's some built-in (and not thoroughly tested) support for chaining, like so:

<span>{{ someDate | moment("add", "2 years, 8 days", "subtract", "3 hours", "ddd, hA") }}</span>

This would add 2 years and 8 months to the date, then subtract 3 hours, then format the resulting date.

Configuration

vue-moment should respect any global Moment customizations, including i18n locales. For more info, check out http://momentjs.com/docs/#/customization/.

You can also pass a custom Moment object through with the plugin options. This technique is especially useful for overcoming the browserify locale bug demonstrated in the docs http://momentjs.com/docs/#/use-it/browserify/

const moment = require('moment')
require('moment/locale/es')

Vue.use(require('vue-moment'), {
    moment
})

console.log(Vue.moment().locale()) //es

vue-moment's People

Contributors

brockpetrie avatar brockreece avatar wilk avatar adamjlev avatar andrew avatar isidentical avatar fercreek avatar hardlymirage avatar mc100s avatar ramytamer avatar wlepinski avatar n3gotium avatar

Watchers

 avatar James Cloos avatar

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.