Vreme.js - The simplest date and time formatting library.

538 阅读2分钟
原文链接: stojanovic.github.io
The simplest date and time formatting library for JavaScript.

Vreme is a simple date and time formatter library, it formats date and time to match provided human-readable example. It is inspired by date formatting in Go lang and Stamp Ruby library.

Why? Because formating date and time should be as simple as posible and remembering mmm/MM/YYYY/%D formats is not.

"Vreme" means Time in Serbian.

Installation

Install from using npm:

npm install vreme

Install from using bower:

bower install vreme

Or download it on a Github.

Usage

Import Vreme and make a new instance, than you should simply provide date and human readable format.

If works with node.js and in the browser. It supports CommonJS, AMD and using as global variable.

Node.js / io.js

You can format full dates and times or just a month/day name, as you can see in the example below:



var Vreme = require('Vreme')

var vreme = new Vreme()

var date = new Date('Sat, 01 Aug 2015 14:10:21')

// To get month
console.log(vreme.format(date, 'January'))            // Output 'August'
console.log(vreme.format(date, 'Jan'))                // Aug
console.log(vreme.format(date, '12'))                 // 08
console.log(vreme.format(date, '2'))                  // 8

// To convert day names
console.log(vreme.format(date, 'Monday'))             // Saturday
console.log(vreme.format(date, 'monday'))             // saturday
console.log(vreme.format(date, 'Mon'))                // Sat
console.log(vreme.format(date, 'Mo'))                 // Sa
console.log(vreme.format(date, 'MO'))                 // SA
console.log(vreme.format(date, '1st'))                // 1st
console.log(vreme.format(date, '22'))                 // 01

// To get year
console.log(vreme.format(date, '1999'))               // 2015
console.log(vreme.format(date, '42'))                 // 15

// To get time
console.log(vreme.format(date, '4:10 pm'))            // '2:10 pm'
console.log(vreme.format(date, '15:32'))              // '14:10'

// To get full dates
console.log(vreme.format(date, 'March 25, 1999'))     // August 01, 2015
console.log(vreme.format(date, 'March 1, 1999'))      // August 1, 2015
console.log(vreme.format(date, 'March 25th'))         // August 1st
console.log(vreme.format(date, '2014/04/25'))         // 2015/08/01
console.log(vreme.format(date, '2014/04/25 4:10 am')) // 2015/08/01 2:10 pm
console.log(vreme.format(date, '02/03/11'))           // 08/01/15
console.log(vreme.format(date, '21.04.2015'))         // 01.08.2015

// Or you can combine it with text
console.log(vreme.format(date, 'Date: March, 25th'))  // Date: August, 1st

      

With prototype

If you pass

usePrototype: true
in options Vreme will be added as a method of Date object (`Date#formatLike`), so you can use it like this:


var Vreme = require('Vreme')

var vreme = new Vreme({
  usePrototype: true
})

var date = new Date('8/20/2015')

// Then you can get date like this
console.log(date.formatLike('March 25, 1999'))    // August 20, 2015

      

In the browser

Just include vreme.js or vreme.min.js in scripts and use the module same as described above.






  Vreme test


  
  



      

Limitations

This module is relying on regular expressions and I tried to keep it as simple as it's possible, so there is some limitations:

  • If you are using integers smaller than 13 for month, year and day (ie. '02/03/11') module will always assume `MM/DD/YY` format;
  • If you use integers between 13 and 31 for both year and day it'll assume (ie. '02/14/14') it'll assume DD/YY format;
  • At the moment it works only with month and day names in english.

There are additional limitation that are not on this list, they'll be added.

For more complex date formats consider using strftime or moment.js

Report issues

Use Githib issues to report a problem or suggest a new feature.

Development

Vreme is written in ES6 and compiled with Babel.

Intall Babel by running npm i babel -g then run npm install to install dev dependencies and npm run compile to transpile it to ES5.

If you want to run Babel manually, you'll need to do: babel src/index.js -o .tmp/vreme.js and then buildify to add support for CommonJS, AMD and script and to make minified version.

Test

Run npm test or mocha -R spec ./test/index.js.

Created by Slobodan Stojanovic; MIT Licence Fork me on GitHub